Dear community,
it seems I am still a bit shakey on executing modal functions.
I am working on a plugin, which, in the end, shall add either an image or a solid color layer as background.
Adding the image background works fine, however the solid color throws the following error:
I have a button that triggers the whole thing:
document.getElementById("btnCropBR").addEventListener('click', async function(event) {
event.stopPropagation();
main(event, {
"direction": ["bottom", "right", "border"]
});
});
This triggers my main function, where other stuff is done to initialize the image, too:
async function main(executionContext, passedVars) {
await processImage();
}
Then the image gets processed ( a fixed border is cropped around the content, afterwards the new background should be added):
async function processImage() {
await addPickedBGLayerStyle();
}
In the end, depending on a setting made before clicking the button, the functions for adding the chosen background style are being called:
async function addPickedBGLayerStyle() {
switch (document.body.getAttribute("data-color")) {
case "color":
try {
await executeAsModal(addSolidColorBGLayer(255, 255, 255), {
commandName: "addSolidColorBGLayer"
});
} catch (error) {
console.error("Error in addSolidColorBGLayer:", error);
}
await executeAsModal(sendToBack, {
commandName: "sendToBack"
});
break;
case "grey":
await executeAsModal(addGreyGradientBGLayer, {
commandName: "addGreyGradientBGLayer"
});
await executeAsModal(sendToBack, {
commandName: "sendToBack"
});
break;
}
}
These are the batchplay calls for adding each:
async function addGreyGradientBGLayer() {
const result = await batchPlay(
[{
_obj: "placeEvent",
null: {
_path: await tokenify("VALID PATH TO A JPG"),
_kind: "local"
},
freeTransformCenterState: {
_enum: "quadCenterState",
_value: "QCSAverage"
},
offset: {
_obj: "offset",
horizontal: {
_unit: "pixelsUnit",
_value: 0
},
vertical: {
_unit: "pixelsUnit",
_value: 0
}
},
_options: {
dialogOptions: "dontDisplay"
}
}], {}
);
}
async function addSolidColorBGLayer(rgbRed, rgbGreen, rgbBlue) {
console.log("rgbRed: " + rgbRed + " rgbGreen: " + rgbGreen + " rgbBlue: " + rgbBlue);
try {
const result = await batchPlay(
[{
_obj: "make",
_target: [{
_ref: "contentLayer"
}],
using: {
_obj: "solidColorLayer",
color: {
_obj: "RGBColor",
red: rgbRed, //255,
grain: rgbGreen, //255,
blue: rgbBlue //255
}
},
_options: {
dialogOptions: "dontDisplay"
}
}], {
//"synchronousExecution": false,
//"modalBehavior": "fail"
}
);
} catch (e) {
console.log(e);
}
console.log("addSolidColorBGLayer done");
}
As you can see, I am calling both, “addSolidColorBGLayer” and “addGreyGradientBGLayer” the same way and even explicitly as modal.
But in the case of the “addSolidColorBGLayer”, I still get the error that it needs to be modal.
Where am I going wrong? Thanks for reading!