Question
Photoshop Script to delete unused layers
Hi all,
I have tried the below code to delete the unused layers in the layerset and artlayers, but the code taking too much time delete the unused layers if the psd file containing lot of layers & layerset. Could anyone please help to get rid of this?
if (app.documents.length) {
var myDocument = app.activeDocument;
var theEmptyLayers = collectEmptyLayers(myDocument, []);
for (var m = 0; m < theEmptyLayers.length; m++) {
theEmptyLayers[m].remove()
}
};
////// function collect all empty layers //////
function collectEmptyLayers (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array}
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers[m];
var bounds = theLayer.bounds;
var check = false;
if (Number(bounds[0]) == 0 && Number(bounds[1]) == 0 && Number(bounds[2]) == 0 && Number(bounds[3]) == 0) {check = true};
if (theLayer.typename == "ArtLayer") {
if (check == true) {
allLayers.push(theLayer)
}
}
// apply the function to layersets;
else {
allLayers = (collectEmptyLayers(theLayer, allLayers))
if (check == true) {
allLayers.push(theLayer);
}
}
};
return allLayers
};
