6

I've been working with a function I found on stackoverflow here that works as far as I can tell by grouping the layers, dumping layer names into an array then ungrouping them.

function getSelectedLayers(){
    var idGrp = stringIDToTypeID( "groupLayersEvent" );
    var descGrp = new ActionDescriptor();
    var refGrp = new ActionReference();
    refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
    descGrp.putReference(charIDToTypeID( "null" ), refGrp );
    executeAction( idGrp, descGrp, DialogModes.ALL );
    var resultLayers=new Array();
    for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}
    var id8 = charIDToTypeID( "slct" );
        var desc5 = new ActionDescriptor();
        var id9 = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var id10 = charIDToTypeID( "HstS" );
        var id11 = charIDToTypeID( "Ordn" );
        var id12 = charIDToTypeID( "Prvs" );
        ref2.putEnumerated( id10, id11, id12 );
    desc5.putReference( id9, ref2 );
    executeAction( id8, desc5, DialogModes.NO );
    return resultLayers;
}   
var layers = getSelectedLayers();

Unfortunately when no layers are selected and this function is run Photoshop throws the message

The command “Group Layers” is not currently available.

Is there any way to at least return a null value when this happens without the script stalling? That way I could display a message about needing to select layers when it's null.

OR Is there any way to determine that no layers are selected before the function tries to run the grouping function?

Any help is greatly appreciated!

  • In order to get a solid answer, please explain why you need to check this. You are iterating through the layers, make them active and want to check if you are reached the last one? I'd suggest to count and sort the layers first. – p2or Jun 21 '15 at 6:08
0

Something like this should work....

var activelayer = application.document.activeLayer;

if(activeLayer){
   var layers = getSelectedLayers();
}else{
   alert("Select a Layer");
}
  • Unfortunately already tried that. When running var activeLayer = activeDocument.activeLayer; function main(){ alert(activeLayer); } main(); with no layers selected in Photoshop, it returns the name of the top layer in the layers panel. No clue why. – Joeykangaroo Feb 21 '15 at 1:30
  • Yes, it seems like this used to work nut Im going off memory. There are a few other values you could check, you have probably thought of these... code //You could check the kind of layer it is. ArtLayers Dont have the layers Array inside them // activeLayer.typename === "LayerSet" // if Layerset check the length of the app.activeDocument.activeLayer.layers array length //activeLayer.typename === "ArtLayer" //if artLayer it can have a "kind " // Or you could check the bounds of the layer to see if its empty code Formatting looks horrible sorry about that. – Andy Stone Feb 21 '15 at 3:21
0

I think this will work, I tested it... By checking the objType you can see if its an array or LayerSet/Folder. I hope this gets you on the right track.

function getSelectedLayers(){
        var objType = typeof app.activeDocument.activeLayer.layers;
        if(objType !== "undefined"){    
            var len = app.activeDocument.activeLayer.layers.length;
            if(len > 0){            
                    var idGrp = stringIDToTypeID( "groupLayersEvent" );
                    var descGrp = new ActionDescriptor();
                    var refGrp = new ActionReference();
                    refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
                    descGrp.putReference(charIDToTypeID( "null" ), refGrp );
                    executeAction( idGrp, descGrp, DialogModes.ALL );
                    var resultLayers=new Array();
                    for (var ix=0;ix<len;ix++){
                                   resultLayers.push(app.activeDocument.activeLayer.layers[ix])
                    };
                    var id8 = charIDToTypeID( "slct" );
                    var desc5 = new ActionDescriptor();
                    var id9 = charIDToTypeID( "null" );
                    var ref2 = new ActionReference();
                    var id10 = charIDToTypeID( "HstS" );
                    var id11 = charIDToTypeID( "Ordn" );
                    var id12 = charIDToTypeID( "Prvs" );
                    ref2.putEnumerated( id10, id11, id12 );
                    desc5.putReference( id9, ref2 );
                    executeAction( id8, desc5, DialogModes.NO );
                      return resultLayers;
            }
   }
    return null;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.