2 Replies Latest reply on Aug 17, 2018 4:38 AM by s.jonsson

    Indesign tagging text with two tags

    s.jonsson Level 1

      Hi,

       

      I need to tag a text twice in extendscript.

      Example:

       

      A simple example below:

       

      var textStyleRanges = app.selection[0].texts[0].textStyleRanges.everyItem().getElements(); //I have a textFrame Selected
      app.activeDocument.xmlElements.item(0).xmlElements.add ("Story", app.selection[0]); //I tag the frame with Story
      
      app.activeDocument.xmlElements.item(0).xmlElements.add ("Tag1", textStyleRanges[0]); //I add Tag1
      app.activeDocument.xmlElements.item(0).xmlElements.add ("Tag2", textStyleRanges[0]); //I would like to add Tag2 inside Tag1
      

       

      Now I need to add another tag inside Tag1 for the same text so it looks like this

      <Tag1><Tag2>My text</Tag2></Tag1>
      

       

      The problem is, when adding Tag2, The first tag gets replaced with Tag2 so it looks like this:

      <Tag2>My text</Tag2>
      

       

      I Understand why, but not how to solve it like I would like.

       

      Any suggestions?

        • 1. Re: Indesign tagging text with two tags
          Loic.Aigon Adobe Community Professional

          As you tag, references change (added tag characters push indeces away). Reference again your tag.

           

          var tf = app.selection[0];
          var txt = tf.parentStory.texts[0];
          var doc = app.activeDocument;
          var root = doc.xmlElements.item(0);
          
          
          root.xmlElements.add ("Story", tf);
          txt = root.xmlElements.add ("Tag1", txt ).xmlContent.texts[0];
          root.xmlElements.add ("Tag2", txt );
          

           

          Out of check on the context (sel, story…).

          • 2. Re: Indesign tagging text with two tags
            s.jonsson Level 1

            Thanks Loic,

             

            I understand what you mean, but...

            How can i make that work here?

             

            //TAG ALL PARAGRAPHS WITH "ParaText"
            var paraArr = app.activeDocument.stories[0].tables[0].cells.everyItem().paragraphs.everyItem().getElements();
            for (var x = paraArr.length - 1; x >= 0; x--){
                app.activeDocument.xmlElements.item(0).xmlElements.add ("ParaText", paraArr[x].paragraphs[0]);
            }
            
            //TAG ALL THAT HAS FONTSTYLE BOLD USING TEXTSTYLERANGES
            var paraArr = app.activeDocument.stories[0].tables[0].cells.everyItem().texts.everyItem().getElements();
            for (var x = paraArr.length - 1; x >= 0; x--){
                var textStyleRanges = paraArr[x].textStyleRanges.everyItem().getElements();
                for (var y = textStyleRanges.length - 1; y >= 0; y--){
                    if (textStyleRanges[y].fontStyle == "Bold"){
                        app.activeDocument.xmlElements.item(0).xmlElements.add ("Bold", textStyleRanges[y])];
                    }
                }
            }
            
            

             

            I think the main problem occurs when also the linebreak is in Bold.

            Example:

            My bold text with the linebreak Bold

            Some other text

             

             

            If I remove the Bold from the linebreak, it works OK.

            I have a solution like this:

             

            if (textStyleRanges[x].characters[-1].contents == String.fromCharCode(13)){
                 var myNewRange = textStyleRanges[x].characters.itemByRange(0,-2);
            }
            

             

            But I think it's an ugly solution....

             

            Any ideas?