1 Reply Latest reply on Dec 6, 2010 7:30 AM by JohnDevlon

    Identifying and Replacing Tagged Text

    mlavie Level 1

      I have a TextFrame full of text. Different portions of the text are marked with different Tags.

       

      I want to identify the various runs of text by the Tag which marked them, and replace the content of that Tagged run of text according to its Tag using my business logic.

       

      For example, for the tags

       

      Tag1

      Tag2

      Tag3

       

      And the following Tagged text (I am including the square brackets which InDesign adds around Tagged text runs)

       

      [Vini][Vidi][Vici]

       

      I would like to be able to identify that

       

      "Vini" is marked by Tag1 (and should be replaced by "Ars")

      "Vidi" is marked by Tag2 (and should be replaced by "Gratia")

      "Vici" is marked by Tag3 (and should be replaced by "Artis")

       

      And then perform the replacement, giving:

       

      [Ars][Gratia][Artis]

       

      How would I go about this?

        • 1. Re: Identifying and Replacing Tagged Text
          JohnDevlon

          Hi,

           

          look at the xmlelements ... that's the key to succes...

           

          What i would try to do is ...

           

          1. Loop over every PageItem

          2. Retreive the associated XML element

          3. Do a recursive search within all sub xmlelements

          4. check the name of every XML element

          5. If it's the correct XML element, change the content property to the new value

           

           

          for (var b = 0; b < myDoc.allPageItems.length; b++){

                 var myXMLelement = myDoc.allPageItems[b].associatedXMLElement;

           

                ProcessXMLelement(myXMLelement);                           

          }

           

           

          function ProcessXMLelement(elm){

           

                 // Get the name of the Tag

                myTagName = elm.markupTag.name.toString();

           

               if (myTagName == "myTag"){

                         elm.contents = "New Text";

               }

           

                 // Process all sub elements

                 for (var i = 0; i < elm.xmlElements.length; i++){

                      ProcessXMLelement(elm.xmlElements[i]); 

                 }     

          }

           

           

          Hopefully this helps

           

          John