1

I am attempting to use chrome.apps for a program that needs to write multiple separate log files of data. The user then needs to be able to access these these log files outside of the app in their file system for post processing.

This will need to be done many times so minimum to no user interaction would be desired for file generation. While this is simple in any native program code, I've been finding this very difficult to do with chrome apps. E.g. can I use chrome apps to create "log_file.txt" & "log_file2.txt" without user interaction? Is there any way I can have the user just specify a directory then from my app, I would be able to create multiple files within that directory without user interaction? I've tried to do this in code but I need "entry" handles for the chrome.filesystem. The "getEntry" method requires an "entry" so it seems impossible to create new "entry"s such that I can write to new files. Any ideas would be appreciated!

Jongware
  • 22,200
  • 8
  • 54
  • 100
NathanZ
  • 15
  • 4
  • Welcome to Stack Overflow! I see you edited your question (which is good) just to add the solution - which is not. Stack Overflow is a site for *questions and answers*. If your own answer substantially derives from the one you accepted, you can always post it as a separate answer. – Jongware Feb 05 '16 at 20:51

1 Answers1

3

Is there any way I can have the user just specify a directory then from my app, I would be able to create multiple files within that directory without user interaction?

Yes. You need to request a directory with

chrome.fileSystem.chooseEntry({type: "openDirectory"}, /*...*/);

As long as you have the permissions

{"fileSystem": ["write", "retainEntries", "directory"]} 

you will be able to create files in that directory, and "retain" (save) the directory entry for later reuse without asking the user again. Creating the files once you have a DirectoryEntry should be similar to this.

But that minimum of interaction (asking for the folder initially) is required.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • A directory handle cannot create a file system object though. To get a file system object via chrome api one must use chrome.requestFileSystem . This requires an options which provides a volume ID. I don't think there is a way to go from directory handle to volume ID. Furthermore, the getVolumeList doesn't appear to be supported by most operating systems . – NathanZ Feb 05 '16 at 19:43
  • A `DirectoryEntry` has a method `getFile` that can be used to create a file. You don't need to create a filesystem - consider that, in terms of HTML5Rocks article I linked, you get `fs.root` directly. – Xan Feb 05 '16 at 19:44
  • Hey Xan, sorry, accidentally hit the enter key too quickly, please see my edited previous comment. – NathanZ Feb 05 '16 at 19:46
  • You don't need that! You need to call `chooseEntry` once, get a `DirectoryEntry`, make sure you retain it and use it. – Xan Feb 05 '16 at 19:48
  • Many thanks, using the .getFile method of the directory entry worked. :) – NathanZ Feb 05 '16 at 20:39