Selecting multiple layers

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

kpt

Selecting multiple layers

Post by kpt »

While it's possible in Photoshop user interface to select several layers in a document (by holding down the Cmd-key and clicking on the layers in the Layer's Palette, or holding down Shift and clicking on the images with the Move tool) I can't find any mention in the Javascript documentation how to get hold of these selected layers.

The method app.activeDocument.activeLayer only returns one of these
layers (the top one, it seems) when several are selected.

Does anyone know how to get access to all of the selected layers?
Mike Hale

Selecting multiple layers

Post by Mike Hale »

You can use scriptlistner to group the selected layers. That makes the group the activeLayer. Then check activeLayer.layers to get the layers that where selected. Then undo to remove the group and restore the layer selections.

Code: Select allfunction groupSelectedLayers(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putClass( stringIDToTypeID( "layerSection" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var ref2 = new ActionReference();
        ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc.putReference( charIDToTypeID( "From" ), ref2 );
executeAction(charIDToTypeID( "Mk  " ), desc, DialogModes.NO );
}

groupSelectedLayers();
var groupLayers = activeDocument.activeLayer.layers
var selectedLayers = new Array;
for(var i=0;i<groupLayers.length;i++){
   selectedLayers.push(groupLayers);
}
executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );

Mike
xbytor

Selecting multiple layers

Post by xbytor »

There is also this from stdlib.js (from my xtools lib):

Code: Select allvar layers = Stdlib.getSelectedLayers(doc);

-X
kpt

Selecting multiple layers

Post by kpt »

You guys are geniuses, thank you very much!
kpt

Selecting multiple layers

Post by kpt »

To complete this thread, I thought I would offer the following (opposite) code to select mutliple layers by their names. I couldn't find anything in the documentation to do this so I looked at the output from ScriptListener.

Code: Select allfunction multiSelect(layerNames) {
    var layers = new Array();
    var id54 = charIDToTypeID( "slct" );
    var desc12 = new ActionDescriptor();
    var id55 = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    for (var i = 0; i < layerNames.length; i++) {
        layers = charIDToTypeID( "Lyr " );
        ref9.putName(layers, layerNames);
    }
    desc12.putReference( id55, ref9 );
    var id58 = charIDToTypeID( "MkVs" );
    desc12.putBoolean( id58, false );
    executeAction( id54, desc12, DialogModes.NO );
}


var layers = [];
layers[0] = "Layer A";
layers[1] = "Layer B";
layers[2] = "Layer C";
layers[3] = "Layer D";

multiSelect(layers);