Skip to main content
Inspiring
March 18, 2024
Answered

Get originally selected layer

  • March 18, 2024
  • 1 reply
  • 279 views

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...

 

 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

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. 

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
March 18, 2024

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. 

Inspiring
March 18, 2024

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 - 😄