Skip to main content
Inspiring
August 12, 2013
Pregunta

How to set and get key/ID to a particular layer.

  • August 12, 2013
  • 1 respuesta
  • 1212 visualizaciones

Hi all,

I have many layerSets and each layer set contains two art layers, I want to provide/set the unique IDs/key to all these layers such that each art layer of a particular layerset have same ID.

After this, I have to randomly move the art layers from one layer set to another and find the pairs of art layers. For this, I need to get the unique IDs/Key back.

So, Is there any way to set and get the unique ID/Key to a layer or layer set?

Thanks In advance,

_VG.

Este tema ha sido cerrado para respuestas.

1 respuesta

Inspiring
August 12, 2013

You can not set it but Photoshop already has a unique ID of each layer in a document.

var ref = new ActionReference();

ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));

ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

executeActionGet(ref).getInteger(charIDToTypeID( "LyrI" ));

The exception is the background layer. It will always be 0 if there is only a background layer and 1 if there are other layers.

_VGAutor
Inspiring
August 12, 2013

Thanks for the reply Michael..

If we have the unique ID of a layer then how do we retreive the layer back?

Inspiring
August 12, 2013

If the layer names are also unique you can get the layer name.

function getLayerNameByLayerID(id) {

    var ref = new ActionReference();

    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));

    ref.putIdentifier( charIDToTypeID( "Lyr " ), id );

    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;

}

If the names are not unique you could make the layer active by ID

function makeActiveByID( id ){

    try{

        var desc = new ActionDescriptor();

            var ref = new ActionReference();

            ref.putIdentifier( charIDToTypeID( "Lyr " ), id );

        desc.putReference( charIDToTypeID( "null" ), ref );

        desc.putBoolean( charIDToTypeID( "MkVs" ), false );

        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

        return activeDocument.activeLayer;

    }catch(e){}

}