Open Finder folder window from button

Hello, is it possible to open a Finder folder window from a UXP panel? I’ve been experimenting with something like:

const {shell} = require(“uxp”);
shell.openExternal(“/Users”);

but get an error in Finder (-50) and it doesn’t work. Doing the same in Windows works as expected, for example:

const {shell} = require(“uxp”);
shell.openExternal(“D:\Work”);

Anyone know how to replicate this in macOS? Very grateful for any help.

1 Like

Ok silly me, I just needed to add the “file://” at the beginning! This works

const {shell} = require("uxp");
shell.openExternal("file:///Users");
2 Likes

A little more context:
Successfully opening anything via shell.openPath / openExternal requires manifest V5, and setting proper permissions for launching stuff.

Note that if you want to open a FOLDER, and not a file, you have to include "" in the "extensions":[] array. Here is what I use in my v5 manifest in order to get shell.openPath to open a folder:

  "requiredPermissions": {
    "localFileSystem": "fullAccess",
    "launchProcess": {
      "extensions": [
        ""
      ]
    }

e.g. To open .txt files, youd add “.txt” to the extensions array.

2 Likes

hi @Zob @grebesoft_ben could you explain a little bit about getting shell.openPath to open a folder? On windows would it be the same as the file explorer window? Man this might be what I need to access any folder in any directory.

Yes, using shell.openPath actually opens a file Explorer window on Windows if you pass in the path to a folder. I use it so a user can see the folder containing the content generated by my plugin. doing this is of no use to my plugin, because I get no variable with which to manipulate the folder.

If you are interested in having programmatic control of any folder on the harddrive, such as being able to read and save files via javascript, you will need to use require('uxp').storage.localFileSystem .

  1. ask the user for the root drive (e.g. J:// ) by using localFileSystem.getFolder()
    Note: You will need to explicitly tell them to select the root drive using a pop up before calling getFolder()
  2. The folder they picked returns to you as a folderEntry object. Ensure its the root drive, and then use localFileSystem.createPersistentToken(folderEntry) to create a token, which allows you to get this file entry in the future without asking the user.
  3. Save the token to your plugin’s localStorage. In the future, when a user opens photoshop again, your code should check localStorage for the token, thus avoiding steps 1 and 2. Instead, you just call localFileSystem.getEntryForPersistentToken(token)

In order to access the subfolders, you need to write a function that calls getEntries() on your current folderEntry, find the right subfolder in that array, and then creates a new folderEntry or fileEntry from that, and then calls getEntries() again. You repeat this until you reach your desired folder or file. You could probably write a recursive function for this.

If someone knows a less convoluted way to access an entire drive, please share. This is the only method I found.

2 Likes

@Zob This is fantastic, it worked almost perfect! Thanks,
I also need this feature so that the user gets the directory with content generated after the finished event and can select the files and drag them to another program.
The problem is how to avoid the “Request For Permission” window, this is not cool when working with different directories. Thanks for pointing me to the right path.
Captura de tela 2023-05-10 084215

1 Like

Agreed, it’s a hassle for the user to keep approving these with workflows which might organize the user’s work into different folders.

Opening a folder doesn’t create a security risk, writing data does. If there is a concern that a plugin might entice a user to click on a bad file it creates, not showing the folder doesn’t eliminate that risk.

@pkrishna @kerrishotts Is there a way to avoid granting permissions for each unique folder a plugin wishes to show? If not possible now, perhaps it could be removed or revised to use a single approval for the plugin to show any folder it is allowed to read/write?

3 Likes

This works for me also in InDesign

I also find this permission dialog unnecessary when a folder is opened and it confuses the user. Please take another look at this @pkrishna @indranil