# Execute a script via a button

**URL:** <https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699>\
**Category:** UXP Plugin API\
**Created:** [October 4, 2021, 4:26pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699 "2021-10-04T16:26:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pye](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Pye](https://forums.creativeclouddeveloper.com/u/Pye)\
**Post date:** [October 4, 2021, 4:26pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/1 "2021-10-04T16:26:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [October 4, 2021, 5:44pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/2 "2021-10-04T17:44:09Z")

</div>

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/action/)  
[https://www.adobe.io/photoshop/uxp/ps\_reference/classes/actionset/](https://www.adobe.io/photoshop/uxp/ps_reference/classes/actionset/)

---

<div class="post-metadata">

**Author:** ![AnthonyK](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AnthonyK](https://forums.creativeclouddeveloper.com/u/AnthonyK)\
**Post date:** [October 6, 2021, 6:55pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/3 "2021-10-06T18:55:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pye](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Pye](https://forums.creativeclouddeveloper.com/u/Pye)\
**Post date:** [October 6, 2021, 11:04pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/4 "2021-10-06T23:04:35Z")

</div>

I found this code in the tutorials

```auto
  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?

---

<div class="post-metadata">

**Author:** ![Pye](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Pye](https://forums.creativeclouddeveloper.com/u/Pye)\
**Post date:** [October 21, 2021, 10:01pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/5 "2021-10-21T22:01:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [October 21, 2021, 11:01pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/6 "2021-10-21T23:01:50Z")

</div>

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

```auto
//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();}    
    
}
    

```

---

<div class="post-metadata">

**Author:** ![Pye](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Pye](https://forums.creativeclouddeveloper.com/u/Pye)\
**Post date:** [October 22, 2021, 1:41pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/7 "2021-10-22T13:41:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [October 22, 2021, 8:40pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/8 "2021-10-22T20:40:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [October 22, 2021, 8:49pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/9 "2021-10-22T20:49:32Z")

</div>

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

```auto
//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();}    
    
}

```

---

<div class="post-metadata">

**Author:** ![Pye](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Pye](https://forums.creativeclouddeveloper.com/u/Pye)\
**Post date:** [October 25, 2021, 4:20pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/10 "2021-10-25T16:20:32Z")

</div>

Thank you very much, that worked perfectly.

Are all the checks needed? I didn’t see any errors or pop ups when i tried to assign an action that didn’t exist. I have 50+ actions so less code would be more benficial to me

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [October 27, 2021, 1:37am UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/11 "2021-10-27T01:37:29Z")

</div>

That is your decision. You can put an else statement in there to prompt an error. In my case, I didn’t want it to throw an error and just move on if the action doesn’t exist.

---

<div class="post-metadata">

**Author:** ![karpiyon](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@karpiyon](https://forums.creativeclouddeveloper.com/u/karpiyon)\
**Post date:** [July 1, 2022, 6:43pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/12 "2022-07-01T18:43:52Z")

</div>

Dies this still work in PS 23?  
I use the exact code but my action is not played.  
The action has no extendedscript adn when I debug it both setFound & actionFound are true before it tries to play it.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [July 1, 2022, 6:55pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/13 "2022-07-01T18:55:49Z")

</div>

I don’t think it works in recent Photoshop versions. I’m pretty sure that UXP scripting is replacing extendscript. So maybe it doesn’t work anymore by design… not sure.

---

<div class="post-metadata">

**Author:** ![karpiyon](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@karpiyon](https://forums.creativeclouddeveloper.com/u/karpiyon)\
**Post date:** [July 1, 2022, 6:58pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/14 "2022-07-01T18:58:40Z")

</div>

UXP scripting is still missing many methods which exist in extendedacript but are still missing in UXP.  
Even if I wanted to convert my existing CEP plugin to UXP it would be impossible at this stage.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [July 1, 2022, 7:06pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/15 "2022-07-01T19:06:10Z")

</div>

I’m not sure why it doesn’t work anymore. It could be a bug. You would have to ask Adobe on that. I just know it stopped working at least 6 months ago, maybe longer. You can’t play an action through UXP containing an extendscript either. One of my plugins plays actions from a Hot Folder. Several of my users were running actions that played scripts. It stopped working for all of them. I don’t know if there is anyway to play a script through UXP at all right now. Again, Adobe would have to give an official answer about if it is possible, or if it is a bug or by design.

---

<div class="post-metadata">

**Author:** ![Wanokuni](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/wanokuni/32/2203_2.png) [@Wanokuni](https://forums.creativeclouddeveloper.com/u/Wanokuni)\
**Post date:** [July 2, 2022, 4:13pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/16 "2022-07-02T16:13:43Z")

</div>

Interesting! @ddbell Your code actually works well for me. for either run normal actions or a JSX script.

```auto
const app = require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const ExecuteAsModal = require("photoshop").core.executeAsModal;

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();
  }
}

document
  .getElementById("action1")
  .addEventListener("click", async function () {
    await ExecuteAsModal(() => playAction("Actions set Name", "Action 1"));
  });

```

but I have notice different behaviour based on how I run the JSX.  
if I have the JSX play like this

 ![Screenshot 2022-07-02 at 18.59.03](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/2/2b072a4cf938001497b0c67c5003d8cb5d6c0e00.png)

It won’t work And I get this error:

 ![Screenshot 2022-07-02 at 18.58.25](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/7/72f21997ba7cbca1869abc76728e9d42f1ab3136.png)

but if I set it to play an action from another set that contain JSX file it works

 ![Screenshot 2022-07-02 at 19.00.33](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/f/f267065c07e7ffe593ac0c8451301fa295de56eb.png)

btw: I am in Photoshop 23.4.1 api 2 manifest 5

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [July 17, 2022, 10:07pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/17 "2022-07-17T22:07:37Z")

</div>

That is interesting. However, I don’t know if I would trust that workaround to be future-proof. I think JSX is on it’s way out. I have seen things in my JSX scripts stop working on newer PS releases. So even if you get it to play, the doesn’t guarantee that the script will work correctly in future PS releases. For myself, I have 1 last JSX program that I am trying to convert to UXP so my collection of plugins will all be JSX-free.

---

<div class="post-metadata">

**Author:** ![Jarda](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jarda/32/1909_2.png) [@Jarda](https://forums.creativeclouddeveloper.com/u/Jarda)\
**Post date:** [July 17, 2022, 10:19pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/18 "2022-07-17T22:19:44Z")

</div>

Don’t rely on it. This one might require your intervention in the future.

---

<div class="post-metadata">

**Author:** ![Wanokuni](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/wanokuni/32/2203_2.png) [@Wanokuni](https://forums.creativeclouddeveloper.com/u/Wanokuni)\
**Post date:** [July 18, 2022, 9:22am UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/19 "2022-07-18T09:22:14Z")

</div>

I am doing the same converting all my jsx files. but I still use some of my jsx script because they rely on keyboard shortcuts, I hope we get support for shortcuts soon in UXP.

---

<div class="post-metadata">

**Author:** ![ev9](https://avatars.discourse-cdn.com/v4/letter/e/ce73a5/32.png) [@ev9](https://forums.creativeclouddeveloper.com/u/ev9)\
**Post date:** [December 18, 2023, 7:14pm UTC](https://forums.creativeclouddeveloper.com/t/execute-a-script-via-a-button/3699/21 "2023-12-18T19:14:43Z")

</div>

Hello, I have here a script to run a pre-recorded photoshop action: I use this “RunAction.js” script, in combination with another “buttonConfig.json” file. Here is the RunAction.js script :

const { core, action } = require(“photoshop”);

async function actionCommands(actionName, actionSet) {  
console.log(`Executing actionCommands with actionName: ${actionName}, actionSet: ${actionSet}`);  
try {  
const result = await core.executeAsModal(async () =\> {

```
  return await action.batchPlay(
    [
      {
        _obj: "play",
        _target: [
          { _ref: "action", _name: actionName },
          { _ref: "actionSet", _name: actionSet }
        ],
        _options: { dialogOptions: "dontDisplay" }
      }
    ],
    {}
  );
}, { commandName: `Executing ${actionName}` });

console.log("Action executed successfully:", result);
return result;

```

} catch (err) {  
console.error(“Error in batchPlay:”, err);  
throw err;  
}  
}

module.exports = { actionCommands };

and here is the buttonConfig.json file :

{  
“Name\_of\_Button”: {  
“action”: “Name\_of\_Action”,  
“actionSet”: “Name\_of\_Set”  
}  
}
