Skip to main content
Mattmcquiff
Inspiring
June 21, 2013
Question

Detect if top line of a layer is empty or not?

  • June 21, 2013
  • 3 replies
  • 1269 views

I'm trying to work out how the top line of an image layer is transparent or not? I need to know if it contains visable pixels so that i instruct the crop correctly.

Ideally in applescript as I understand this. otherwise usual script is fine too.

Many Thanks

Matt

This topic has been closed for replies.

3 replies

Inspiring
June 21, 2013

Depending on what you are trying to do you could use Document.trim(). That would remove transparent pixels from the top of images that have them and do nothing for the images that don't have transparent pixel at the top.

You could use scriptlistener to load then save the document transparency channel to an alpha channel then select the top row and get the histogram of that alpha channel.

There are other ways as well but they use a layer object so may not work correctly if the document has more than one layer.

Or do it the easy way and use Mark's suggestion. But again it tries to copy the activeLayer. Another layer may not be transparent at the top.

// select the area you want to test for transparency

try{

    app.activeDocument.selection.copy();

}catch(e){

    // deal with transparent pixels in selection

}

Inspiring
June 22, 2013

tell application "Adobe Photoshop CS5"

  activate

          tell current document

                    set the current layer to the first layer

                    set w to width

                    select region {{0, 0}, {w, 0}, {w, 1}, {0, 1}}

                    try

  copy

                              display dialog "Your selection has data!!!" giving up after 2

                    on error

                              display dialog "Your selection is empty!!!" giving up after 2

                    end try

          end tell

end tell

pixxxelschubser
Community Expert
Community Expert
June 21, 2013

There are many possibilities. E.g. this Javascript works for the active layer:

var Bds = app.activeDocument.activeLayer.bounds;

if (Bds[1] > 0) {

    alert (Bds[1] + " distance from above")

    }

Inspiring
June 21, 2013

I would make a selection of the area and try to copy it… PS will throw an error if it's empty…

I think their are better ways though…