Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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);
}
};
Copy link to clipboard
Copied
Note that layer ids are valid for a given PS session even if you move the layer. They are not persistent across PS sessions.
Copy link to clipboard
Copied
That's strange X as I have used this in a few scripts and it has not been restricted to a single session.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
The only time it seems to fall over is when the backgound layer is made floating then accessing that layer is hit and miss.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 = ...
Copy link to clipboard
Copied
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.