Copy link to clipboard
Copied
Hi,
I would like to delete all layers including background and layer sets except of selected layer.
It seemed easy first but it stops... I tried several ways; What am I doing wrong ?
#target photoshop
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
app.bringToFront();
var doc = app.activeDocument;
var currentLayer = doc.activeLayer; // remember the selected layer
var selectedlayername = currentLayer.name;
currentLayer.move( activeDocument, ElementPlacement.PLACEATBEGINNING );
function DeleteLayer(layername) { var desc1 = new ActionDescriptor();
var ref1 = new ActionReference(); ref1.putName(cTID('Lyr '), layername);
desc1.putReference(cTID('null'), ref1); var list1 = new ActionList();
list1.putInteger(17); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('Dlt '), desc1, DialogModes.NO);
};
function DeleteBackground() { var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putProperty(cTID('Lyr '), cTID('Bckg')); desc1.putReference(cTID('null'), ref1); var list1 = new ActionList(); list1.putInteger(1); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('Dlt '), desc1, DialogModes.NO); };
function initial () {
for(var i = 0 ; i < doc.layers.length;i++){ if (doc.layers.isBackgroundLayer == true ) { DeleteBackground();} }
var layerNum = doc.layers.length;
for(var s = 1 ; s <= layerNum;s++){
if ( doc.layers
.name != selectedlayername ) {DeleteLayer (doc.layers
.name);}
else if ( doc.layers
.name == selectedlayername ) {}
}
}
initial();
Thank you,
Best Regards.
Hi arteangelus,
Try this Code...
#target photoshop
var docRef = app.activeDocument;
var layName = docRef.activeLayer.name
var l = Number(docRef.layers.length)+1;
while(--l){
if(docRef.layers[l-1].name!=layName && docRef.layers[l-1].name!="Background"){
docRef.layers[l-1].remove();
}
else{continue;}
}
-yajiv
Copy link to clipboard
Copied
I think it may be easier to open a new document with a transparent canvas the size of the current documents canvas then duplicate the selected layers into that new document. Layers names may not be unique and if you start deleting layers layers positions in the stack change. The selected layer may no longer be selected. If you noted the selected layer name there may be more than one layer with the same name which do you delete which do you keep. It not an easy thing to program.
If you make that new document you could delete all layers but one copy the new document layers back in close no save the new document and the delete the bottom two layer..
Copy link to clipboard
Copied
The reason yours may not be working is that you're using the actual length of the layers in your loop, and that changes as you delete the layers. You need to assign that number to a variable that will not change as you delete the layers. Try this script:
#target photoshop
var doc = activeDocument;
var curLayer = doc.activeLayer
curLayer.isBackgroundLayer = false;
curLayer.move (doc.layers[0],ElementPlacement.PLACEBEFORE);
var numLayers = doc.layers.length
for(var i=1;i<numLayers;i++){
doc.layers[1].remove();
}
Copy link to clipboard
Copied
If you want faster performance, then I would recomend you
1) Remeber your selected layer
2) Try unlock background
try{
var idsetd = charIDToTypeID( "setd" );
var desc24 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref13 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idBckg = charIDToTypeID( "Bckg" );
ref13.putProperty( idLyr, idBckg );
desc24.putReference( idnull, ref13 );
var idT = charIDToTypeID( "T " );
var desc25 = new ActionDescriptor();
var idOpct = charIDToTypeID( "Opct" );
var idPrc = charIDToTypeID( "#Prc" );
desc25.putUnitDouble( idOpct, idPrc, 100.000000 );
var idMd = charIDToTypeID( "Md " );
var idBlnM = charIDToTypeID( "BlnM" );
var idNrml = charIDToTypeID( "Nrml" );
desc25.putEnumerated( idMd, idBlnM, idNrml );
var idLyr = charIDToTypeID( "Lyr " );
desc24.putObject( idT, idLyr, desc25 );
var idLyrI = charIDToTypeID( "LyrI" );
desc24.putInteger( idLyrI, 3229 );
executeAction( idsetd, desc24, DialogModes.NO );}
catch(){;}
3) Choose menu item "Select > All layers"
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc20 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref9 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref9.putEnumerated( idLyr, idOrdn, idTrgt );
desc20.putReference( idnull, ref9 );
executeAction( idselectAllLayers, desc20, DialogModes.NO );
4) Unlock all locks on this selection Photoshop: Javascript - can't unlock folder with script which was generated by ScriptListener | Phot...
5) Deselect layer you remebered in variable
6) Delete
Loops are incredible slow if call some in Photoshop method in each step. This algorithm is without loop.
Copy link to clipboard
Copied
Hi arteangelus,
Try this Code...
#target photoshop
var docRef = app.activeDocument;
var layName = docRef.activeLayer.name
var l = Number(docRef.layers.length)+1;
while(--l){
if(docRef.layers[l-1].name!=layName && docRef.layers[l-1].name!="Background"){
docRef.layers[l-1].remove();
}
else{continue;}
}
-yajiv
Copy link to clipboard
Copied
Thank you natrev​,
I have combined your script with Delete background function as well so here is a working example if someone needs it;
#target photoshop
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
var docRef = app.activeDocument;
var layName = docRef.activeLayer.name
function DeleteBackground() { var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putProperty(cTID('Lyr '), cTID('Bckg')); desc1.putReference(cTID('null'), ref1); var list1 = new ActionList(); list1.putInteger(1); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('Dlt '), desc1, DialogModes.NO); };
for(var i = 0 ; i < docRef.layers.length;i++){
if (docRef.layers.isBackgroundLayer == true ) { DeleteBackground();} }
var l = Number(docRef.layers.length)+1;
while(--l){
if(docRef.layers[l-1].name!=layName && docRef.layers[l-1].name!="Background"){
docRef.layers[l-1].remove();
}
else{continue;}
}