25 Replies Latest reply on Oct 22, 2013 6:28 AM by KyranOB

    select text frame from XML structure

    KyranOB

      I am attempting to move specific text frames created though an XML import to the bottom of the page (bottom pasteboard) as Folio and slug info.

       

      I can select the text associated with an XML element "VTag" but not the Text frame. Any help would be appreciated.

       

      Thanks

       

       

      property myHoHoV : {}
      on myLoopLoop(myElement)
         
          tell application "Adobe InDesign CS4"
              tell active document
                  set moreElement to every XML element of myElement
                  repeat with x from 1 to (count of moreElement)
                      set em1 to item x of moreElement
                      select em1
                      if (name of markup tag of em1 is "VTag1") then
                          set em2 to em1
                          set properties of view preferences to {horizontal measurement units:inches, vertical measurement units:inches, ruler origin:page origin}
                          set transform reference point of layout window 1 to top left anchor
                          select text of em2
                          tell application "Adobe InDesign CS4"
                              move text of em2 to {5, 5}
                          end tell
                      end if
                      tell me to myLoopLoop(item x of moreElement)
                  end repeat
              end tell
          end tell
      end myLoopLoop

        • 1. Re: select text frame from XML structure
          MANiSUBa

          Hi Kyran,

           

          I have used XPath concept for placing the specific XML tagged contents into a new text frame.

          Also I have moved this text frame onto the slug area as per your requirement.

           

          Please change the geometric bounds as per your requirement.

          You can also use object style along with anchor object concept for performing this text frame moment.

           

          global myGlueCode
          on run
              tell application "Adobe InDesign CS4"
                  set myRootXML to first XML element of document 1
                  set myApplicationPath to file path
                  set myFilePath to file path as string
                  set myFilePath to myFilePath & "Scripts:Xml rules:glue code.scpt"
                  set myGlueCode to load script file myFilePath
                  set myRuleSet to {my VTag()}
              end tell
              tell myGlueCode
                  __processRuleSet(myRootXML, myRuleSet, {})
              end tell
          end run

           

          to VTag()
              script VTag
                  property name : "VTag"
                  property xpath : "//VTag"
                  on apply(myXMLElement, myRuleProcessor)
                      global myReturnString
                      set myReturnString to "OK"
                      tell application "Adobe InDesign CS4"
                          set myDocument to document 1
                          set myPage to page 1 of myDocument
                          set myTextFrame to text frame 1 of myPage
                          set pht to page height of document preferences of document 1 as string
                          tell myXMLElement
                              set myFrame to place into copy on myPage place point {"43p", "5p"} copy item myTextFrame
                              set geometric bounds of myFrame to {"0p", "0p", "5p", "43p"}
                              move myFrame to {0, pht}
                          end tell
                      end tell
                      return false
                  end apply
              end script
          end VTag

           

           

          Regards

          MANi

          • 2. Re: select text frame from XML structure
            KyranOB Level 1

            Thanks so much for the help. Problem is I get the attached error

             

            Applescript Error

            invalid Structure iterator for the current XML rules

            Processor deleted

            Please modify the rules so the structural changes happen after the children nodes have been processed (or skipped)

             

            and then the VTAG XML element is deleted.

            Any ideas on how to fix?

            Thanks again

             

            Kyran O'Brien

            Senior Systems Specialist

            The Bon-Ton Stores, Inc.

            331 W. Wisconsin Ave.

            Milwaukee, WI 53203

            414-347-1152 ext. 3308

            • 3. Re: select text frame from XML structure
              MANiSUBa Level 1

              Please try this below mentioned code:

               

              global myGlueCode
              on run
                  tell application "Adobe InDesign CS4"
                      set myRootXML to first XML element of document 1
                      set myApplicationPath to file path
                      set myFilePath to file path as string
                      set myFilePath to myFilePath & "Scripts:Xml rules:glue code.scpt"
                      set myGlueCode to load script file myFilePath
                      set myRuleSet to {my VTag()}
                  end tell
                  tell myGlueCode
                      __processRuleSet(myRootXML, myRuleSet, {})
                  end tell
              end run

               

              to VTag()
                  script VTag
                      property name : "VTag"
                      property xpath : "//VTag"
                      on apply(myXMLElement, myRuleProcessor)
                          global myReturnString
                          set myReturnString to "OK"
                          tell application "Adobe InDesign CS4"
                              set myDocument to document 1
                              set myPage to page 1 of myDocument
                              set myTextFrame to text frame 1 of myPage
                              set pht to page height of document preferences of document 1 as string
                              try
                                  tell myXMLElement
                                      set myFrame to place into copy on myPage place point {"43p", "5p"} copy item myTextFrame
                                      set geometric bounds of myFrame to {"0p", "0p", "5p", "43p"}
                                      move myFrame to {0, pht}
                                  end tell
                              on error myError
                                  set myReturnString to myError
                              end try
                          end tell
                          return false
                      end apply
                  end script
              end VTag

               

              If still the problem exists then please attach me a sample of your InDesign document to my mail id (mani.170882@gmail.com).

               

              Thanks & Regards

              MANi

              • 4. Re: select text frame from XML structure
                MANiSUBa Level 1

                Change the tag name as "VTag1" in the script if your tag name is VTag1

                • 5. Re: select text frame from XML structure
                  John Hawkinson Level 5

                  I am attempting to move specific text frames created though an XML import to the bottom of the page (bottom pasteboard) as Folio and slug info.

                   

                  I can select the text associated with an XML element "VTag" but not the Text frame. Any help would be appreciated.

                  You should be able to look at the parent of the text.

                   

                  You would also be able to select the text frame referenced by a tag if the tag was applied to the frame directly, rather than to text within the frame.

                   

                  I would really recommend avoiding the XML Rules Processor. It adds a lot of complexity.

                   

                  Deleting your personal information from your email signature is wise before replying, because otherwise it is posted in the web forum for all to see.

                  • 6. Re: select text frame from XML structure
                    KyranOB Level 1

                    Thanks for the deleting persona; info heads up.

                    • 7. Re: select text frame from XML structure
                      KyranOB Level 1

                      how do I reference the tag via the frame? I would love to avoid the XML structure if I can.

                      • 8. Re: select text frame from XML structure
                        MANiSUBa Level 1

                        Modifiy the line     property xpath : "//VTag"     to         property xpath : "//VTag/VTag1" in my provide script. It works fine for me.

                        • 9. Re: select text frame from XML structure
                          KyranOB Level 1

                          Great thanks

                          • 10. Re: select text frame from XML structure
                            MANiSUBa Level 1

                            If this script has worked out for you, then please choose the correct answer or helpful answer option.

                             

                            This discussion post will be still open for others discussions and shows the status icon as discussion icon rather than the answered icon.

                            So always please do the needful (checking the respective option) when every you get the correct or helpful anwser from any person.

                             

                            Regards

                            MANi

                            1 person found this helpful
                            • 11. Re: select text frame from XML structure
                              John Hawkinson Level 5

                              how do I reference the tag via the frame? I would love to avoid the XML structure if I can.

                              Sorry for the delay.

                              tags.png

                              On the left, the frame is tagged. On the right the text inside the frame is tagged. To get the first case, you select the frame with the selection tool before clicking on the tag to apply the tag.

                              • 12. Re: select text frame from XML structure
                                KyranOB Level 1

                                Thanks, I know how to get that info but not how to use it in Applescript.

                                It errors saying can't get Text Frame whose XML Tag " Name of document 1" =

                                "folio1"

                                 

                                See below.

                                tell application "Adobe InDesign CS4"

                                    set myPageHeight to page height of document preferences of document 1

                                    set myPageWidth to page width of document preferences of document 1

                                    set myOldRulerOrigin to ruler origin of view preferences of document 1

                                    set ruler origin of view preferences of document 1 to page origin

                                    set zero point of document 1 to {0, 0}

                                    set myPage to active page of active window

                                    tell document 1

                                        select item (text frame whose XML tag name = "Folio1")

                                        move (text frame whose XML tag name = "Folio1" as text) to {0.25,

                                myPageHeight + 0.125}

                                        end tell

                                        select item (text frame whose XML tag name = "Folio1")

                                        move (text frame whose XML tag name = "VTag1" as text) to {4,

                                myPageHeight + 0.125}

                                        end tell

                                    end tell

                                     

                                end tell

                                • 13. Re: select text frame from XML structure
                                  John Hawkinson Level 5

                                  I'm really a JavaScript person, so I'm not going to offer you a canned example, but the problem is that text frames don't have XML tags.

                                  They have associatedXMLElements that have XML tags.

                                  • 14. Re: select text frame from XML structure
                                    MANiSUBa Level 1

                                    Hi Kyran,

                                     

                                    Please check whether the below script meets your requirement.

                                    I have also attached the screenshot of the InDesign document in which I have tested my below mentioned script.

                                     

                                    Picture 2.png

                                     

                                     

                                    tell application "Adobe InDesign CS4"
                                        activate
                                        set myPageHeight to page height of document preferences of document 1
                                        set myPageWidth to page width of document preferences of document 1
                                        set myOldRulerOrigin to ruler origin of view preferences of document 1
                                        set ruler origin of view preferences of document 1 to page origin
                                        set zero point of document 1 to {0, 0}
                                        set myPage to active page of active window
                                        tell document 1
                                            set pgcnt to count of pages
                                            repeat with p from 1 to pgcnt
                                                tell page p
                                                    set txtframe to count of text frames
                                                    repeat with f from 1 to txtframe
                                                        set paracnt to count of paragraphs of text frame f
                                                        tell text frame f
                                                            set tagname to {}
                                                            repeat with ap from 1 to paracnt
                                                                set end of tagname to (name of markup tag of (item 2 of associated XML elements) of paragraph ap) as string
                                                            end repeat
                                                            if ((count of tagname) is equal to 1) and (every item of tagname contains "VTag1") then
                                                                select
                                                                move to {0.125, myPageHeight + 0.125}
                                                            else
                                                                select nothing
                                                            end if
                                                        end tell
                                                    end repeat
                                                end tell
                                            end repeat
                                        end tell
                                        activate
                                        display dialog "Process Completed..." with icon 1
                                    end tell

                                     

                                    Please revert for further updates if any.

                                     

                                    Regards

                                    MANi

                                    1 person found this helpful
                                    • 15. Re: select text frame from XML structure
                                      KyranOB Level 1

                                      Thanks for the code MANi. It is so close

                                       

                                      I get the following error however.

                                       

                                      Can’t make name of «class XMLt» of item 2 of «class Xans» of paragraph 1 of «class txtf» 1 of «class page» 1 of document 1 of application "Adobe InDesign CS4" into type string.

                                      • 16. Re: select text frame from XML structure
                                        MANiSUBa Level 1

                                        Hi Kyran,

                                         

                                        I have updated the code as per the sample InDesign document which you have provided me earlier (as attached in the below screenshot).

                                        It is working fine for me. Please revert for further assistance if any. Please provided me the actual InDesign document where you are getting this error.

                                         

                                        Picture 1.png

                                         

                                        tell application "Adobe InDesign CS4"

                                            activate
                                            set myPageHeight to page height of document preferences of document 1
                                            set myPageWidth to page width of document preferences of document 1
                                            set myOldRulerOrigin to ruler origin of view preferences of document 1
                                            set ruler origin of view preferences of document 1 to page origin
                                            set zero point of document 1 to {0, 0}
                                            set myPage to active page of active window
                                            tell document 1
                                                set pgcnt to count of pages
                                                repeat with p from 1 to pgcnt
                                                    tell page p
                                                        set txtframe to count of text frames
                                                        repeat with f from 1 to txtframe
                                                            set paracnt to count of paragraphs of text frame f
                                                            tell text frame f
                                                                set tagname to {}
                                                                repeat with ap from 1 to paracnt
                                                                    set end of tagname to (name of markup tag of (item 1 of associated XML elements) of paragraph ap) as string
                                                                end repeat
                                                                if ((count of tagname) is equal to 1) and (every item of tagname contains "VTag1") then
                                                                    select
                                                                    move to {0.125, myPageHeight + 0.125}
                                                                else
                                                                    select nothing
                                                                end if
                                                            end tell
                                                        end repeat
                                                    end tell
                                                end repeat
                                            end tell
                                            activate
                                            display dialog "Process Completed..." with icon 1
                                        end tell

                                         

                                        Regards

                                        Mani

                                        • 17. Re: select text frame from XML structure
                                          MANiSUBa Level 1

                                          Hi Kyran,

                                           

                                          If the earlier code has some problem, then try this below code.

                                           

                                          tell application "Adobe InDesign CS4"
                                              activate
                                              set myPageHeight to page height of document preferences of document 1
                                              set myPageWidth to page width of document preferences of document 1
                                              set myOldRulerOrigin to ruler origin of view preferences of document 1
                                              set ruler origin of view preferences of document 1 to page origin
                                              set zero point of document 1 to {0, 0}
                                              set myPage to active page of active window
                                              tell document 1
                                                  set pgcnt to count of pages
                                                  repeat with p from 1 to pgcnt
                                                      tell page p
                                                          set txtframe to count of text frames
                                                          repeat with f from 1 to txtframe
                                                              set paracnt to count of paragraphs of text frame f
                                                              tell text frame f
                                                                  try
                                                                      set tagname to {}
                                                                      repeat with ap from 1 to paracnt
                                                                          set end of tagname to (name of markup tag of (item 1 of associated XML elements) of paragraph ap) as string
                                                                      end repeat
                                                                      if ((count of tagname) is equal to 1) and (every item of tagname contains "VTag1") then
                                                                          select
                                                                          move to {0.125, myPageHeight + 0.125}
                                                                      else
                                                                          select nothing
                                                                      end if
                                                                  end try
                                                              end tell
                                                          end repeat
                                                      end tell
                                                  end repeat
                                              end tell
                                              activate
                                              display dialog "Process Completed..." with icon 1
                                          end tell

                                           

                                          Regards

                                          MANi

                                          • 18. Re: select text frame from XML structure
                                            KyranOB Level 1

                                            This works great!

                                            Thanks

                                            • 19. Re: select text frame from XML structure
                                              KyranOB Level 1

                                              MANi shouldn't i be able to simply change "VTag1" to "Folio1" or "Textbox1" and controll those also? Nothing happens when I do. No error but no movement. My goal is to control all element via the XML structure. Is it because these XML elements have atributes in the XML structure between the select and the parent?

                                              Thanks again

                                              • 20. Re: select text frame from XML structure
                                                MANiSUBa Level 1

                                                Hi Kyran,

                                                 

                                                I have checked your InDesign document with the below mentioned script, it is working for me.

                                                Please check and revert for further assistance if any.

                                                 

                                                 

                                                tell application "Adobe InDesign CS4"
                                                    activate
                                                   
                                                    set myPageHeight to page height of document preferences of document 1
                                                    set myPageWidth to page width of document preferences of document 1
                                                    set myOldRulerOrigin to ruler origin of view preferences of document 1
                                                   
                                                    set ruler origin of view preferences of document 1 to page origin
                                                    set zero point of document 1 to {0, 0}
                                                    set myPage to active page of active window
                                                   
                                                    tell document 1
                                                        set pgcnt to count of pages
                                                        repeat with p from 1 to pgcnt
                                                            tell page p
                                                                set txtframe to count of text frames
                                                                repeat with f from 1 to txtframe
                                                                    set paracnt to count of paragraphs of text frame f
                                                                    tell text frame f
                                                                        try
                                                                            set tagname to {}
                                                                            repeat with ap from 1 to paracnt
                                                                                set end of tagname to (name of markup tag of (item 1 of associated XML elements) of paragraph ap) as string
                                                                            end repeat
                                                                            if ((count of tagname) is equal to 1) and (every item of tagname contains "VTag1") then
                                                                                select
                                                                                move to {0.125, myPageHeight + 0.125}
                                                                            else if ((count of tagname) is equal to 1) and (every item of tagname contains "Folio1") then
                                                                                select
                                                                                move to {0.125, myPageHeight + 0.125}
                                                                            else if ((count of tagname) is equal to 1) and (every item of tagname contains "TextBox3") then
                                                                                select
                                                                                move to {0.125, myPageHeight + 0.125}
                                                                            else
                                                                                select nothing
                                                                            end if
                                                                        end try
                                                                    end tell
                                                                end repeat
                                                            end tell
                                                        end repeat
                                                    end tell
                                                   
                                                    activate
                                                    display dialog "Process Completed..." with icon 1
                                                end tell

                                                 

                                                 

                                                Regards

                                                MANi

                                                • 21. Re: select text frame from XML structure
                                                  MANiSUBa Level 1

                                                  Hi Kyran,

                                                   

                                                  Below is the screenshot of the InDesign document which I used for testing.

                                                  Picture 4.png

                                                   

                                                  Regards

                                                  MANi

                                                  • 22. Re: select text frame from XML structure
                                                    KyranOB Level 1

                                                    This works great!

                                                     

                                                    I figured it out. It was a simple problem with using "greater than" instead

                                                    of "equal to" for the Textbox1 as there was more than 1 in my doc.

                                                     

                                                    Thank You so much. We use a third party application that has a distinct XML

                                                    structure that loads the InDesign document. Now I can control all the

                                                    elements coming over.

                                                     

                                                    This will be a great asset once I figure out how I want to use it (and

                                                    figure out the correct syntax for the graphic files also).

                                                    Thanks again

                                                    • 23. Re: select text frame from XML structure
                                                      KyranOB Level 1

                                                      MANi asny thoughts on how I would be able to sellect the images instead of the text using this base script. Everything I try fails.

                                                      • 24. Re: select text frame from XML structure
                                                        MANiSUBa Level 1

                                                        Do you want an applescript to select the images (links) in the InDesign document.

                                                        Please let me know the purpose for your image selection.

                                                        • 25. Re: select text frame from XML structure
                                                          KyranOB Level 1

                                                          The goal is to only select both text and images that are in item positions in the XML structure and move them to X and Y coordinates, and resize each individualy

                                                           

                                                          I am trying to take all the images and text from the XML import and create a layout to a pre-determined grid.

                                                           

                                                          That way I can create an App that has a series of buttons for 2x4 layouts, 3x4 layouts and a 2x3 layout etc.

                                                           

                                                          So basically it is a level above where we are in the current script (any text frame and any image for Item position 1 for example)