3 Replies Latest reply on Sep 23, 2015 5:04 AM by AdamVe

    Strange behavior with "insertTextAsContent"




      I've got a strange behavior with this loop :


      for (j = 0; j < myParagraph.characters.length-1; j++) {

           var myCharacter = myParagraph.characters[j];

           if (myCharacter.appliedFont.fontStyleName == "Regular") {


                myXmlElement.insertTextAsContent(myCharacter.contents, XMLElementPosition.ELEMENT_END);




      I loop through every characters of a paragraph, test if they are "regular", "italic", "bold"..., and store them in different xmlElement. Everything is fine except that every double characters (in french : homme, cette, elle, ville...) output in my xmlElement as one only character (home, cete, ele, vile...). I try with "aaaaaazzzzzzeeeeeerrrrrtttttyyyyy" and it give me, the same way, "azerty" ?!


      The "alert" line output the correct full sentences character by character ("h", "o", "m", "m", "e"...).


      If you have any idea to explain, you are more than welcome




      ** edit : don't know if it's important but I'm using CS6 **

        • 1. Re: Strange behavior with "insertTextAsContent"
          Daniel Sterchi Adobe Community Professional



          Have you seen this in the object model viewer?


          XMLElement.insertTextAsContent (using:varies, position: XMLElementPosition ): Text

          Adobe InDesign CC (8.0) Object Model

          Inserts the specified text as content before, in, or after the XML element.

          using: Data Type: varies

          The text to be inserted. Can accept: String or SpecialCharacters enumerator.

          position: Data Type: XMLElementPosition

          The position at which to insert the text. Note that text inserted before or after the element does not become part of the content of the element. Instead, it becomes content of the parent of the element.


          Don't ask me why it works when there is no doubled character


          So i tried the following and it worked. I hope this will solve your problem.


          ****************************************************************************************** **********************

          function main() {


               if (app.documents.length != 0) {


                  var myDoc = app.activeDocument;

                  var myParagraph = myDoc.textFrames[0].paragraphs[0];

                  var myXmlElement  = myDoc.xmlElements[0];


                  for (j = 0; j < myParagraph.characters.length-1; j++) {

                      var myCharacter = myParagraph.characters[j];


                      if (myCharacter.appliedFont.fontStyleName == "Regular") {


                          myXmlElement.contents += myCharacter.contents;

                          alert(">" + myXmlElement.contents+ "<");

                      }     // if


                  }     //for

              }     // if

          }   // main()


          ****************************************************************************************** **********************


          Kind regards


          Dani (from Switzerland)

          1 person found this helpful
          • 2. Re: Strange behavior with "insertTextAsContent"
            LeftHead Level 1

            Hi Dani,

            Sorry for late reaction but I was on something completely different


            Yes and No for your answer: I didn't use the "before" or "after" position but the "ELEMENT_END" that should do the job with or without double letters. I must miss something logical or maybe is it a bug with this method.


            Regarding your solution, I found it by myself some days ago but (another strange behavior ) when using it some characters (like unbreakable space or some quotes characters) were convert to their "specialCharacter" numbers (1397645907 for nonbreaking space for exemple).


            As I didn't found any clean solution, I'm using for the moment this "ugly trick":


            // ajout du caractère traité

            if (myParagraph.characters[j].contents != myParagraph.characters[j + 1].contents) {

                myXmlElement.insertTextAsContent(myParagraph.characters[j].contents, XMLElementPosition.ELEMENT_END);

            } else {

                // correction du bug de "insertTextAsContent avec les lettres doublées

                var myBugCorrection = myParagraph.characters[j].contents + myParagraph.characters[j].contents; // attention si la lettre répétée a un "style" différent, risque de "tripler" la lettre

                myXmlElement.insertTextAsContent(myBugCorrection, XMLElementPosition.ELEMENT_END);



            Not really good but it works now and I will try to find something better for the next iteration.


            Thanks anyway for the help



            • 3. Re: Strange behavior with "insertTextAsContent"


              two years later I have still the same problem with insertTextAsContent in InDesign CC 2015.


              I used the following fix:


              var beforeContents = htmlSpan.contents;

              htmlSpan.insertTextAsContent(ch.contents, XMLElementPosition.ELEMENT_END);

              var afterContents = htmlSpan.contents;

              if(beforeContents == afterContents)

                   htmlSpan.contents += ch.contents;