Error: "The command failed because Photoshop is in a modal state" - need help

My plugin that used to work just fine started giving me a random error when calling batchPlay():

“The command failed because Photoshop is in a modal state”.

I haven’t used the plugin in the last few months, so am not sure if Photoshop update broke something or if it’s a race condition that revealed a preexisting defect.

My plugin does two basic things, each of which has its own button and underlying JS function:

AddBorder to all open images.
Save all open images.

Both features work conceptually the same way:

Iterate through app.documents and set app.activeDocument = app.documents[i];
Get values from app.activeDocument
Set and Get UI element values using document.getElementById();
Perform some calculations
Execute various batchPlay commands.

The error occurs in both functions upon calling a batchPlay. It happens randomly. E.g. I may have 10 images opened and sometimes processing of all images succeeds, sometimes it breaks on a random image.

I’m still debugging through it, but I wonder if I’m violating some key concepts that would cause the stated error message.

I’m not ready to post any code here just yet as I would have to simplify it quit a bit to make it useful.

Thank you.

Are you using API version 1? If so, then try using the batchPlay option “modalBehavior”: “wait”. I had a similar issue with several of my plugins and one of the Adobe engineers helped me to solve it this way.

With API 2, this won’t be needed and you will want to let the option be default or “execute”.

Not 100% certain your issue is the same as mine was but it sounds like it may be.

Here is an example of how to implement it.

async function getSelectionBounds(){
    
const result = await batchPlay(
[
   {
      "_obj": "get",
      "_target": [
         {
            "_property": "selection"
         },
        {
            "_ref": "document",
            "_enum": "ordinal",
            "_value": "targetEnum"
        }
      ],
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "wait"
});


var left = result[0].selection.left._value;
var top = result[0].selection.top._value; 
var right = result[0].selection.right._value; 
var bottom = result[0].selection.bottom._value;  
    
return [left,top,right,bottom];    
    
}
1 Like

This fixed it. Thank you for the advice.