Enable and disable Layer!

welcome everybody.
I have the code but it is not working properly
Can you share a solution to the problem?

Code:

const showLayer = async (_show, _id) => {
    try {
        const ExecuteAsModal = require("photoshop").core.executeAsModal;
        const LAYER = app.activeDocument.layers[0];
        const batchPlay = require("photoshop").action.batchPlay(
            [{
                _obj: _show ? "show" : "hide",
                _target: {
                    _ref: "layer",
                    _id: _id
                }
            }], {}

        );

    } catch (e) {
        console.error(e.message);
    }
}

Regards :pray:
Mohamed Fathy…

you don’t need batchPlay for this.

const ExecuteAsModal = require("photoshop").core.executeAsModal;
let layer = app.activeDocument.layers[0];

ExecuteAsModal( () => {
    layer.visible = false;
},{})
1 Like

What’s the error? Did you check the result?

There is nothing wrong with the console.
this result:

But when I press the button, it does not respond.

the const batchPlay in your code will hold the promise of the result.

try editing your code as follows

let result = await require("photoshop").action.batchPlay(
            [{
.....

and finally:

console.log(result)

Make it await for BP and see what’s the response for it

const batchPlay = await require("photoshop").action.batchPlay(/*.... */);

The code you wrote does not work when the button is pressed
Can you help me how to make the response.

const ExecuteAsModal = require("photoshop").core.executeAsModal;
let layer = app.activeDocument.layers[0];

ExecuteAsModal( () => {
    layer.visible = false;
},{})

Regards :pray:
Mohamed Fathy…

This is the complete code:
Please look at it and help me with the solution to make the button respond.


document.getElementById("lockRatioCb").addEventListener("click", async function() {
    await ExecuteAsModal(showLayer)
})
const showLayer = async (_show, _id) => {
    try {
        let LAYER = app.activeDocument.layers[0]; //
        let result = await require("photoshop").action.batchPlay(
            [{
                _obj: _show ? "show" : "hide",
                _target: {
                    _ref: "layer",
                    _id: _id
                }
            }], {}
        );
    } catch (e) {
        console.error(e.message);
        console.log(result)
    }
}

Regards :pray:
Mohamed Fathy…

This should go right after you get the result and not in the catch block

I modified the code but to no avail to find a solution
The complete code here it is.

document.getElementById("lockRatioCb").addEventListener("click", async function() {
    const LAYER = app.activeDocument.layers[0];
    await showLayer(true, LAYER.id);
})
const showLayer = async (_show, _id) => {
    try {
        const activeLayers = app.activeDocument.activeLayers;
        const ExecuteAsModal = require("photoshop").core.executeAsModal;
        await batchPlay(
            [{
                _obj: _show ? "show" : "hide",
                _target: {
                    _ref: "layer",
                    _id: _id
                }
            }], {}
        );
    } catch (e) {
        console.error(e.message);
    }
}

Regards :pray:
Mohamed Fathy…

So why did you remove the result log completely? Add it back to the main block and see what console shows you.

Hi Karmalakas.
This is the result of the console.


Also, this code is complete, in addition to the one that I updated, which is the result.

document.getElementById("lockRatioCb").addEventListener('click', async (evt) => {
    await ExecuteAsModal(showLayer)
})
const showLayer = async (_show, _id) => {
    try {
        let LAYER = app.activeDocument.layers[0]; //
        let result = await require("photoshop").action.batchPlay(
            [{
                _obj: _show ? "show" : "hide",
                _target: {
                    _ref: "layer",
                    _id: _id
                }
            }], {}
        );
        console.log(result);
    } catch (e) {
        console.error(e.message);

    }
}

But the code is not working
I hope that you also try it and test it, oh, if possible, please.

Regards :pray:
Mohamed Fathy…

You need to run showLayer() as a function and not just try to output the function itself as a showLayer

i do it , but stell not work…
the full code:

document.getElementById("lockRatioCb").addEventListener('click', async (evt) => {
    await ExecuteAsModal(MAX)
})
//doIt async(MAX)...
const MAX = async () => {
    let documentID = await app.activeDocument.layers._id;
    let LAYER = app.activeDocument.layers[0]; //
    await showLayer(true, LAYER.id); //WORK...
}; //Finally its not worked...
const showLayer = async (_show, _id) => {
    try {
        let result = await require("photoshop").action.batchPlay(
            [{
                _obj: _show ? "show" : "hide",
                _target: {
                    _ref: "layer",
                    _id: _id
                }
            }], {}
        );
        console.log(result);
    } catch (e) {
        console.error(e.message);

    }
}

Do you have a suggestion about that?

Regards :pray:
Mohamed Fathy…

So what do you get in console when you click that lockRatioCb button? I think you should get some error on this line

let documentID = await app.activeDocument.layers._id;

I did the modification as you told me.
the code:

document.getElementById("lockRatioCb").addEventListener('click', async (evt) => {
    await ExecuteAsModal(MAX)
})
//doIt async(FUCK)...
const MAX = async () => {
    let documentID = await app.activeDocument._id;
    let LAYER = app.activeDocument.layers[0]; //
    await showLayer(true, LAYER.id); //WORK...
}; //Finally its not worked...
const showLayer = async (_show, _id) => {
    try {
        let result = await require("photoshop").action.batchPlay(
            [{
                _obj: _show ? "show" : "hide",
                _target: {
                    _ref: "layer",
                    _id: _id
                }
            }], {}
        );
        console.log(result);
    } catch (e) {
        console.error(e.message);

    }
}

Console result for button + operations:

Please take a look at it. I appreciate it for helping me find a solution to my problem.
But I don’t know what’s the problem my friend yet?

Regards :pray:
Mohamed Fathy…

This still doesn’t show the console output when you click the checkbox. What I see here, that you just enter variable names into console and press Enter. Also checkbox should have a change listener and not click

console output(when I click the checkbox):

[{…}]
0:
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
at: ƒ at()
concat: ƒ concat()
arguments: (...)
caller: (...)
length: 1
name: "concat"
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[0]
constructor: ƒ Array()
copyWithin: ƒ copyWithin()
entries: ƒ entries()
every: ƒ every()
fill: ƒ fill()
filter: ƒ filter()
find: ƒ find()
findIndex: ƒ findIndex()
flat: ƒ flat()
flatMap: ƒ flatMap()
forEach: ƒ forEach()
includes: ƒ includes()
indexOf: ƒ indexOf()
join: ƒ join()
keys: ƒ keys()
lastIndexOf: ƒ lastIndexOf()
length: 0
map: ƒ map()
pop: ƒ pop()
push: ƒ push()
reduce: ƒ reduce()
reduceRight: ƒ reduceRight()
reverse: ƒ reverse()
shift: ƒ shift()
slice: ƒ slice()
some: ƒ some()
sort: ƒ sort()
splice: ƒ splice()
toLocaleString: ƒ toLocaleString()
arguments: (...)
caller: (...)
length: 0
name: "toLocaleString"
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[0]
toString: ƒ toString()
arguments: (...)
caller: (...)
length: 0
name: "toString"
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[0]
unshift: ƒ unshift()
values: ƒ values()
Symbol(Symbol.iterator): ƒ values()
Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}
[[Prototype]]: Object



Regards :pray:
Mohamed Fathy…

Could you make a screencast? This copied output doesn’t make sense without proper formatting. But from what I see, it seems like BP got executed

I just tried the code and it works fine. What do you mean by It’s not working? What do you expect it to do and what it actually does?