get index of selected layer

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

lukasz

get index of selected layer

Post by lukasz »

for some reason I cannot get the index of the selected layer. having a brain fart.

trying to turn off visibility of selected layer then turn on visibility next layer and have next layer also be active layer.

Code: Select allvar currentLayer = app.activeDocument.activeLayer.length;
var activelayer = app.activeDocument.activeLayer;
activelayer.visible = false;
var nextLayer = currentLayer-1;
nextLayer.visible = true;
app.activeDocument.activeLayer = nextLayer;

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

txuku

get index of selected layer

Post by txuku »

Bonjour lukasz

I think this corresponds to what you seek :

Code: Select allvar doc = app.activeDocument;
var currentLayer = doc.activeLayer;
   
for(i=0; i < doc.layers.length; )
{         
  if(doc.layers==currentLayer)
  {
      a=i;
      //alert(a);
      i = doc.layers.length;
  }
  else{ i++; }
}

currentLayer.visible = false;
try
{
  var nextLayer = doc.layers[a-1];
}
catch(e)
{
  var nextLayer = doc.layers[doc.layers.length-1];

nextLayer.visible = true;
doc.activeLayer = nextLayer;
lukasz

get index of selected layer

Post by lukasz »

that worked perfect. one more issue though.
documentation says index is method of layers, but I can't seem to select via the index. any idea whats going on?

Code: Select allvar newActivelayer = doc.layers.index(1);
larsen67

get index of selected layer

Post by larsen67 »

Not like that…

Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[1];

Index inside of a square bracket pair…
lukasz

get index of selected layer

Post by lukasz »

doh, ok works get thanks!
lukasz

get index of selected layer

Post by lukasz »

still a little hiccup. doesnt seem to want to select next layer, however it does make it visible? which seems weird because it makes it visible after it selects the layer in the code, or should? but in the document the next layer is not selected.

Code: Select all#target illustrator

var activelayer = app.activeDocument.activeLayer;
activelayer.visible = false;

for(i=0; i < doc.layers.length; )
{
  if(doc.layers==activelayer)
  {
      var a = i;
      alert(a);
      var i = doc.layers.length;
      var b = a+1;
  }
  else{ i++; }
}

app.activeDocument.activeLayer = app.activeDocument.layers;
var activelayer = app.activeDocument.activeLayer;
activelayer.visible = true;
larsen67

get index of selected layer

Post by larsen67 »

#target illustrator

Sorry but you can't expect the syntax of Photoshop to work with Illustrator… While there are common objects like app & doc little else will work… In PS layers can exist inside a doc or layer set… In Illustrator they can exist anywhere… other layers, groups, etc… this needs to be scripted as per the Illustrator DOM…
lukasz

get index of selected layer

Post by lukasz »

hmmm, interesting. I understand not all of it will work. but usually the core dom is the same, they do handle layers differently though. i will look into that.
Mike Hale

get index of selected layer

Post by Mike Hale »

lukasz wrote:but usually the core dom is the same

It has been my experience that the core javascript is the same, but the DOM's for each Adobe app have little if anything in common.
larsen67

get index of selected layer

Post by larsen67 »

Try getting the name of your active layer… You will see it's the one you wanted it's just a lazy UI that does not redraw as you would expect… Even app.redraw() will not refresh this for you…