Copy link to clipboard
Copied
I want to toggle on and off a layer by name and add it to a keypress via an action. Go me!
var srcDoc = app.activeDocument;
var layerName = "my lovely layer";
srcDoc.activeLayer = srcDoc.artLayers.getByName(layerName);
var vis = srcDoc.activeLayer.visible;
srcDoc.activeLayer.visible = !vis;
So far so good. However, I'm a completist and I want to go back to the originally selected layer before hand.
Adding this at the start
var currentLayer = srcDoc.activeLayer;
and then using that reference to go back to it :
srcDoc.activeLayer = currentLayer;
Does not work as expected. My script-fu may be weak this morning. -I get mixed results depending on what layer is initially selected and wherther it is visible or not.
Any ideas where I'm going wrong??
Answers, as always, on a postcard...
1 Correct answer
Why do you make the Layer the activeLayer to show or hide it?
var srcDoc = app.activeDocument;
var layerName = "my lovely layer";
var thisOne = srcDoc.artLayers.getByName(layerName);
var vis = thisOne.visible;
thisOne.visible = !vis;
should work just as well for top-level Layers.
Explore related tutorials & articles
Copy link to clipboard
Copied
Why do you make the Layer the activeLayer to show or hide it?
var srcDoc = app.activeDocument;
var layerName = "my lovely layer";
var thisOne = srcDoc.artLayers.getByName(layerName);
var vis = thisOne.visible;
thisOne.visible = !vis;
should work just as well for top-level Layers.
Copy link to clipboard
Copied
I'm probabaly working on a (legacy) practice that in order to perform a function on a layer you had to make it active first.
- Also running a function that toggles the layer after just toggling really confuses matters - 😄

