Select all layers within a group with script

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

GDrider77

Select all layers within a group with script

Post by GDrider77 »

So i would like to run a script on a group that can contain different amounts of layers with different names. Is it possible to select all layers(regardlesss of number or name) within "Group1" say, and then do something with them?

thanks
r

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Select all layers within a group with script

Post by Mike Hale »

Something like this should select all layer and nested layersets in the selected layer.

Code: Select all// assumes the group you want to select the layer in is the activeLayer
var groupStartIdx = getActiveLayerIndex();
app.activeDocument.activeLayer = app.activeDocument.activeLayer.layers[app.activeDocument.activeLayer.layers.length-1];
var groupEndIdx = getActiveLayerIndex();
for(l=groupEndIdx;l<groupStartIdx;l++){
    addToLayerSelectionByIndex( l );
}
   
function addToLayerSelectionByIndex( idx ) {
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIndex( charIDToTypeID('Lyr '), idx );
    desc.putReference( charIDToTypeID('null'), ref );
    desc.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection') );
    desc.putBoolean( charIDToTypeID('MkVs'), false );
    executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
};
function getActiveLayerIndex() {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var res = executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )) - 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;
};
GDrider77

Select all layers within a group with script

Post by GDrider77 »

thanks, that works perfectly!