Storage
Using the File API
XD provides a simple, cross-platform API surface that makes it easy to read and write text and binary files. Three key things to note:
- Instead of working with string paths, you work with File and Folder objects.
- Access to the user's files requires showing a file picker UI, but you can access temporary files and any files packaged inside your plugin without needing any user interaction. If you have a File/Folder object, then you have access to the corresponding item on disk.
- File APIs are asynchronous, returning Promises which you can use with
then()
or theasync
/await
keywords.
Getting access to the local file system
Start using the file system APIs by obtaining a FileSystemProvider object (shortened to fs
in the code here):
Copied to your clipboardconst fs = require("uxp").storage.localFileSystem;
You can use the fs
object to access a temporary folder or your plugin's own folder immediately, or request access to user folders by showing a file picker:
Copied to your clipboard1// These require no user interaction:2const tempFolder = await fs.getTemporaryFolder();3const pluginFolder = await fs.getPluginFolder(); // read-only access to the plugin's install folder4const pluginDataFolder = await fs.getDataFolder(); // folder to store settings56// Display file/folder picker UI to access user files:7const userFolder = await fs.getFolder(); // folder picker8const aFile = await fs.getFileForOpening(); // "Open" file picker, suitable for reading contents9const anotherFile = await fs.getFileForSaving("hello.txt"); // "Save" file picker, suitable for writing contents
If you have a Folder, you can use getEntries()
to enumerate the folder's contents:
Copied to your clipboard1const entries = await folder.getEntries();2entries.forEach((entry) => console.log(entry.name));
If you have a File, you can read and write its contents like so:
Copied to your clipboard1const contents = await aFile.read();2await anotherFile.write(contents);
You can create new files inside a folder you have access to:
Copied to your clipboard1const newFile = await folder.createFile("examples.txt", { overwrite: true });2newFile.write("Hello, world!");
See the full reference.