Search code examples
javascriptphotoshopphotoshop-script

Is it possible to check if certain layer is empty? CS6 Script


I want function which checks if certain layer has all pixels with 0% transparency, in other words, layer is just empty.

function isLayerEmptyCheck(layer) {
   //code
}

Unfortunately, I couldn't find any information in documentation (Photoshop CS6 Scripting Guide, Photoshop CS6 JavaScript Ref) of any artlayer property of this kind.


Solution

  • There is only need to check of property of artlayer bounds to find out, if all values of its arrays are equal to "0 px". When all of them are "0 px", then it means that layer is empty.

    Below I created function which checks if input layer is unfilled.

    #target photoshop
    
    var doc = app.activeDocument;
    var certainLayer = doc.artLayers[0];
    
    var isLayerEmpty = isLayerEmptyCheck(certainLayer);
    
    alert(isLayerEmpty);
    
    function isLayerEmptyCheck(layer) {
    
        var isLayerEmpty = new Boolean;
    
        var LayerBounds = layer.bounds;
        if (LayerBounds[0] === "0 px" && LayerBounds[1] === "0 px" && LayerBounds[2] === "0 px" && LayerBounds[3] === "0 px") {
            return isLayerEmpty = true;
        } else {
            return isLayerEmpty = false;
        }
    
    }