# Confusion about using batchplay

**URL:** https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842
**Category:** Photoshop
**Created:** [June 22, 2022, 5:48pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842 "2022-06-22T17:48:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![MrL](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@MrL](https://forums.creativeclouddeveloper.com/u/MrL)
#### Post date: [June 22, 2022, 5:48pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/1 "2022-06-22T17:48:02Z")

</div>

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:

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

```

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

thanks

---

<div class="post-metadata">

### Author: ![MrL](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@MrL](https://forums.creativeclouddeveloper.com/u/MrL)
#### Post date: [June 22, 2022, 6:03pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/2 "2022-06-22T18:03:46Z")

</div>

this is my code,Code 1 does not function properly

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

}

```

---

<div class="post-metadata">

### Author: ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)
#### Post date: [June 22, 2022, 8:55pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/3 "2022-06-22T20:55:11Z")

</div>

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 🤔

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

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

---

<div class="post-metadata">

### Author: ![MrL](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@MrL](https://forums.creativeclouddeveloper.com/u/MrL)
#### Post date: [June 23, 2022, 4:58am UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/4 "2022-06-23T04:58:57Z")

</div>

@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:**

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

```

**After change: normal operation:**

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

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

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

```

---

<div class="post-metadata">

### Author: ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)
#### Post date: [June 23, 2022, 4:38pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/5 "2022-06-23T16:38:39Z")

</div>

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.

```javascript
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:

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

---

<div class="post-metadata">

### Author: ![MrL](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@MrL](https://forums.creativeclouddeveloper.com/u/MrL)
#### Post date: [June 23, 2022, 5:24pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/6 "2022-06-23T17:24:40Z")

</div>

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:

---

<div class="post-metadata">

### Author: ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)
#### Post date: [June 23, 2022, 7:25pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/7 "2022-06-23T19:25:26Z")

</div>

@heewoo @samgannaway ^^^^

---

<div class="post-metadata">

### Author: ![samgannaway](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@samgannaway](https://forums.creativeclouddeveloper.com/u/samgannaway)
#### Post date: [June 24, 2022, 8:08pm UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/8 "2022-06-24T20:08:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![MrL](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@MrL](https://forums.creativeclouddeveloper.com/u/MrL)
#### Post date: [June 25, 2022, 4:53am UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/9 "2022-06-25T04:53:43Z")

</div>

Hi,  
Thank you very much for your help.

---

<div class="post-metadata">

### Author: ![Adam](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/adam/32/1808_2.png) [@Adam](https://forums.creativeclouddeveloper.com/u/Adam)
#### Post date: [June 25, 2022, 7:28am UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/10 "2022-06-25T07:28:19Z")

</div>

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](http://forums.creativeclouddeveloper.com/t/move-layer-to-front-through-batch-play-works-differently-in-ps-23-4-1/4814/41)

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [June 25, 2022, 8:50am UTC](https://forums.creativeclouddeveloper.com/t/confusion-about-using-batchplay/4842/11 "2022-06-25T08:50:13Z")

</div>

As @Adam mentioned, most likely because of that fix, [I can’t create channel masks](http://forums.creativeclouddeveloper.com/t/move-layer-to-front-through-batch-play-works-differently-in-ps-23-4-1/4814/21) anymore, which I still can do without any issues on v23.3.2
