How To Get BatchPlay To Work With API 2

You need to wrap your batchPlay call in a try/catch block:

const batchplayError = async () => {
  const { core } = require("photoshop");
  const batchPlay = require("photoshop").action.batchPlay;

  return core.executeAsModal(async () => {
    try {
      return await batchPlay(
        [   
          // ...actionDescriptors array
        ]
        // {} Second argument commented out
      );
    } catch (error) {
      console.log(error);
    }
  });
};

Screenshot 2023-02-25 193717

I guess try blocks don’t catch every error inside them in javascript? I was using a function that wraps executeAsModel to catch any errors and then passing the async function that contains the batchplay call into the error-catching function. I’m almost positive that in python, for example, any exception hit in the block would be caught.

But that does not appear to be the case in this language, because as soon as I used your version, which only wraps the batchplay call, I finally got some logs.

Thanks.

You would be correct in thinking that any exception thrown within a try block should be passed to the catch:

“If any statement within the try-block (or in a function called from within the try-block) throws an exception, control is immediately shifted to the catch-block.” ~ MDN try/catch article

I’ve encountered this behaviour in UXP before, but I never got around to testing further. If memory serves correct it seemed like executeAsModal blocks it. I’m now just in the habit of always wrapping any batchPlay calls directly!

1 Like