Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
6

Get originally selected layer

Engaged ,
Mar 18, 2024 Mar 18, 2024

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

 

 

TOPICS
Actions and scripting , Windows

Views

191
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 18, 2024 Mar 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. 

Votes

Translate
Community Expert ,
Mar 18, 2024 Mar 18, 2024

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. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 18, 2024 Mar 18, 2024

Copy link to clipboard

Copied

LATEST

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

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines