UXP Get Folder or FIle from path

Is there a way to get a file or folder from a known local path or activeDocument path?
I am only finding documentation of using localFileSystem.getFolder() or localFileSystem.getFileForOpening(); which opens the file picker dialog.
I want to avoid the user having to choose the file or folder because it is already known from the activeDocument.path.
With extendscript I would simply reference the folder, check it and create it if it doesn’t exist with:

var myFolder = new Folder("my/local/path");
if (myFolder.exists == false);{

I would also like to be able to automatically open specific files from a known path with out the use of a user file picker dialog. I had a way to do this in extendscript and even embed the image on the activeDocument, but I am not finding the UXP documentation that will do the same.

var myImg = new File("~/my/path/img.jpg");
function placeEmbedded(filePath) {  
        var actDesc = new ActionDescriptor();  
        actDesc.putPath(charIDToTypeID('null'), filePath);  
        actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));  
        executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO);  

AFAIK, without file picker you can access files only from the plugin folder. Otherwise you will need a file picker. After user selects folder once, you can save a token and using this token. Then you can access all files in the folder recursively without the picker. But you still need to show picker at least once to get the token.

UXP is a sandboxed environment, meaning that users must grant explicit access to their documents to your plugin. Your plugin cannot assume that it has full rights to the user’s data on the hard drive.

The file picker follows a similar pattern in iOS and others whereby picking a file or folder from the picker grants consent & permission — this also helps us work within the OS sandboxes which are becoming increasingly restrictive – anything provided through the file picker is granted at the OS level too.

You can ask for a folder, and you’ll get access to everything inside, recursively. Asking for what would essentially be the same folder multiple times can be annoying though, and so you can convert those tokens into persistent tokens that don’t require multiple consent responses.

See https://developer.adobe.com/photoshop/uxp/2022/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/FileSystemProvider/#createpersistenttokenentry

You can then convert that persistent token into a session token for use w/ Ps APIs.

Persistent tokens may still fail if the underlying permission model changes, so if you can’t obtain a token, you need to route through the file/folder picker again.

While this is more restrictive than ExtendScript, it’s done with multiple factors in mind:

  • User’s privacy
  • Working with OS-level sandboxes
  • Some basic level of trust such as ensuring that plugins can’t just accidentally mess up the file system
1 Like