Run not yet supported UXP API features via JSX

Context

I’m transitioning a large code base of Extend Script code into a UXP environment. Unfortunately UXP is very much still under development, and a number of document operations are not yet functional. For example, layer comp CRUD behavior, but there are likely others. As a workaround, I’ve built the user interface in UXP, and am attempting to continue using Extend Script for the document operations. I am more than happy to depreciate the Extend Script code as UXP’s ambitions are better supported.

The last, and perhaps final, milestone towards a minimum viable product involves using Batch Play to call a JSX file residing within the developer plugin folder (to avoid user input). It was once functioning but is no longer.

Related Code

let batchPlay = require("photoshop").action.batchPlay;
let filesystem = require("uxp").storage.localFileSystem;

export async function runPluginJsx(filename) {
    let folder = await filesystem.getDataFolder()
    const filepath = filename + ".jsx";
    let jsxFileObject = await folder.getEntry(filepath);
    let filetoken = await filesystem.createSessionToken(jsxFileObject);
    let command = [
        {
            "_obj":"AdobeScriptAutomation Scripts",
            "javaScript":{
                "_kind":"local",
                "_path": filetoken,
            },
            "javaScriptMessage": "JSM",
            "_isCommand": true,
            "_options": {
               "dialogOptions": "dontDisplay"
            }
        }
    ];
    console.log("before") // prints
    let result = await batchPlay(command, {
        "synchronousExecution": false,
        "modalBehavior": "fail"
    });
    console.log("after") // does not print
    console.log(result) // does not print
    return result
}

If I remove the await call for result (an error handling bug?), then I am able to see the promise is rejected for the following reason:

Error: Event: AdobeScriptAutomation Scripts may modify the state of Photoshop. Such events are only allowed from inside a modal scope

If a modal is required, can someone please point me towards a code sample that might help?

Error: Event: AdobeScriptAutomation Scripts may modify the state of Photoshop. Such events are only allowed from inside a modal scope

I am still navigating and learning scripting. my understanding from this error is that you need to use ExecuteAsModal for anything that change Photoshop state.

check this link may give you an idea

1 Like

Thanks @Wanokuni - with your help, I have a working solution. It does not currently require user input, so all goals are accomplished for the time being.

let batchPlay = require("photoshop").action.batchPlay;
let filesystem = require("uxp").storage.localFileSystem;
let core = require("photoshop").core;

export async function runPluginJsx(filename) {
    try {
        let result = await core.executeAsModal(
            batchPlayJsx,
            {
                "commandName": "Batch Play Extend Script JSX",
                "descriptor": {"filename": filename}
            }
        )
        return result
    } catch(e) {
        console.log(e)
    }
}

async function batchPlayJsx(executionContext, descriptor) {
    let folder = await filesystem.getDataFolder()
    const filepath = descriptor.filename + ".jsx";
    let jsxFileObject = await folder.getEntry(filepath);
    let filetoken = await filesystem.createSessionToken(jsxFileObject);
    let command = [
        {
            "_obj":"AdobeScriptAutomation Scripts",
            "javaScript":{
                "_kind":"local",
                "_path": filetoken,
            },
            "javaScriptMessage": "JSM",
            "_isCommand": true,
            "_options": {
               "dialogOptions": "dontDisplay"
            }
        }
    ];
    let result = await batchPlay(command, {
        "synchronousExecution": false,
    });
    return result
}