Try / catch for UXP batchPlay code in Photoshop CC 2021

Is there a way to get try / catch to work with batchPlay code in UXP for Photoshop? I’ve tried it with several different batchPlay functions. In all cases I’ve tested, it doesn’t catch the error so it seems universal to batchPlay coding.

I tested with a couple of DOM items and try/catch works properly.

Here is a basic test example which doesn’t work.

var batchPlay = require("photoshop").action.batchPlay;

try{await selectLayer("Nonexistent Layer");}
catch(e){}

async function selectLayer(layerName){

await batchPlay(
[
{
"_obj": "select",
"_target": [
{
"_ref": "layer",
"_name": layerName
}
],
"_options": {
"dialogOptions": "dontDisplay"
}
}
],{
"synchronousExecution": true,
"modalBehavior": "fail"
});

return "done";

}

Since BatchPlay returns a Promise (of Descriptor Array), maybe u could chain it with .then(), .catch() or .finally() and log the results.

Thanks for the advice. Maybe I’m having a hard time wrapping my brain around it. Perhaps I’ll try again tomorrow.

In any case, I think it will work fine with the DOM so as more DOM functionality becomes available it may not even be an issue anyway.

I did this test with the DOM with async/await. If there are less than 3 layers then this is successful to catch the error.

try{await app.activeDocument.layers[2].rotate(90);}
catch(e){app.showAlert("Error caught: "+String(e));}

Just tested it, .catch() doesn’t seem to work, but .then() works and returns a descriptor including a message when there is an error:
photoshop.app.batchPlay([__selectByIndex(2)], {}).then(e => console.log(e))
image

On your system, does the Photoshop popup error message still occur? Or do you get both the console log and the Photoshop message?

Any chance that you post your selectByIndex function too so I can test it exactly as you are? I’m not able to replicate this on my system. I thought I applied it correctly but I’m still getting a Photoshop error only but nothing in the logs.

I’ve never gotten any Photoshop error popup for bad batchplayCode yet :thinking: At least as far as I remember. So I’m only seeing the error in the console, which I’m actively logging.

Here’s the function

export function __selectByIndex(_index: number): ActionDescriptor {
  return {
    _obj: 'select',
    _target: { _ref: 'layer', _index},
  }
}

You can remove the typing if you’re not using typescript

Just had a chance to test and I could repeat your results with your functions. However, when using my select by layer name function, the error would pop up in Photoshop and not appear in the descriptor in the log.

I figured out that the options key needs to be removed from the descriptor. Without the options key it will show the error in the log. With the option key, the error message pops up in Photoshop and doesn’t show in the log.

Just FYI in case anyone needs the info… This works to skip the Photoshop error message and return it using .then.

batchPlay([selectLayer("Non-existent layer")], {}).then(e => console.log(e))
function selectLayer(layerName){
    
return  {
      "_obj": "select",
      "_target": [
         {
            "_ref": "layer",
            "_name": layerName
         }
      ],
   }
   
}

However, this will show the error message in Photoshop and not return the error message in the descriptor.

batchPlay([selectLayer("Non-existent layer")], {}).then(e => console.log(e))
function selectLayer(layerName){
    
return  {
      "_obj": "select",
      "_target": [
         {
            "_ref": "layer",
            "_name": layerName
         }
      ],
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
   
}

I could do with some clarification on this please.

From a button on the plugin I am selecting a Brush Preset.
All works fine if the Brush Preset is present but if the Brush Preset is not there then obviously it fails and I get a Photoshop Error Message Box.

Ideally, I would have liked to catch this error and show my own Error Message but I dont think this is possible.

This is the code I am using to select the Brush Preset

const batchPlay = require("photoshop").action.batchPlay;

const result = await batchPlay(
[
   {
      "_obj": "select",
      "_target": [
         {
            "_ref": "brush",
            "_name": "Small Bubbles"
         }
      ],
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});

It has been suggested that removing

    "_options": {
         "dialogOptions": "dontDisplay"

does stop the Photoshop Error Box from showing and yes, I can confirm it does on my system. I am not sure what this code actually does and could it have any negative impact by removing it ?.

The dialogOptions are probably related to the DialogModes setting from Extendscript:
image
I haven’t seen anything in the API about this though. Ironically, it’s doing the exact opposite of what it’s supposed to do - “dontDisplay” should suppress dialogues as one might expect, however it actually shows them until the code gets removed. I don’t include this option in any of my Descriptors.

Anyways, you could prevent the error from occurring, if you check if the brush preset exists before selecting it. You’ll probably find it in the Application Descriptor

Thanks @simonhenke that has done the trick :+1:

That is correct. “ERROR” will show an error dialog in ExtendScript but “NO” won’t show dialog but throw an error.