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
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