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

How to get the number/id of layer?

Guest
May 06, 2009 May 06, 2009

Tried searching the forums, but found only inDesign code 8(

So the question is: how can I get number of layer (defined by name, or current layer)?

Thanks in advance.

TOPICS
Actions and scripting
3.6K
Translate
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

Valorous Hero , May 07, 2009 May 07, 2009

If there is no layer sets you could use something like this.

This will tell you how many Layers with the name 1 and then select the last one.

var layerOne=[];
var doc = activeDocument;

for(var a =0;a<doc.artLayers.length;a++){
if(doc.layers.name == '1') layerOne.push(a);
}
alert("Number of layer one's = " +layerOne.length+" Layer numbers = "+layerOne.toString() );
activeDocument.activeLayer=doc.layers[parseInt(layerOne[layerOne.length-1])]; //select last layer 1

Translate
Adobe
Valorous Hero ,
May 06, 2009 May 06, 2009

It depends on the index you are looking for?

Here is one method...( functions by Mike Hale)

//alert(getLayerId());
//selLayer(24);

function getLayerId(){
//Assumes activeDocument and activeLayer
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    d = executeActionGet(ref);
return d.getInteger(charIDToTypeID('LyrI'));
};

function selLayer(layerID,add){
var result =getLayerItemIndexByLayerID(layerID);
if(result != true){
selectLayerByIndex(result -1,add);
}else{
alert("Layer does not exist"); 
  }
};
function getLayerItemIndexByLayerID(id) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
try{
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
}catch(e){
   return true;
  }
};
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};

Translate
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
Advisor ,
May 06, 2009 May 06, 2009

Note that layer ids are valid for a given PS session even if you move the layer. They are not persistent across PS sessions.

Translate
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
Valorous Hero ,
May 06, 2009 May 06, 2009

That's strange X as I have used this in a few scripts and it has not been restricted to a single session.

Translate
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
Advisor ,
May 06, 2009 May 06, 2009

Add 20 layers to a blank document.

Record the ids for each layer.

Save it.

Reopen it.

Delete some and reorder the rest.

Record the ids.

Unless things have changed and they are now using UUIDs for for layer ids, you should see a difference.

The addition of layer-level metadata in CS4, however, provides a convenient way to get around this problem.

-X

Translate
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
Valorous Hero ,
May 06, 2009 May 06, 2009

Ah, I didn't come accross that problem as no changes to the layers were made. It was in a graphic design enviroment to add designer/author/date etc. information that was then embeded into the current graphic. So it was a complex template and much easier to access with the layer Ids.

Thank for the heads up X.

Translate
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
Guru ,
May 07, 2009 May 07, 2009

Hi X,

I know that you have posted similar finding on the PS-Scripts forum, but like Paul it's been my findings that a layer's id is persistent.

As long as a layer is in the doc it's id stays the same no matter how many other layers have been added, deleted, or moved. Or how many time the doc has been closed and reopened. And if that layer is deleted no other layer added later will have the same id as the deleted one.

The only drawback I remember about using layerID is it is harder to use than Index or Name.

Mike

Translate
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
Advisor ,
May 07, 2009 May 07, 2009

Right you are, Mike. I was given this info from somebody at Adobe back in PS7/CS days (I think) and took it as gospel. Either the information was incorrect or somewhere along the way they made the layerids persistent.

-X

Translate
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
Valorous Hero ,
May 07, 2009 May 07, 2009

The only time it seems to fall over is when the backgound layer is made floating then accessing that layer is hit and miss.

Translate
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
Guest
May 07, 2009 May 07, 2009


Wow, complex stuff. I'll find use for it, thanks.
But right now I was looking for something different. Probably I explained it bad.

There is some files(pretty crappy organized) that I need to process. Each has a few layers with same names("1"). Thank god all these files have something in common -  layer that I need supposed to be visible(yes, sometimes there is 3 and more layers "1") and always goes after the trash layer.
So I wanted to compare found "1"s by their number. I mean order in layer list.

At the moment, I did all the work with monstrous action, that selects layer "1", renames it to "1base" and then selects "1" again.  Though,
I want something more elegant. Since work is done, need it more for educational reasons.
I always can make something the old way - by hand - but want to learn PS scripting. It is so facinatinig.

EDIT: Me being stupid and overhelmed with functions 8) There is enough info for answer my question.

Translate
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
Valorous Hero ,
May 07, 2009 May 07, 2009

If there is no layer sets you could use something like this.

This will tell you how many Layers with the name 1 and then select the last one.

var layerOne=[];
var doc = activeDocument;

for(var a =0;a<doc.artLayers.length;a++){
if(doc.layers.name == '1') layerOne.push(a);
}
alert("Number of layer one's = " +layerOne.length+" Layer numbers = ...

Translate
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
Guest
May 07, 2009 May 07, 2009
LATEST

Many thanks. Works like a charm.

OFFTOP:

Can't understand PS ways... Why index 0 is upper layer? Appears to me that Background or bottom layer being 0 is more logical/intuitive.

Translate
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