: Ending a Photoshop script when no layers are selected 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
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!
More posts by @Gonzalez368
2 Comments
Sorted by latest first Latest Oldest Best
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;
}
Something like this should work....
var activelayer = application.document.activeLayer;
if(activeLayer){
var layers = getSelectedLayers();
}else{
alert("Select a Layer");
}
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.