4 Replies Latest reply on Jun 22, 2013 3:05 AM by Muppet Mark

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

    Mattmcquiff Level 1

      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

        • 1. Re: Detect if top line of a layer is empty or not?
          Muppet Mark Level 5

          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…

          • 2. Re: Detect if top line of a layer is empty or not?
            pixxxel schubser MVP & Adobe Community Professional

            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")
                }
            
            • 3. Re: Detect if top line of a layer is empty or not?
              Michael L Hale Level 5

              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

              }

              • 4. Re: Detect if top line of a layer is empty or not?
                Muppet Mark Level 5

                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