4 Replies Latest reply on Aug 27, 2018 6:35 AM by s.jonsson

    Indesign tagging text with two tags

    s.jonsson

      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?

            • 3. Re: Indesign tagging text with two tags
              Loic.Aigon Adobe Community Professional
              var main = function() {
              
              var doc = app.properties.activeDocument,
              a, n, fgp = app.findGrepPreferences.properties, t, c;
              
              if ( !doc ) {
              alert("You need an open document" );
              return;
              }
              
              app.findGrepPreferences = null;
              
              //Tagging paras
              tagContent ( doc, {findWhat:".+"}, 'p' );
              
              //Tagging Bold contents (only Bold, not Bold Italic…)
              tagContent ( doc, {findWhat:".+", fontStyle:"Bold"}, 'b' );
              
              app.findGrepPreferences.properties = fgp;
              }
              
              
              function tagContent (doc, searchProps, tagName ) {
              var a, n, t, c;
              
              app.findGrepPreferences.properties = searchProps
              
              a = doc.findGrep();
              n = a.length;
              while(n--) {
              t = a[n];
              c = t.parentStory.textContainers[0];
              !c.associatedXMLElement && c.autoTag();
              c.associatedXMLElement.xmlElements.add(tagName, t );
              }
              
              }
              
              
              var u;
              
              
              app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
              
              • 4. Re: Indesign tagging text with two tags
                s.jonsson Level 1

                This is great!

                 

                But... if I have a story with a grouped object it breaks, because Indesign cannot tag this.

                 

                I already have a function fixing this (adding a tag right before the group)

                 

                But to be able to use your soultion I need to be able to run it even if there are groups in a story.