# Understanding synchronicity with Photoshop batchPlay

**URL:** https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422
**Category:** Photoshop
**Created:** [March 14, 2022, 4:24pm UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422 "2022-03-14T16:24:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![nikshankarnoble](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@nikshankarnoble](https://forums.creativeclouddeveloper.com/u/nikshankarnoble)
#### Post date: [March 14, 2022, 4:24pm UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422/1 "2022-03-14T16:24:39Z")

</div>

Hi!

I’m working on a Photoshop UXP plugin (API Version 2) and am trying to write a function that writes all the image layers out as PNGs to a particular folder.

The following code attempts to loop through the active document layers and export them out with batchPlay. The console output suggests that the operations are happening in the correct order, but only the bottom layer (the last element of activeDocument.layers) actually gets exported to the folder. It seems like the batchPlay commands aren’t actually being called when I think they are, perhaps I’m not understanding how to execute synchronous batchPlay commands correctly? I saw the same behaviour when not using async functions too

```auto
async function testExport() {
    try {
        const { app } = require("photoshop");

        await require("photoshop").core.executeAsModal(
            async () => {
                for (let layer of app.activeDocument.layers) {
                    // Select layer.
                    const selectAction = {
                        _obj: "select",
                        _target: [
                            {
                                _ref: "layer",
                                _id: layer.id,
                            },
                        ],
                        makeVisible: false,
                        layerID: [layer.id],
                        _isCommand: true,
                    };
                    console.log("selecting", layer.id);
                    await require("photoshop").action.batchPlay(
                        [selectAction],
                        {
                            synchronousExecution: true,
                            modalBehavior: "execute",
                        }
                    );
                    console.log("selected", layer.id);

                    // Export selected layer.
                    const exportAction = {
                        _obj: "exportSelectionAsFileTypePressed",
                        _target: {
                            _ref: "layer",
                            _id: layer.id,
                        },
                        fileType: "png",
                        quality: 32,
                        metadata: 0,
                        destFolder: destFolder,
                        sRGB: true,
                        openWindow: false,
                        _options: { dialogOptions: "dontDisplay" },
                    };
                    console.log("exporting", layer.id);
                    await require("photoshop").action.batchPlay(
                        [exportAction],
                        {
                            synchronousExecution: true,
                            modalBehavior: "execute",
                        }
                    );
                    console.log("exported", layer.id);
                }
            },
            { commandName: "Export Layers" }
        );
    } catch (err) {
        console.error(err);
    }
}                 

```

Any help would be very much appreciated!

Thanks,  
Nik

---

<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: [March 14, 2022, 5:01pm UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422/2 "2022-03-14T17:01:03Z")

</div>

What’s the _return_ result from the `batchPlay` commands? You might be getting errors there (`batchPlay` doesn’t always throw on an error).

---

<div class="post-metadata">

### Author: ![nikshankarnoble](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@nikshankarnoble](https://forums.creativeclouddeveloper.com/u/nikshankarnoble)
#### Post date: [March 15, 2022, 12:09pm UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422/3 "2022-03-15T12:09:51Z")

</div>

Hi @kerrishotts!

The batchPlay commands are returning an array of one empty object:

```auto
[
    {}
]

```

No errors are being reported to the console.

Thanks!  
Nik

---

<div class="post-metadata">

### Author: ![nikshankarnoble](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@nikshankarnoble](https://forums.creativeclouddeveloper.com/u/nikshankarnoble)
#### Post date: [March 15, 2022, 12:24pm UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422/4 "2022-03-15T12:24:28Z")

</div>

Also just to mention, when I remove the loop and just tell it to export the currently selected layer it works without issue! It’s only having issues once I put it into the loop.

---

<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: [March 15, 2022, 8:58pm UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422/5 "2022-03-15T20:58:09Z")

</div>

I’m guessing there’s more in that object – a lot of objects have properties that can’t be enumerated when you log them out, so they _look_ empty, but often are not.

Try logging this instead:

```auto
const result = await /*the rest of your command */
console.log(result.map(item => `${item._obj} ${item.message}`).join("\n"));

```

What do you get now?

---

<div class="post-metadata">

### Author: ![nikshankarnoble](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@nikshankarnoble](https://forums.creativeclouddeveloper.com/u/nikshankarnoble)
#### Post date: [March 16, 2022, 11:52am UTC](https://forums.creativeclouddeveloper.com/t/understanding-synchronicity-with-photoshop-batchplay/4422/6 "2022-03-16T11:52:50Z")

</div>

When I try that I simply receive `undefined undefined` in the console for the export command.

I tried calling the export function on layers by ID manually in the function below:

```auto
async function testExport() {
    const { executeAsModal } = require("photoshop").core;

    let result1 = await executeAsModal(
        function () {return exportLayerByID(6);},
        { commandName: "Export layer 6" }
    );

    let result2 = await executeAsModal(
        function () {return exportLayerByID(14);},
        { commandName: "Export layer 14" }
    );

    let result3 = await executeAsModal(
        function () {return exportLayerByID(15);},
        { commandName: "Export layer 15" }
    );
}

```

Commenting out two of them allows me to export any of the layers without issue, but once I run all three of them in the function only two are exported… not sure what’s going on.
