0

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!

1 Answer 1

0

I use a function from Stdlib in xtools available here: http://ps-scripts.sourceforge.net/xtools.html. Just check the length of the array returned from Stdlib.getSelectedLayers() before running your code above - or inspect the code from the library to modify it to your own needs.

Reading your question again, I wonder if the above code isn't the getSelectedLayers function from the same library? If so, have you tried checking for an activeDocument before running it? I would think if you have any layers selected at least one of them should be active??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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