2 Replies Latest reply on Jun 18, 2010 1:32 AM by Dr. TYPO

    adding text to parentStory destroys XML element / JavaScript, InD CS4

    Dr. TYPO Level 1

      Dear scripters,

       

      I'm currently stuck with this problem:

       

      - I'm writing text to text frames on a layout page

      - while writing some characters/strings are XML tagged (I'm creating an element)

      - I can do this only once, because adding more text after I have created an XML element destroys the link to the element

       

      Here's my sample:

       

      var MyBox = app.activeDocument.textFrames.add();
      MyBox.geometricBounds = ["0pt","0pt", "200pt", "200pt"];

       

      //Fill some content into the text frame
      MyBox.parentStory.contents = "bla";

       

      //create a xml element for some of the text
      app.activeDocument.xmlElements.item(0).xmlElements.add("test1", MyBox.parentStory.characters.itemByRange(1, 2));

       

      //so far so good...

      //now some calculating
      var result = "456";


      //add result to the text frame
      //doing this destroys the link to the existing xml element
      MyBox.parentStory.contents += result;

       

      //doing this creates a new xml element with the wrong characters selected and linked
      app.activeDocument.xmlElements.item(0).xmlElements.add("test2" , MyBox.parentStory.characters.itemByRange(4, 2));

       

       

       

      Can someone please help on?

       

       

      Best regards,

       

      TYPO

        • 1. Re: adding text to parentStory destroys XML element / JavaScript, InD CS4
          Harbs. Level 6

          Add the text to an insertionPoint. writing over the entire story contents will destroy anything in the story (including xml).

           

          Harbs

          • 2. Re: adding text to parentStory destroys XML element / JavaScript, InD CS4
            Dr. TYPO Level 1

            Hello Harbs,

             

            thank you, it almost works now; the xml element brackets are not destroyed anymore.

             

            The only problem I have now is that the second element becomes a  child of the first one.

             

            How can I force each element to  be "independent"?

             

             

            Here's the code:

             

            var MyBox = app.activeDocument.textFrames.add();
            MyBox.geometricBounds = ["0pt","0pt", "200pt", "200pt"];

             

            var MyContent = "bla";
            MyBox.parentStory.insertionPoints[-1].contents = MyContent;
            MyBox.parentStory.characters.itemByRange(-1, -MyContent.length).select();
            app.activeDocument.xmlElements.item(0).xmlElements.add("test1" ,app.activeDocument.selection[0]);

             

            MyContent = "456";
            MyBox.parentStory.insertionPoints[-1].contents = MyContent;
            MyBox.parentStory.characters.itemByRange(-1, -MyContent.length).select();
            app.activeDocument.xmlElements.item(0).xmlElements.add("test2" ,app.activeDocument.selection[0]);

             

             

             

            Best regards,

             

            TYPO

            ***