Skip to main content
Known Participant
September 14, 2020
Answered

Script: Delete selected layer, if empty.

  • September 14, 2020
  • 2 replies
  • 3986 views

There are various ways to delete all layers that are empty, but is there a script which can detect if the "selected" layer is empty, and if so, delete it?
Thanks!
Jeff

This topic has been closed for replies.
Correct answer r-bin

I'm using CS6.. lol

 

Thanks!


Option for CS6

var r = new ActionReference();    
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var list = executeActionGet(r).getList(stringIDToTypeID("targetLayers"));

var selected = new Array();
var ids = new Array();

for (var i = 0; i < list.count; i++) selected.push(list.getReference(i).getIndex());

for (var i = 0; i < selected.length; i++) 
    {
    var r = new ActionReference();    
    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("bounds"));
    r.putIndex(stringIDToTypeID("layer"), selected[i]);

    var bounds = executeActionGet(r).getObjectValue(stringIDToTypeID("bounds"));

    var x0 = bounds.getUnitDoubleValue(stringIDToTypeID("left"));
    var x1 = bounds.getUnitDoubleValue(stringIDToTypeID("right"));
    var y0 = bounds.getUnitDoubleValue(stringIDToTypeID("top"));
    var y1 = bounds.getUnitDoubleValue(stringIDToTypeID("bottom"));

    if (!x0 && !x1 && !y0 && !y1) ids.push(selected[i]);
    }

if (ids.length)
    {
    var r = new ActionReference();
    for (var i = 0; i < ids.length; i++) r.putIndex(stringIDToTypeID("layer"), ids[i]);

    var d = new ActionDescriptor();
    d.putReference(stringIDToTypeID("null"), r);
    executeAction(stringIDToTypeID("delete"), d, DialogModes.NO); 
    }

2 replies

Legend
September 16, 2020
This script removes empty layers among selected.
 
var r = new ActionReference();    
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayersIDs"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var list = executeActionGet(r).getList(stringIDToTypeID("targetLayersIDs"));

var selected = new Array();
var ids = new Array();

for (var i = 0; i < list.count; i++) selected.push(list.getReference(i).getIdentifier());

for (var i = 0; i < selected.length; i++) 
    {
    var r = new ActionReference();    
    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("bounds"));
    r.putIdentifier(stringIDToTypeID("layer"), selected[i]);

    var bounds = executeActionGet(r).getObjectValue(stringIDToTypeID("bounds"));

    var x0 = bounds.getUnitDoubleValue(stringIDToTypeID("left"));
    var x1 = bounds.getUnitDoubleValue(stringIDToTypeID("right"));
    var y0 = bounds.getUnitDoubleValue(stringIDToTypeID("top"));
    var y1 = bounds.getUnitDoubleValue(stringIDToTypeID("bottom"));

    if (!x0 && !x1 && !y0 && !y1) ids.push(selected[i]);
    }

if (ids.length)
    {
    var r = new ActionReference();
    for (var i = 0; i < ids.length; i++) r.putIdentifier(stringIDToTypeID("layer"), ids[i]);

    var d = new ActionDescriptor();
    d.putReference(stringIDToTypeID("null"), r);
    executeAction(stringIDToTypeID("delete"), d, DialogModes.NO); 
    }
 
Known Participant
September 16, 2020

Thanks for the help, and  scripts!

Stephen_A_Marsh,   The addition was just what i needed to make it work,  Thanks!

 

r-bin,  Also, thanks for the script!,   Although it gave me this error.

"- The command 'Get' is not currently available.
Line: 5"

 

Thanks again for all the help!

Jeff

Legend
September 16, 2020
Which version of photoshop?
My script does not work in low-end Photoshop like CS6.
But it is possible to remake.
 
Community Expert
September 14, 2020

Doesn't the "Delete All Empty layers" script shipped with PS work for you.

 

-Manan

-Manan
Known Participant
September 14, 2020

Thanks Manan,

 

I have several layers that are empty, which i need to keep.

This action is part of a larger script.

For this part, it focuses on a selected layer, and remove it, if empty.

 

Thanks

Jeff

Inspiring
September 16, 2020

Wouldn’t 

( 0 == layer.bounds[2] && (0 == layer.bounds[3])

theoretically also identify Layers that are off-canvas and the lower right corner of whic is exactly in the top left corner of the canvas? 


Hahaha, nuts, almost got a right solution there... 🙂

I left the activeLayer part out on purpose to encourage OP to get acquainted with the Photoshop Scripting Reference.

 

For OP: the following link takes you to Photoshop Scripting documentation.

Application

 

Alternatively, the following link presents the same information in a slightly different way. Somehow, when I'm stuck using the first link, my brain "gets it" using this one: (I suppose it doesn't make any sense, but it works for me)

Application

 

If you're ever stuck on something, it often pays off to go to either one and look around.

 

Another link I can highly recommend is this one which deals less with Photoshop and more with "how to code/script"

https://estk.aenhancers.com/

 

Good luck almost namesake!