Confusion about using batchplay

hello ,
Recently, I have encountered some problems when using batchplay, such as stopping at a certain step for some reason. This is the code form I use:

photoshop.action.batchPlay([
    action1, 
    action2, 
    action3, ...], {"synchronousExecution": false})

Is it executed synchronously or asynchronously? I mean the actions included in batchplay.

thanks

this is my code,Code 1 does not function properly

async function test_fun() {


    function Layer_Add(r, g, b) {

        try {

            const JSON = {
                "_obj": "make",
                "_target": [{
                    "_ref": "contentLayer"
                }],
                "using": {
                    "_obj": "contentLayer",
                    "type": {
                        "_obj": "solidColorLayer",
                        "color": {
                            "_obj": "RGBColor",
                            "red": r,
                            "grain": g,
                            "blue": b
                        }
                    }
                },
                "_isCommand": true
            }
            return JSON;

        } catch (error) {
            console.log(error);
        }


    }

    function Layer_Move_Bottom() {

        try {

            const JSON = {
                "_obj": "move",
                "_target": [{
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                }],
                "to": {
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "back"
                },
                "_isCommand": true
            }
            return JSON;

        } catch (error) {
            console.log("err:Layer_Move_Top");
        }


    }


    //code1:Unable to run code normally
    await require("photoshop").action.batchPlay([
        Layer_Add(255, 255, 255),
        Layer_Move_Bottom(),
    ], {
        "synchronousExecution": false
    });



    //code2:Able to run code normally
    await require("photoshop").action.batchPlay([
        Layer_Add(255, 255, 255),
    ], {
        "synchronousExecution": false
    });

    await require("photoshop").action.batchPlay([
        Layer_Move_Bottom(),
    ], {
        "synchronousExecution": false
    });


}

Your try/catch blocks don’t make a lot of sense where they’re at. All you do inside is return the const, there isn’t really anything that could even throw an error :thinking:

The batchPlay function however might throw, or it might also wrap errors in the return value. Check both via:

try {
    const result = await require("photoshop").action.batchPlay([
        Layer_Add(255, 255, 255),
        Layer_Move_Bottom(),
    ], {
        "synchronousExecution": false
    });
    console.log(result);
} catch(e) {
    console.log(e);
}

What happens if you change synchronousExecution to true?

1 Like

@simonhenke Thank you for your reply.

Even if synchronizousexecution is set to true, the problem still occurs. I suspect this is a batchplay bug. Now my solution is to handle the problem independently.

Before change: Code in question:

await photoshop.action.batchPlay([
    action1, 
    action2, 
    action3(err step), ...], {"synchronousExecution": false})

After change: normal operation:

await photoshop.action.batchPlay([
    action1, 
    action2, ...], {"synchronousExecution": false})

await photoshop.action.batchPlay([
    action3(err step)], {"synchronousExecution": false})

await photoshop.action.batchPlay([...], {"synchronousExecution": false})

The following works fine in PS 23.4.1, but with API version 2, you must wrap your code with executeAsModal.

First, define your descriptors – there’s no need for try/catch here, because these can’t ever throw. I’ve condensed into inline functions just to reduce some JS boilerplate.

const Layer_Add = (r, g, b) => ({
                "_obj": "make",
                "_target": [{
                    "_ref": "contentLayer"
                }],
                "using": {
                    "_obj": "contentLayer",
                    "type": {
                        "_obj": "solidColorLayer",
                        "color": {
                            "_obj": "RGBColor",
                            "red": r,
                            "grain": g,
                            "blue": b
                        }
                    }
                },
                "_isCommand": true
            });

const Layer_Move_Bottom = () => ({
                "_obj": "move",
                "_target": [{
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                }],
                "to": {
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "back"
                },
                "_isCommand": true
            });

Second, execute your batch of descriptors:

await require("photoshop").core.executeAsModal(async () => {
  try {
    // works fine, but WILL FAIL outside of executeAsModal
    const response = await require("photoshop").action.batchPlay([
        Layer_Add(255, 255, 255),
        Layer_Move_Bottom(),
    ], { "synchronousExecution": false });
    console.log(response);
  } catch (e) {
    console.error(e);
  }
}, {commandName: "Test"});

If the above is failing for you, what does the response look like on the console?

1 Like

Thanks for your help. @kerrishotts

I completely copied your code to test. Unfortunately, it still doesn’t work properly. When the selected layer is the layer at the bottom, the code is executed, and the pure color document can be created normally, but it will not be moved to the bottom.It means that the command “layer_move_bottom” has not been executed or taken effect, but no error will be reported.

My PS version is 23.3.1

This is my test video:

@heewoo @samgannaway ^^^^

This issue (executeAsModal + layer move & the lack of error reporting) was fixed in 23.4.

If you wish to stay with 23.3, you may have to split the descriptor into separate executeAsModal calls. You could then use Layer.sendToBack() instead of Layer_Move_Bottom.

1 Like

Hi,
Thank you very much for your help.

Hi @samgannaway

This fix probably caused other issues:

http://forums.creativeclouddeveloper.com/t/move-layer-to-front-through-batch-play-works-differently-in-ps-23-4-1/4814/41

1 Like

As @Adam mentioned, most likely because of that fix, I can’t create channel masks anymore, which I still can do without any issues on v23.3.2