Create new folder without dialog using UXP

I’m probably overlooking something simple here. I just want to create a folder if it does not exist.

In CEP, all I have to do is

let outputFolder = app.Folder(outputFolderStr);
if (!outputFolder.exists) outputFolder.create();

How can I do this in UXP?

According to the storage APIs, I cannot see an obvious way to create a folder reference to a specific folder. All it says is:

// These require no user interaction:
const tempFolder = await fs.getTemporaryFolder();
const pluginFolder = await fs.getPluginFolder();  // read-only access to the plugin's install folder
const pluginDataFolder = await fs.getDataFolder();  // folder to store settings

// Display file/folder picker UI to access user files:
const userFolder = await fs.getFolder();  // folder picker

To provide some context, we have an in-house batch image converter tool built in CEP which I want to convert to UXP. Two of the fields are input and output folders which are saved as preferences. And the artists prefer to copy and paste the directories from file explorer rather than use folder pickers.

The UXP file system is sandboxed, much like modern OSes are becoming. As such, beyond files in the plugin’s data, bundle, or temporary folder, users have to grant access explicitly to files. Once granted, you can convert these to persistent references (using createPersistentTokenEntry) and your user won’t need to be prompted again.

Access to folders will grant access to everything within, recursively. This means that for your purposes, you could ask for access to root volume(s) once, and then you’d have access to everything inside. You can use folder#getEntry(path) to use string-like paths to get access to specific locations.

Note that if you’re targeting the marketplace for distribution, you generally shouldn’t ask for the root volume. But if you’re doing this for a client or enterprise and aren’t going through the marketplace for delivery, this should work.

In the medium term, we’ll be adding Drag and Drop File support, meaning that you could have users drop a folder onto the panel, which would grant access as well. Longer term, we’ll look at adding a specific permission that grants full access automatically (again, not for marketplace plugins), but for now, the above is the best way forward.

1 Like