8 Replies Latest reply on Dec 7, 2010 4:11 AM by Stephen Horne

    How to give commands to items of a list (Applescript)

    Stephen Horne Level 1

      Hi,

       

      I'm confused by something I've come across in indesign scripting. I'm making a list of page items from a selection, then sorting it into sublists, to each of whish I would like to pass commands. Once I've made them into lists, however, I always get an error if I try to pass a command to that list.

       

      Here is a condensed example:

       

      tell application "Adobe InDesign CS5"

      set itemList to selection

      set list1 to {item 1 of itemList, item 2 of itemList}

      set list2 to {item 3 of itemList, item 4 of itemList}

      tell list1

      set name to "test"

      end tell

      end tell

       

      or

       

      tell application "Adobe InDesign CS5"

      set itemList to selection

      set list1 to {item 1 of itemList, item 2 of itemList}

      set list2 to {item 3 of itemList, item 4 of itemList}

      set name of list1 to "test"

      end tell

       

      both give me an error, but if I tell the selection itself (also a list, no?) it works, eg:

       

      tell application "Adobe InDesign CS5"

      set name of selection to "test"

      end tell

       

      or, infuriatingly:

       

      tell application "Adobe InDesign CS5"

      set itemList to selection

      set list1 to {item 1 of itemList, item 2 of itemList}

      set list2 to {item 3 of itemList, item 4 of itemList}

      select list2

      set name of selection to "test"

      end tell

       

      so I can select the items using a list, but not much else.

       

      Does anyone know what's happening here?

       

      Thanks.

       

        • 1. Re: How to give commands to items of a list (Applescript)
          sstanleyau Level 4

          You can't send a command to a list; your script is seen as trying to set the name of a list, and that can't be done.

           

          Once you start addressing the selection,you're addressing a single InDesign identity -- the fact that it returns a list of items is irrelevant.

          • 2. Re: How to give commands to items of a list (Applescript)
            Stephen Horne Level 1

            Aha. That's a shame.

            Thanks for the help.

            • 3. Re: How to give commands to items of a list (Applescript)
              sstanleyau Level 4

              It's not that bad -- you can still act on multiple items at once. It's just that you can't specify them as a list, usually. But you can do something like "set name of page items of aGroup to"blah"".

              • 4. Re: How to give commands to items of a list (Applescript)
                Stephen Horne Level 1

                Thanks Shane, I've gone with selecting the list of items, then setting the name of the selection. Not very neat, but it serves the purpose. I'll post the finished script in case it's of interest to anyone.

                 

                It's a bodge to get around the way items that are grouped "forget" their original layers once ungrouped. Please enlighten me if I'm missing something; I've not posted anything about it, just searched in vain.

                 

                It comprises two scripts. I bind the first to cmd-G, and the second to cmd-shift-G in ID,replacing the standard "group" and "ungroup" shortcuts.

                 

                It's a bodge because it uses the "name" property of page items to store the previous layer name. The other option was the "label" property, but I use that, and don't use the name one. An earlier incarnation saved the string value of the name in there too, fixing it to how it was on the ungroup, but this got shaved out in some streamlining.

                 

                script 1:

                 

                set oldDelims to AppleScript's text item delimiters

                 

                tell application "Adobe InDesign CS5"

                set itemList to selection

                set theNameList to name of selection

                set theItemLayerList to item layer of selection

                set theDocumentLayerNameList to name of layers of active document

                set theDocumentLayerList to layers of active document

                 

                set itemLayerBreakdown to {}

                repeat with i from 1 to count theDocumentLayerList

                set end of itemLayerBreakdown to {}

                end repeat

                 

                repeat with i from 1 to count theItemLayerList

                repeat with j from 1 to count theDocumentLayerList

                if item i of theItemLayerList is item j of theDocumentLayerList then

                set end of item j of itemLayerBreakdown to (item i of itemList)

                end if

                end repeat

                end repeat

                repeat with i from 1 to (count itemLayerBreakdown)

                if item i of itemLayerBreakdown is not {} then

                set layerName to (item i of theDocumentLayerNameList)

                select item i of itemLayerBreakdown

                tell selection

                set name to layerName

                end tell

                end if

                end repeat

                 

                try

                set theNewGroup to make new group in active document with properties {group items:itemList}

                on error number 30477

                select itemList

                set lockedItems to items of selection whose locked is true

                select lockedItems

                display dialog "Selected items are locked and cannot be grouped."

                return

                end try

                select theNewGroup

                end tell

                 

                set AppleScript's text item delimiters to oldDelims as text

                 

                 

                 

                 

                script 2:

                 

                 

                 

                 

                set oldDelims to AppleScript's text item delimiters

                 

                tell application "Adobe InDesign CS5"

                set itemList to page items of selection

                ungroup selection

                select itemList

                set theNameList to name of selection

                set theDocumentLayerNameList to name of layers of active document

                set theDocumentLayerList to layers of active document

                 

                set itemLayerBreakdown to {}

                repeat with i from 1 to count theDocumentLayerList

                set end of itemLayerBreakdown to {}

                end repeat

                 

                repeat with i from 1 to count theNameList

                repeat with j from 1 to count theDocumentLayerList

                if item i of theNameList is item j of theDocumentLayerNameList then

                set end of item j of itemLayerBreakdown to (item i of itemList)

                end if

                end repeat

                end repeat

                 

                repeat with i from 1 to (count itemLayerBreakdown) -- to 1 by -1

                if item i of itemLayerBreakdown is not {} then

                select item i of itemLayerBreakdown

                move selection to item i of theDocumentLayerList

                end if

                end repeat

                select itemList

                end tell

                 

                set AppleScript's text item delimiters to oldDelims as text

                • 5. Re: How to give commands to items of a list (Applescript)
                  sstanleyau Level 4

                  You might consider using insert label instead -- a bit more effort, but more robust than names. OTOH, if you're happy with names you can do it a bit more easily. Have a look at these snippets:

                   

                  tell application "Adobe InDesign CS5"

                  set usedNames to {}

                  set theNames to name of item layer of items of selection

                  repeat with i from 1 to count of theNames

                  set aName to item i of theNames

                  if usedNames does not contain aName then

                  set name of (every item of selection whose name of item layer is aName) to aName

                  set end of usedNames to aName

                  end if

                  end repeat

                  end tell

                   

                   

                  tell application "Adobe InDesign CS5"

                  set theItems to page items of selection

                  ungroup selection

                  set selection to theItems

                  set usedNames to {}

                  set theNames to name of items of selection

                  repeat with i from 1 to count of theNames

                  set aName to item i of theNames

                  if usedNames does not contain aName then

                  log (get every item of selection whose name is aName)

                  set item layer of (every item of selection whose name is aName) to layer aName of document 1

                  set end of usedNames to aName

                  end if

                  end repeat

                  end tell

                  • 6. Re: How to give commands to items of a list (Applescript)
                    Stephen Horne Level 1

                    These are a bit tidier, thanks Shane. The second one doesn't quite work as is, because of the "set item layer of (every item of selection whose name is aName) to layer aName of document 1", this muddles up the ordering of the page items for some reason. After much messing around with reversing the list before setting the item layer of the items within, I realised that a "move selection to layer…" moved them all without messing up their relative indexes.

                     

                    When you say insert label, is that a document label, rather than a per-item label? I've had a look in the dictionary and found the insert label command, is this what you mean? Create a document-based list of groups and their items?

                    --

                    Stephen Horne

                    • 7. Re: How to give commands to items of a list (Applescript)
                      sstanleyau Level 4

                      When you say insert label, is that a document label, rather than a per-item label?

                      No, you can have them on items. They are key-based -- you can set as many as you like for an item -- and they don't appear anywhere in the UI. That means you have to know the key to retrieve or overwrite them. They're a bit more work to use, but they are much more robust.

                      • 8. Re: How to give commands to items of a list (Applescript)
                        Stephen Horne Level 1

                        Great! I'm just reading up on this now. Exciting stuff!

                         

                        Thanks for the pointers.