5 Replies Latest reply on Aug 19, 2010 1:39 AM by Muppet Mark-QAl63s

    Selecting a Graphic Layer with Apple Script

    chris Harges

      I'm trying to build on a very helpful post by sstanleyau showing how to choose different layers in a placed PSD or PDF. Unfortunately, I'm stuck in my test script. It seems so simple …

       

      Here's what the script should do after an image is placed in a text frame:

       

      1. "Read" the names of each of the graphic layers in the placed graphic.

      2. If one of the names contains a string, then

      3. Turn current visibility of that layer on

      4. Turn the visibility of every other layer off.

       

      However, my script seems to come up with a different result each time I run it and often fails to "find" a layer that I know is there. Is it possible that my script can't access the name of a graphic layer if its visibility is set to off?

       

      Here's why I've got

       

      --before running, user selects a test frame containing a placed PDF with multiple layers named blue, red and green.

       

      tell application "Adobe InDesign CS4"

      set theFrame to item 1 of selection

      set glOptions to graphic layer options of graphic 1 of theFrame

      set theName to text of "blue"

       

      repeat with z from 1 to count of graphic layers in glOptions

      if name of graphic layer z of glOptions contains theName then

      repeat with d from 1 to count of graphic layers in glOptions

      if current visibility of graphic layer d of glOptionsfalse then

      set current visibility of graphic layer d of glOptions to false

      end if

      end repeat

       

      if current visibility of graphic layer d of glOptions = false then

      set current visibility of graphic layer z of glOptions to true

      end if

      end if

      end repeat

      end tell

        • 1. Re: Selecting a Graphic Layer with Apple Script
          sstanleyau Level 4

          I think you're probably after something more like this:

           

          tell application "Adobe InDesign CS5"

          set theFrame to item 1 of selection

          set glOptions to graphic layer options of graphic 1 of theFrame

          set theName to "blue"

           

          repeat with z from 1 to count of graphic layers in glOptions

          if name of graphic layer z of glOptions contains theName then

          repeat with d from 1 to count of graphic layers in glOptions

          if current visibility of graphic layer d of glOptionsfalse then

          set current visibility of graphic layer d of glOptions to false

          end if

          end repeat

          else

          repeat with d from 1 to count of graphic layers in glOptions

          if current visibility of graphic layer d of glOptionstrue then

          set current visibility of graphic layer d of glOptions to true

          end if

          end repeat

          end if

          end repeat

          end tell

          • 2. Re: Selecting a Graphic Layer with Apple Script
            chris Harges Level 1

            Hmm. I'm still getting the same problem. It will toggle off one layer and then throw this error:

             

            error "Adobe InDesign CS4 got an error: Can’t get current visibility of graphic layer 2 of graphic layer options of PDF id 209 of rectangle id 206 of page id 190 of spread id 185 of document \"Untitled-1\"." number -1728 from current visibility of graphic layer 2 of graphic layer options of PDF id 209 of rectangle id 206 of page id 190 of spread id 185 of document "Untitled-1"

            • 3. Re: Selecting a Graphic Layer with Apple Script
              Muppet Mark-QAl63s Level 4

              Im NOT sure if you have just made this a little over complicated… This works just fine for me. Tested with about a dozen psd layers…

               

               

              tell application "Adobe InDesign CS2"

              tell active document

              tell rectangle 1

              tell graphic 1

              tell graphic layer options

              set GLC to count of graphic layers

              repeat with i from 1 to GLC

              if name of graphic layer i ≠ "Blue" then

              set current visibility of graphic layer i to false

              else

              set current visibility of graphic layer i to true

              end if

              end repeat

              end tell

              end tell

              end tell

              end tell

              end tell

               

              This variant also works as I would have expected too… Retaining visibility from a list…

               

              tell application "Adobe InDesign CS2"

              tell active document

              tell rectangle 1

              tell graphic 1

              tell graphic layer options

              set On_List to {"Red", "Aqua", "Orange"}

              set GLC to count of graphic layers

              repeat with i from 1 to GLC

              if name of graphic layer i is not in On_List then

              set current visibility of graphic layer i to false

              else

              set current visibility of graphic layer i to true

              end if

              end repeat

              end tell

              end tell

              end tell

              end tell

              end tell

              • 4. Re: Selecting a Graphic Layer with Apple Script
                chris Harges Level 1

                Thanks Muppet Mark and Shane,

                 

                Muppet Mark's first variant works for me perfectly. So why does both my initial script and Shane's version throw an error? The only difference I can see is that we set graphic layer options to a variable and you nested it in a tell block. Either way, we're both working the hierarchy right so the script should be able to find the graphic layers either way. I'm happy for a script that works by confused by why one works and the other doesn't.

                • 5. Re: Selecting a Graphic Layer with Apple Script
                  Muppet Mark-QAl63s Level 4

                  I got the same error as you when I tested the posted code. Did not have the time to try work out why but It contains 3 loops and I suspect some element is out of range in this, where the logic to me required only the 1 loop… Happy that it works 4U anyhow…