Hi @Karmalakas,
Thank you for your reply. My Issue is quite complex but I want to do simple thing - no matter how many layers or groups I have and no matter what is selected, I want to create the new layer on top of everything to do more stuff on it later (via batchPlay).
Let’s assume that Layer 1 was selected before running the function, I would like to create the layer in the position shown on the screenshoot.
Thanks to you and @Wanokuni, I’ve managed to merge everything within one history step.
Now I just can’t find a good way to place the layer where it should be in some cases. (especially when I have many groups).
I decided to create layer and move it to the top via DOM but I’m experiencing some strange behaviour.
Ive recorded what’s happening.
This was the first step:
document.getElementById("btndustAndScratches").addEventListener("click", async function () {
await ExecuteAsModal(DaS);
});
async function DaS(executionContext) {
let hostControl = executionContext.hostControl;
let suspensionID = await hostControl.suspendHistory({
documentID: app.activeDocument?.id,
name: "Dust And Scratches",
});
const docexists = () => {
return Boolean(app.documents?.length);
};
const exists = docexists();
if (exists === true) {
const newLayer = await doc.createLayer({name: "DustAndScratches"}) // I have no idea why the layer is being moved as shown on the video
} else {
PhotoshopCore.showAlert({ message: "Open the document first" });
}
await hostControl.resumeHistory(suspensionID);
}
this is what’s happening:
I assume that the layer should be just created and not moved. I don’t know what’s happening.
Then I was trying to move the layer as explained by @Pierre_G here
document.getElementById("btndustAndScratches").addEventListener("click", async function () {
await ExecuteAsModal(DaS);
});
async function DaS(executionContext) {
let hostControl = executionContext.hostControl;
let suspensionID = await hostControl.suspendHistory({
documentID: app.activeDocument?.id,
name: "Dust And Scratches",
});
const docexists = () => {
return Boolean(app.documents?.length);
};
const exists = docexists();
if (exists === true) {
const newLayer = await doc.createLayer({name: "DustAndScratches"}) // I have no idea why the layer is being moved as shown on the video
// THIS IS WHAT I'VE ADDED
const layers = doc.activeLayers; // should I add await? .layers or .activeLayers?
const SelectedLayer = layers[0]; // should I add await?
SelectedLayer.moveAbove(doc.layerTree[0]);
//
} else {
PhotoshopCore.showAlert({ message: "Open the document first" });
}
await hostControl.resumeHistory(suspensionID);
}
All I’m getting is a weird loop, there is no history for that in PS:
And this is the whole code (that doesn’t work):
/////////////////////////////////////////////////////////////////////////////////////
///////// GLOBAL CONSTS /////////
/////////////////////////////////////////////////////////////////////////////////////
const app = require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const core = require("photoshop").core;
const PhotoshopCore = require('photoshop').core;
const doc = app.activeDocument;
const ExecuteAsModal = require("photoshop").core.executeAsModal;
// FUNCTION
document.getElementById("btndustAndScratches").addEventListener("click", async function () {
await ExecuteAsModal(DaS);
});
async function DaS(executionContext) {
let hostControl = executionContext.hostControl;
let suspensionID = await hostControl.suspendHistory({
documentID: app.activeDocument?.id,
name: "Dust And Scratches",
});
const docexists = () => {
return Boolean(app.documents?.length);
};
const exists = docexists();
if (exists === true) {
const newLayer = await doc.createLayer({name: "DustAndScratches"}) // I have no idea why the layer is being moved as shown on the video
const layers = doc.activeLayers; // should I add await? .layers or .activeLayers?
const SelectedLayer = layers[0]; // should I add await?
SelectedLayer.moveAbove(doc.layerTree[0]);
await batchPlay(
[
{
_obj: "mergeVisible",
duplicate: true,
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "select",
_target: {
_ref: "menuItemClass",
_enum: "menuItemType",
_value: "view200Percent"
},
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "dustAndScratches",
_options: {
dialogOptions: "display"
}
},
{
_obj: "make",
new: {
_class: "channel"
},
at: {
_ref: "channel",
_enum: "channel",
_value: "mask"
},
using: {
_enum: "userMaskEnabled",
_value: "revealAll"
},
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "invert",
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "select",
_target: {
_ref: "channel",
_enum: "channel",
_value: "mask"
},
makeVisible: false,
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "select",
_target: {
_ref: "paintbrushTool"
},
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "reset",
_target: {
_ref: "color",
_property: "colors"
},
_options: {
dialogOptions: "dontDisplay"
}
},
{
_obj: "select",
_target: {
_ref: "menuItemClass",
_enum: "menuItemType",
_value: "fitOnScreen"
},
_options: {
dialogOptions: "dontDisplay"
}
}
], {});
} else {
PhotoshopCore.showAlert({ message: "Open the document first" });
}
await hostControl.resumeHistory(suspensionID);
}
I have no Idea what I’m doing wrong, probably many things, I feel stupid.