Skip to main content
March 15, 2011
Answered

Artlayers unique ID... (CS5)

  • March 15, 2011
  • 1 reply
  • 4286 views

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.

This topic has been closed for replies.
Correct answer Michael_L_Hale

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.


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.

1 reply

Paul Riggott
Inspiring
March 15, 2011

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;
}

March 15, 2011

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?

Paul Riggott
Inspiring
March 15, 2011

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;
}