Copy link to clipboard
Copied
Hi,
I want export layers names with unique ids to external file.I search a lot in documentation and also on Internet and I didnt find anything about unique ids of layers in photoshop. Each created artlayer should have own unique id, right? So, how to get that uid?
Greeds.
Here is how to get the layer's ID. But as Paul said the ID is only unique to the document. And the background layer always returns 0 and it's id is not unique.
function getActiveLayerId() {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
return executeActionGet(ref).getInteger( stringIDToTypeID( "layerID" ) );
}
Note I used both the string
...Copy link to clipboard
Copied
Using Mike's function will get you the layer ID's but these are only unique to that one document.
var LayerArray =[];
LayerArray = getSelectedLayersIdx();
alert(LayerArray.toString());
function getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++){
selectedLayers.push( desc.getReference( i ).getIndex());
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));
}
return selectedLayers;
}
Copy link to clipboard
Copied
Thanks. That is nice snippet. But You can specify which function / line is responsible for UID?
That codes:
'Dcmn',
'ItmI',
'Lyr ',
'Prpr', etc.
Tells me tottaly nothing : ( Where I can find reference to them?
Copy link to clipboard
Copied
More than one of those. Here is some of Mike code to get the active layers index.
Using the numeric makes the code faster as no conversion is required.
It is not possible to use one line of code to get the index.
function getActiveLayerIndex() {
var ref = new ActionReference();
ref.putProperty( 1349677170 , 1232366921 );
ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
var res = executeActionGet(ref).getInteger( 1232366921 ) /* This is the index */
- Number( hasBackground() );
return res;
}
function hasBackground(){
var res = undefined;
try{
var ref = new ActionReference();
ref.putProperty( 1349677170 , 1315774496);
ref.putIndex( 1283027488, 0 );
executeActionGet(ref).getString(1315774496 );;
res = true;
}catch(e){ res = false}
return res;
}
Copy link to clipboard
Copied
I am not tested this yet but this code get 'index' property from layers. Index is this same as unique id? Layer index isn't a position on layer list? So when I move layer - index will be changed... Please correct me if I'm wrong...
Unique ID is always this same for each layer - I can change name, position on artboard, position in layer list and I still will have this same UID.
Copy link to clipboard
Copied
Here is how to get the layer's ID. But as Paul said the ID is only unique to the document. And the background layer always returns 0 and it's id is not unique.
function getActiveLayerId() {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
return executeActionGet(ref).getInteger( stringIDToTypeID( "layerID" ) );
}
Note I used both the stringID and charID for the layerID key. You can find some information about charID and stringID in the Photoshop SDK. You can find the most up to date list I know of in Xbytor's xtools.
Copy link to clipboard
Copied
Do we really need to activate layer for this?
I noted a considerable performance hit due to activating the layer.
Suppose, I am traversing layers using a for loop.
Then I have a layer object.
How do I directly get layer ID from this layer object?
for(var layerIndex=0; layerIndex < layerNodes.length; layerIndex++)
{
var currLayer = layerNodes[layerIndex];
try{
// get layerID by "currLayer" ?
}catch(e){
}
}
Copy link to clipboard
Copied
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), layerIndex);
var layerDesc = executeActionGet(ref);
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
Copy link to clipboard
Copied
You should always ask for one key as well. Asking for all the keys is very slow if you have text on the layer for example. I would add this line ref.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID('layerID') );
Something like this:
var ref = new ActionReference();
ref.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID('layerID') );
ref.putIndex( charIDToTypeID( "Lyr " ), layerIndex);
var layerDesc = executeActionGet(ref);
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
Copy link to clipboard
Copied
Good point.
Copy link to clipboard
Copied
PITerminology.h in the Photoshop SDK has a bit more descriptive
constants but it is not much help and they are sometimes inconsistent.