8 Replies Latest reply on Oct 17, 2018 4:32 AM by deckarduk

    Tag Markers query...

    deckarduk Level 1

      Please can someone tell me the best way to access the tags applied to an item or text.

       

      I have a page that contains tagged items and tagged text on it.

      I can see the tags when I have 'View Structure', 'View Tagged Frames' and 'View Tag Markers' switched on.

      I can also see tags when i view a story in 'Story Editor'.

       

      I've tried a few things, and read quite a few posts, but as yet I'm not achieving what I'd like.

      If I place the cursor at the start of a text box containing tagged copy, and move through it a character at a time using the right arrow key, I can see the assigned tags change in the 'Tags' palette.

      I'd like to be able to do the same using Javascript, I'm trying to pick up the start and end tags. Please can someone point me in the right direction.

       

      Thanks in advance.

        • 1. Re: Tag Markers query...
          Manan Joshi Level 4

          You could try something like the following, this will traverse the whole document structure and print the tag name and its content on the console.

          var doc = app.activeDocument
          var rootXMLElement = doc.xmlElements[0]
          traverseXMLElement(rootXMLElement)
          function traverseXMLElement(xmlElement)
          {
            $.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content :" + xmlElement.contents)
            for(var i = 0; i < xmlElement.xmlElements.length; i++)
            traverseXMLElement(xmlElement.xmlElements[i])
          }
          

          P.S. :- The code is rough draft with no error checking and handling for tags containing images.

           

          -Manan

          • 2. Re: Tag Markers query...
            deckarduk Level 1

            Thank you for the help and for the code, it's really useful and doing pretty much what I wanted.

            It’s interesting seetng your solution to the query.

             

            Thanks once again.

            • 3. Re: Tag Markers query...
              deckarduk Level 1

              Just another quick query to see if I'm understanding the code correctly.

              It seems to dynamically generate a nested loop depending on the number xml elements within the root structure?

               

              Have I got that correct?

               

              Thanks.

              • 4. Re: Tag Markers query...
                Manan Joshi Level 4

                Yes that is correct, it traverses the whole xml structure of the document starting from the Root.

                • 5. Re: Tag Markers query...
                  deckarduk Level 1

                  Thanks Manan.

                  • 6. Re: Tag Markers query...
                    deckarduk Level 1

                    Thanks once again for the original code. Working forward with that I now have something working pretty much as i want.

                    In your original post you did mention something about 'handling for tags containing images'. I'm trying to incorporate something takes these into account.

                    I've set up a very simple document with a text box tagged as a story. Within that text box is an image that is tagged as an artifact. If I execute the code below I can see the artifact tag ok.

                     

                    var doc = app.activeDocument
                    var rootXMLElement = doc.xmlElements[0];
                    $.writeln (rootXMLElement.xmlElements[0].xmlElements[0].markupTag.name);
                    
                    

                     

                    Please can you tell me what direction I should be looking to incorporate something that handles images.

                    Why is it that the original code doesn't spot tagged images? Should I be looking at a different part of the XML structure?

                     

                    Thanks in advance.

                    • 7. Re: Tag Markers query...
                      Manan Joshi Level 4

                      The reason i said that the code does not handle images is that for an image the property contents would not yield any results. You would be able to get the markuptag name fine for image tags but the content value would be blank, in order to handle images you could try something like below

                      var doc = app.activeDocument  
                      var rootXMLElement = doc.xmlElements[0]  
                      traverseXMLElement(rootXMLElement)  
                      function traverseXMLElement(xmlElement)  
                      {
                        if(xmlElement.contents != "")
                             $.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content :" + xmlElement.contents) 
                        else
                        {
                              if(xmlElement.graphics.length > 0)
                              {
                                  var grp = xmlElement.graphics[0]
                                  $.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content : Image Name :" + xmlElement.graphics[0].itemLink.name) 
                              }
                        }
                        for(var i = 0; i < xmlElement.xmlElements.length; i++)  
                             traverseXMLElement(xmlElement.xmlElements[i])  
                      } 
                      

                      In the above code i check if the contents is blank, if it then i check if the tag indeed contains an image or not, if it does i get the graphic object and then get the name from the corresponding link object. This would give you a gist of the object model hierarchy in this case, now you can extend this to your purpose.

                       

                      -Manan

                      • 8. Re: Tag Markers query...
                        deckarduk Level 1

                        Hi Manan,

                         

                        That's great, thanks for the help and for once again expanding my knowledge of InDesign JS.

                         

                        The code I've created so far has a try block which, I think, effectively bypasses the images.

                        Your code has has illustrated I need to treat the graphics differently.

                         

                        The only slight problem I have at the moment is getting the baseline and horizontalOffset of the frame containing the graphic.

                        I'm pretty sure I've referred to the correct item in my code because I'm getting an artifact tag. The only thing is it doesn't seem to have the 2 properties I'm after.

                         

                        Once again, thank you for your help Manan.