3 Replies Latest reply on Jun 6, 2012 5:36 AM by Loic.Aigon

    Access DOM from context menu (javascript)

    wetv

      Hi,

       

      I' trying to create a script to extend InDesign.

      Here is my scenario:

      1. Open InDesign file
      2. Import XML data in the layout
      3. For one item in the XML tree, in a context menu, load external data from a custom webservice.

       

      I'm able to create the context menu, but what I don't know how to do is to get the right XML node when focusing on an item from the layout.

       

      Does someone know how to access DOM with Javascript script?

       

      Thanks.

      Lionel

        • 1. Re: Access DOM from context menu (javascript)
          Loic.Aigon Adobe Community Professional

          Hi Lionel,

           

          You can reach the xmlElement from the layout item thanks to the associatedXMLElement.

           

          Given a tagged item in the layout :

           

           

          if ( item.associatedXMLElement != null )
          {
               //do sth
          }
          

           

          Loic

          http://www.loicaigon.com

          1 person found this helpful
          • 2. Re: Access DOM from context menu (javascript)
            wetv Level 1

            Thanks Loïc,

             

            I'm very new to scripting InDesign, how can I get the item object?

             

            We can switch to French if you prefer...

             

            Thanks again.

            Lionel

            • 3. Re: Access DOM from context menu (javascript)
              Loic.Aigon Adobe Community Professional

              Hi Lionel,

               

              It really depends on what you want to target. If your item is at the first level ( not into a group ), you can adress it either by its index or its name or its label if any:

               

              var pi = app.activeDocument.pageItems[0]

              or

              var pi = app.activeDocument.pageItems.itemByName ( "foo" ); //given that the name of the object is foo in the layer palette.

              or

              var pi = app.activeDocument.pageItems.itemByID ( 123 ); //given that the id of the object is 123.

               

              if you want to adress an object with a specific label, it's longer process. You have to loop every pageItems and compare their label.

               

              Also if you know the nature of the object, you may target the collection directly :

              var tf = pp.activeDocument.textFrames[0];

               

              On the contrary if your intention is to work on the xml node, you may also loop through the xml structure.

               

              To be honest, there are several ways to work in your case.

               

              In any cases, don't hesitate to read the InDesign Scripting Guide which would answer most of these topics

               

              Loic

               

              PS: I prefer stick to my bad english as english spoken people may need this info