Execute a script via a button

I know its possible to convert an action to a uxp script but is it possible to execute a script from a uxp button? At times its more usful to be able to edit the action rather than remake the script everytime.

Thanks

Do you mean to play an action, or run a JSX script? Yes, either can be done.

I have a UXP plugin that plays actions and there have been no issues at all with that.

You can also play JSX extendscript through UXP. I don’t use this in my plugins but I did do some testing. It works fine but for some reason runs at about 1/2 the speed that my JSX extendscripts run when ran natively, not through UXP.

Here is the documentation for playing actions.

https://www.adobe.io/photoshop/uxp/ps_reference/classes/action/
https://www.adobe.io/photoshop/uxp/ps_reference/classes/actionset/

I currently play actions using batchPlay and have a report that if the action contains a script (.jsx), then it won’t play from the plugin. So using APIs to play actions instead of batchPlay may be preferable.

I found this code in the tutorials

  const app = require('photoshop').app;
  const allActionSets = app.actionTree;
  const firstActionSet = allActionSets[0];
  let actions = new Map(); // a JS Map allows easy "find by name" operations
  firstActionSet.actions.forEach((action) => { actions.set(action.name, action)});
  const myAction = actions.get("Wood Frame - 50 pixel");
  if (myAction) { // user may have deleted this action
    await myAction.play();
  }

I assume if i change “Wood Frame - 50 pixels” to my action name it should then point to my action. I have 2 questions though.

  1. How do i set the actionset folder name?
  2. how do i bind this to a buttonid?

Can anyone help me with this please, i still can’t figure out how to play an action from a UXP button.

I’m sure this isn’t the most efficient use of javascript and forgive the longhand… but it works.

//Use same ID in HTML doc
document.getElementById("yourButtonID").addEventListener("click", async function (){
await playAction("Your Action Set","Your Action");
});

async function playAction(setName,actionName){

let setFound=false;    
let actionFound=false;    
 
  const actionSets = require('photoshop').app.actionTree;
    
 // check for action set name to exist    
  for (let i=0; i<actionSets.length; i++){
      if (actionSets[i].name==setName){var setToPlay=actionSets[i]; setFound=true;}
  }
  
 //check for action name to exist    
 if(setFound==true){
    for (let i=0; i<setToPlay.actions.length; i++){
    if (setToPlay.actions[i].name==actionName){var actionToPlay=setToPlay.actions[i]; actionFound=true;}
    }
}    

 //play if action set/action both exist    
  if(setFound==true&&actionFound==true){await actionToPlay.play();}    
    
}
    

I’m getting an error, i think its because of a ‘{’ at the end of the line ‘async function playAction(setName,actionName){’

It doesn’t seem to be closed again. Where should the ‘}’ go?

I tried it at the end but the action wouldn’t play from the button

What is the exact error?

I must have missed the closing bracket when I copied it from my plusing. I updated the post. It is the exact function I use for multiple plugins and I’ve never had it give an error.

OK, I just updated it again. I missed the last line and the closing bracket when I copied it last time… oops.

//Use same ID in HTML doc
document.getElementById("yourButtonID").addEventListener("click", async function (){
await playAction("Your Action Set","Your Action");
});

async function playAction(setName,actionName){

let setFound=false;    
let actionFound=false;    
 
  const actionSets = require('photoshop').app.actionTree;
    
 // check for action set name to exist    
  for (let i=0; i<actionSets.length; i++){
      if (actionSets[i].name==setName){var setToPlay=actionSets[i]; setFound=true;}
  }
  
 //check for action name to exist    
 if(setFound==true){
    for (let i=0; i<setToPlay.actions.length; i++){
    if (setToPlay.actions[i].name==actionName){var actionToPlay=setToPlay.actions[i]; actionFound=true;}
    }
}    

 //play if action set/action both exist    
  if(setFound==true&&actionFound==true){await actionToPlay.play();}    
    
}