Skip to content Skip to sidebar Skip to footer

Getting Selected Layer Or Group Layers Array Using Javascript (photoshop Cs4)

K basically I need to write a photoshop script that iterates through the selected layers and retrieve some information about them. if I can get an array of the selected layers I wi

Solution 1:

Bravo's code works except it fails with an error when no layers are selected, so I tweaked it to remove the error and simply return an empty array:

function getSelectedLayers(){
  var resultLayers=newArray();
  try{
    varidGrp= stringIDToTypeID( "groupLayersEvent" );
    vardescGrp=newActionDescriptor();
    varrefGrp=newActionReference();
    refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
    descGrp.putReference(charIDToTypeID( "null" ), refGrp );
    executeAction( idGrp, descGrp, DialogModes.NO );
    for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}
    varid8= charIDToTypeID( "slct" );
    vardesc5=newActionDescriptor();
    varid9= charIDToTypeID( "null" );
    varref2=newActionReference();
    varid10= charIDToTypeID( "HstS" );
    varid11= charIDToTypeID( "Ordn" );
    varid12= charIDToTypeID( "Prvs" );
    ref2.putEnumerated( id10, id11, id12 );
    desc5.putReference( id9, ref2 );
    executeAction( id8, desc5, DialogModes.NO );
  } catch (err) { }
  return resultLayers;
}   
$.writeln(getSelectedLayers());`

Simply wrapping the code in a try/catch block didnt work, so I also changed:

executeAction( idGrp, descGrp, DialogModes.ALL );

to

executeAction( idGrp, descGrp, DialogModes.NO );

and that made the runtime error go away.

Solution 2:

This will get you the selected layers

 function getSelectedLayers(){
        varidGrp= stringIDToTypeID( "groupLayersEvent" );
        vardescGrp=newActionDescriptor();
        varrefGrp=newActionReference();
        refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
        descGrp.putReference(charIDToTypeID( "null" ), refGrp );
        executeAction( idGrp, descGrp, DialogModes.ALL );
        var resultLayers=newArray();
        for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}
        varid8= charIDToTypeID( "slct" );
            vardesc5=newActionDescriptor();
            varid9= charIDToTypeID( "null" );
            varref2=newActionReference();
            varid10= charIDToTypeID( "HstS" );
            varid11= charIDToTypeID( "Ordn" );
            varid12= charIDToTypeID( "Prvs" );
            ref2.putEnumerated( id10, id11, id12 );
        desc5.putReference( id9, ref2 );
        executeAction( id8, desc5, DialogModes.NO );
        return resultLayers;
    }   
    varlayers= getSelectedLayers();

Source: https://github.com/kynd/photoshopScripts/blob/master/Rename%20Selected%20Layers/Rename%20Selected%20Layers.jsx

Solution 3:

You'll need to check out the 'ArtLayers' and 'LayerSets' objects. The following snippet will get all the layers in the first group within a document:

var lyrs = app.activeDocument.layerSets[0].artLayers;

But since each of your groups can contain other groups, you may have to recursively loop through all of them to get all your layers depending on your use case.

I highly recommend checking out the xtools library. It has a nice function 'Stdlib.getLayersList' that will allow you to get the all the layers recursively from nested groups (plus a whole lot of other great stuff). Get the library here.

Post a Comment for "Getting Selected Layer Or Group Layers Array Using Javascript (photoshop Cs4)"