I can probably find this code easily somewhere else and just insert it into my script, but I'm trying to understand it, so I'm attempting to write it myself.
That being said, ARGH! Why is this not deleting all the hidden layers?
Code: Select all#target Photoshop
var myDoc = app.activeDocument
myDoc.activeLayer = myDoc.layers[myDoc.layers.length-1]//don't know if I need this, but I thought maybe it wasn't working because it wasn't starting at the bottom
for (var n=0; n<myDoc.layers.length; n++) {
var thisLayer = myDoc.layers[n]
if (thisLayer.visible == false) {
thisLayer.remove()
}
}
if (myDoc.mode = CMYKColor) {
myDoc.mergeVisibleLayers()
myDoc.changeMode(ChangeMode.RGB)
}
Here's a before and after of the layers panel. The file I'm using is just a test document. I need to NOT flatten documents, but make sure nothing is discarded that is visible when I convert documents to RGB, if they are CMYK. Maybe there's an option to specify merge instead of discard when converting the mode?
delete hidden layers
-
larsen67
delete hidden layers
I have not tried your code but… When scripting ( in general ) its better to loop in reverse for actions such moving or removing items in an array or collection…
This way you preserve the index… Each time to remove an object n shifts -1… This no matter if you start at the back of the pack/deck…
n will always be an item in the array range… I hope that makes sense…?
There is a call out from the layers menu for this ( built in function ) so it may be that you can call using script listener ( action manager syntax )
This way you preserve the index… Each time to remove an object n shifts -1… This no matter if you start at the back of the pack/deck…
n will always be an item in the array range… I hope that makes sense…?
There is a call out from the layers menu for this ( built in function ) so it may be that you can call using script listener ( action manager syntax )
-
larsen67
delete hidden layers
OK, I just tried it and it all look fine to me…
Code: Select all#target photoshop
deleteHiddenLayers();
function deleteHiddenLayers() {
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };
var desc9 = new ActionDescriptor();
var ref8 = new ActionReference();
ref8.putEnumerated( cTID('Lyr '), cTID('Ordn'), sTID('hidden') );
desc9.putReference( cTID('null'), ref8 );
executeAction( cTID('Dlt '), desc9, DialogModes.NO );
};
For this I used X's toolkit and added the script listener plugin provided by Adobe… Instructions in the guides…
Code: Select all#target photoshop
deleteHiddenLayers();
function deleteHiddenLayers() {
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };
var desc9 = new ActionDescriptor();
var ref8 = new ActionReference();
ref8.putEnumerated( cTID('Lyr '), cTID('Ordn'), sTID('hidden') );
desc9.putReference( cTID('null'), ref8 );
executeAction( cTID('Dlt '), desc9, DialogModes.NO );
};
For this I used X's toolkit and added the script listener plugin provided by Adobe… Instructions in the guides…
-
essejesse
delete hidden layers
Thank you. I've integrated the script listener code you provided and tested once. It worked just as expected.
Can you elaborate a little more on what you mean by looping in reverse? I'm not all that familiar with scripting and want to make sure I understand before I move on to another task.
Do you mean that because I was starting at the bottom of the layer stack, and working up? I tried making the active layer the top layer[0], but that didn't perform any better.
Can you elaborate a little more on what you mean by looping in reverse? I'm not all that familiar with scripting and want to make sure I understand before I move on to another task.
Do you mean that because I was starting at the bottom of the layer stack, and working up? I tried making the active layer the top layer[0], but that didn't perform any better.
-
Mike Hale
delete hidden layers
I think if you are looping through layers( or anything else for that matter ) by index you shouldn't do anything in the loop that changes the index. Adding, deleting, and/or moving layers in most cases changes the index of the layers. If you are careful you may be able to deal with the sliding indexes. But I think you are better off using the first loop to collect the objects you want to work with. Then another loop to do whatever you need to do.
So loop through the layers and if not visible add the layer object to an array. After the loop is done you will have an array of layer objects you can delete without worrying about what removing the layers is doing to the index of all the layers.
So loop through the layers and if not visible add the layer object to an array. After the loop is done you will have an array of layer objects you can delete without worrying about what removing the layers is doing to the index of all the layers.
-
essejesse
delete hidden layers
This has been fun getting these to work. Are there any drawbacks to doing this either one of these ways?
This one reverse loops:
Code: Select all#target Photoshop
var myDoc = app.activeDocument
for (var n=myDoc.layers.length-1; n>=0; n--) {
var thisLayer = myDoc.layers[n]
if (thisLayer.visible == false) {
thisLayer.remove()
}
}
and this one creates the array of hidden layers, and then removes them:
Code: Select all#target Photoshop
var myDoc = app.activeDocument
var deleteMe = new Array ()
for (var n=0; n<myDoc.layers.length; n++) {
if (myDoc.layers[n].visible == false) {
deleteMe.push(myDoc.layers[n]) //this works, though I don't know if it's the right way to do it
}
}
for (var i=0; i<deleteMe.length; i++) {
deleteMe.remove()
}
Also, is there a better way to just remove all the layers in the array?
This one reverse loops:
Code: Select all#target Photoshop
var myDoc = app.activeDocument
for (var n=myDoc.layers.length-1; n>=0; n--) {
var thisLayer = myDoc.layers[n]
if (thisLayer.visible == false) {
thisLayer.remove()
}
}
and this one creates the array of hidden layers, and then removes them:
Code: Select all#target Photoshop
var myDoc = app.activeDocument
var deleteMe = new Array ()
for (var n=0; n<myDoc.layers.length; n++) {
if (myDoc.layers[n].visible == false) {
deleteMe.push(myDoc.layers[n]) //this works, though I don't know if it's the right way to do it
}
}
for (var i=0; i<deleteMe.length; i++) {
deleteMe.remove()
}
Also, is there a better way to just remove all the layers in the array?