Hi,
I am trying to generate a function which will merge all visible layers.
I used alchemist and generates the function as shown below.
Before I use my “mergeVisible()” I must make sure that the current selected layer is an artLayer(pixes, smartobject), therefore I created another function to select any visible pixel or smartLayer.
The general structure for is this:
const { app, core, action } = require("photoshop");
async function myMergeVisible(){
await selectVisibleArtLayer();
await mergeVisible();
}
The problem is that the second function, mergeVisible starts before the first function, selectVisibleArtLayer() completes.
When I terminate the program after selectVisibleArtLayer(), the correct layer is selected.
When I let it run, it is not yet selected when mergeVisible() is reached.
It is selected, later on.
Why is that?
What should I do make sure the layer is selected prior to continue the program flow?
async function selectLayerById(layerId, isFirst = true) {
await require("photoshop").core.executeAsModal(async (executionControl, descriptor) => {
if (isFirst) {
const result = await batchPlay(
[
{
_obj: "select",
_target: [
{
_ref: "layer",
_id: layerId
}
],
makeVisible: false,
layerID: [
layerId
],
_options: {
dialogOptions: "dontDisplay"
}
}
], {
synchronousExecution: false,
modalBehavior: "execute"
});
}
else {
const result = await batchPlay(
[
{
_obj: "select",
_target: [
{
_ref: "layer",
_id: layerId
}
],
makeVisible: false,
"selectionModifier": { "_enum": "selectionModifierType", "_value": "addToSelection" },
layerID: [
layerId
],
_options: {
dialogOptions: "dontDisplay"
}
}
], {
synchronousExecution: false,
modalBehavior: "execute"
});
}
//
}, {
"commandName": "selectLayerById"
});
}
async function mergeVisible() {
//smartObject
//pixel
//make sure an art layer is selected.
//this command will not work if a filter layer is selected
await require("photoshop").core.executeAsModal(async (executionControl, descriptor) => {
const batchPlay = require("photoshop").action.batchPlay;
const result = await batchPlay(
[
{
_obj: "mergeVisible",
_options: {
dialogOptions: "dontDisplay"
}
}
], {
synchronousExecution: false,
modalBehavior: "execute"
});
}, {
"commandName": "mergeVisible"
});
}