Copy link to clipboard
Copied
I am running the following script to delete hidden layers:
for(var i = 0 ; i < app.activeDocument.layers.length;i++){
currentLayer = app.activeDocument.layers;
if(currentLayer.visible == false) {
currentLayer.allLocked = false;
currentLayer.remove();
}
}
I realized however, that it does not take into consideration, if it has anything clipped to it, via clipping mask.
For example(below), if I delete hidden layers via this method Layer 1 gets deleted, and Layer 5 becomes visible, which unintended.
Is there a way to traverse clipped layers and delete them as well? Or is there a way to call the photoshop menu item "Delete Hidden Layers" which removes Layer 1 and it's clipped layers?
Copy link to clipboard
Copied
You can test if a layer is clipped another layer by:
if(currentLayer.grouped){alert('This layer is clipped')}
Note, it will not tell you if the layer you want to delete has clipped layers. What you will have to do is find your current layer and then select the layer above it to test to see if it's clipped to the lower layer.
Copy link to clipboard
Copied
You can run this command from script:
It delete also cliped layers. (All layers in document must be unlocked and it works only with whole document)
I also did some similar script which removes all layers with zero size/pixels/text-string inside. Even if they are visible. And I solved same Issue like you. Magic scripts for Photoshop (Delete empty layers faster v1.0) Anyway this script is focused to performance and code is hard to read and understand if you compare it with original Adobe script with slow recursive loop and slow DOM approach.
If layer is empty(invisible) and will be deleted it goes index above until there are cliped layers. It creates array of cliped layers and this array is merged with array where are layers to hide. I am hiding/deleting all layers in one step(not one by one) because of big performance diference. (line 333 and 288)
You also can have layer clipped to hidden folder since CS6.