Can't open image file using UXP script!

Hello everyone;

I’m an absolute beginner here, I’m trying to simply open a png document (or any image document) using UXP script, but no matter what I do, this won’t happen, can you please help me, here is my code.

let entry = await require('uxp').storage.localFileSystem.getFileForOpening()
const document = await app.open(entry);

supplying the file path directly does not work either:

const document = await app.open("C:\\Users\\Me\\Pictures\\1.jpg");

I’m loading the script using File>Scripts>Browse, The file picker appears, but when I select a file nothing happens!!
Please help me, I have been scratching my head for 2 days! My photoshop version is 24.

Regards

Anything that changes the state of Photoshop needs to be wrapped in an executeAsModal function, see the docs for more detail.

Also, UXP doesn’t allow you access to the full filesystem without user permission - which you’re doing with the picker and why the raw path fails - but you also need to generate a token for the file you want to work with and then pass the token to the open function rather than the path.

1 Like

Thank you for your kind reply, unfortunately, this is not working for me, here is my code which is taken from the documentation, I just added an executeAsModal function:

async function actionCommands() {
    const fs = require('uxp').storage.localFileSystem;
    let entry = await fs.getFileForOpening();
    let token = fs.createSessionToken(entry);
    let result = await require('photoshop').action.batchPlay([{
        _obj: "open",
        "target": {
            _path: token, // Rather than a system path, this expects a session token
            _kind: "local",
        }
    }], {});
}
async function runModalFunction() {
    await require("photoshop").core.executeAsModal(actionCommands, { "commandName": "Action Commands" });
}
await runModalFunction();

supplying the generated token to the open method of the app class did not work either, supplying the entry to the open method (like my old code) and adding executeAsModal function also did not work, I can’t believe opening a file could be that hard, may it is something related to my system?!

1 Like

Is this inside an async function too? I’m going to guess that it isn’t and is causing your error.
await runModalFunction();

Try wrapping all your code in a try/catch statement to log any errors to console and see what it tells you.

1 Like

@shwan Opening an image file via a UXP script will be available in the next version of UXP. For now, this behavior can only be accomplished via UXP plugins.

Here’s a sample plugin demonstrating this:
dec1.zip (8.7 KB)

Thank you for that info, it was not very obvious in the documentation.

@shwan Yes, we’ll work on updating the docs.