Copy link to clipboard
Copied
I am building a script to cleanse photoshop documents, and one of the options I am including is to remove all hidden layers. There is no direct command to do so (or none that I have seen), so I used a chunk of code from ScriptListener to perform the 'Delete hidden layers' command.
I only want to enable this option if there are layers that are hidden, so I'm wondering if there is a simple way to determine if any layers are hidden in the document (aside from looping through all of the layers). Or, if there's a way to determine if the 'Delete hidden layers' command is enabled or not, since that is only enabled when there are hidden layers.
I would just bung the code between a try catch block (but I'm lazy )
If you did want to check this should be fast...
...alert(hasHiddenLayers());
function hasHiddenLayers(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
try{
activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
for(i;i<count;i++){
if(i == 0) continue;
Copy link to clipboard
Copied
I would just bung the code between a try catch block (but I'm lazy )
If you did want to check this should be fast...
alert(hasHiddenLayers());
function hasHiddenLayers(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
try{
activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
for(i;i<count;i++){
if(i == 0) continue;
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
if(layerName.match(/^<\/Layer group/) ) continue;
if(!desc.getBoolean(charIDToTypeID( "Vsbl" ))) return true;
};
return false;
};
Copy link to clipboard
Copied
Thanks, Paul, that seems to work. Haven't tried it against any massive files yet, but it works with the files I tested.
Michael, I am using CS5-CS6. The 'Delete hidden layers' command was what I recorded with the ScriptListener, would have used the runMenuItem, but I'm lazy like Paul, and didn't bother to figure out how to look up the index.
Thanks to you both
Copy link to clipboard
Copied
I miss-read the post. Yeah I agree with Paul - just warp the executeAction in a try/catch.
I tried to see if I could get a descriptor for hidden layers with this code.
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), stringIDToTypeID( "hidden" ) );
var desc = executeActionGet(ref)
But it returns the descriptor for the top layer regardless of that layer's visibility.
Copy link to clipboard
Copied
What version of Photoshop are you using? Newer version do have a 'delete hidden layers' command that you could call using app.runMenuItem()