Copy link to clipboard
Copied
// 2025, use it at your own risk;
if (app.documents.length > 0) {
var theLayers = collectSelectedLayersWithoutColorAndLock ();
for (var m = 0; m < theLayers.length; m++) {
deleteLayerByID(theLayers[m][1], false)
}
};
////// collect bounds of selected layers //////
function collectSelectedLayersWithoutColorAndLock () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref =
...
Copy link to clipboard
Copied
If we can imagine it, it's highly likely that we can accomplish our need! The Adobe scribes, those robed, incense-burning, script-writing coders who lurk here, will be along with your solution, I'm sure, Delta.
Copy link to clipboard
Copied
Yes, this is possible, however, the devil is in the details...
As a starting point:
/*
Delete Unlocked Layers Without Color Label.jsx
Stephen Marsh
v1.0 - 27th May 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-delete-layers-by-color-or-attribute/m-p/15341191
*/
// Single history step
app.activeDocument.suspendHistory("Delete Unlocked Layers Without Color Label", "main()");
function main() {
deleteUnlockedLayersWithoutColorLabel(app.activeDocument.layers);
}
// Helper functions
function getLayerColorLabel(layer) {
try {
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), layer.id);
var desc = executeActionGet(ref);
var keyColor = stringIDToTypeID("color");
if (desc.hasKey(keyColor)) {
var colorType = desc.getEnumerationValue(keyColor);
var colorName = typeIDToStringID(colorType);
return colorName !== "none" ? colorName : null;
}
} catch (e) {
// Unable to get color label
}
return null; // No color label
}
function deleteUnlockedLayersWithoutColorLabel(layers) {
for (var i = layers.length - 1; i >= 0; i--) {
var layer = layers[i];
if (layer.isBackgroundLayer) {
// Skip removing the Background layer
// Comment out if you want to remove the Background layer
continue;
}
if (layer.typename === "ArtLayer") {
if (!layer.allLocked && getLayerColorLabel(layer) === null) {
try {
layer.remove();
} catch (e) { }
}
} else if (layer.typename === "LayerSet") {
// First process child layers
deleteUnlockedLayersWithoutColorLabel(layer.layers);
// Remove empty groups
if (layer.layers.length === 0 && !layer.allLocked) {
// Comment out if desired
layer.remove();
}
}
}
}
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
Copy link to clipboard
Copied
@Stephen Marsh Thanks for your reply, I want the script to delete only the selected layers that are not locked (lock all)
Copy link to clipboard
Copied
@Stephen Marsh Thanks for your reply, I want the script to delete only the selected layers that are not locked (lock all)
@Delta2487 – I invite you to try the script posted earlier (it doesn't work on selected layers, it loops over all layers). It has a single history step undo, just in case the results are not as expected.
Copy link to clipboard
Copied
@Stephen Marsh I've already tried it and it perfectly removes unlocked layers, but I only want it to work on selected layers.
Copy link to clipboard
Copied
@Stephen Marsh I've already tried it and it perfectly removes unlocked layers, but I only want it to work on selected layers.
By @Delta2487
That's the bit that is easier said than done...
Copy link to clipboard
Copied
That's the bit that is easier said than done...
By @Stephen Marsh
Yes it should be, anyway, thanks for the help
Copy link to clipboard
Copied
// 2025, use it at your own risk;
if (app.documents.length > 0) {
var theLayers = collectSelectedLayersWithoutColorAndLock ();
for (var m = 0; m < theLayers.length; m++) {
deleteLayerByID(theLayers[m][1], false)
}
};
////// collect bounds of selected layers //////
function collectSelectedLayersWithoutColorAndLock () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var checkColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
var checkLock = layerDesc.getObjectValue(stringIDToTypeID("layerLocking"));
var lockAll = checkLock.getBoolean(stringIDToTypeID("protectAll"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
if (checkColor == "none" && lockAll == false) {selectedLayers.push([theName, theIdentifier, ])};
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var checkColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
var checkLock = layerDesc.getObjectValue(stringIDToTypeID("layerLocking"));
var lockAll = checkLock.getBoolean(stringIDToTypeID("protectAll"));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
if (checkColor == "none" && lockAll == false) {selectedLayers = [[theName, theIdentifier, ]]}
} catch (e) {};
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};
Copy link to clipboard
Copied
@c.pfaffenbichler Thank you, it works perfectly, thank you very much.
Copy link to clipboard
Copied
If you want the thing to record as one Action step (instead of each deletion individually) you can combine it with @Stephen Marsh ’s suspendHistory-approach.
Copy link to clipboard
Copied
@c.pfaffenbichler - thanks for stepping in, I was having unexpected problems getting selected layers to work.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now