Can i run javascript.jsx file using UXP button

can i run javascript.jsx file using UXP button

as far as I know there’s no direct way of running JSX atm, but you can use batchplay to do so

JAVASCRIPT

const app = require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
async function ScriptRun(jsxFile) {
   let pluginFolder = await fs.getPluginFolder()
   try {
      let jsxFileObject = await pluginFolder.getEntry(jsxFile);
      var filetoken = await fs.createSessionToken(jsxFileObject);
   } catch (e) {
      app.showAlert("File Can't be found!");
   }
   return await batchPlay(
      [
         {
            "_obj": "AdobeScriptAutomation Scripts",
            "javaScript": {
               "_path": filetoken,
               "_kind": "local"
            },
            "javaScriptMessage": "JSM",
            "_isCommand": true,
            "_options": {

               "dialogOptions": "dontDisplay"

            }
         }
      ], {
      "synchronousExecution": false,
      "modalBehavior": "fail"
   });

}

FUNCTION CALL

ScriptRun("jsx/myScript.jsx")

where the file myScript.jsx is located inside a directory called jsx in the plugin folder

credits to @DavideBarranca

also if I may suggest using the following way to bind the event your script runners (buttons), since in my case I use the function on a lot of buttons.

HTML

<button class="sr" sr-path="jsx/script1.jsx">Script 1</button>
<button class="sr" sr-path="jsx/script2.jsx">Script 2</button>
...

and to bind all of them I use the “sr” class and the path from “sr-path” attribute like so:

JAVASCRIPT

Array.from(document.querySelectorAll(".sr")).forEach(sr => {
   sr.onclick = (e) => {
      ScriptRun(e.target.getAttribute("sr-path"))
   }
});

:red_circle: Do not do this if you plan on distributing your plugin to others. There is no guarantee that plugins that run JSX like this will continue to work in the future, and your plugin may be removed from the marketplace.

Convert to batchPlay and the PS DOM API instead.

Thanks
I am not familiar with modern java.
this UXP only using my works.
my old javascript are working good and I cannot re create to UXP
that is why Iam asking how to connect java script to uxp. I will try this.
Thanks for the advise.

UXP uses JavaScript just like JSX Scripts but you’ll have to relearn some stuff but the concept is the same. I recommend you port your code while you have the time since JSX is deprecated and they might stop supporting it in the future.
this is a good place to start

1 Like

Thanks for the advice.