4 Replies Latest reply on Aug 28, 2012 12:26 AM by Stev_u25a1

    Place text object stored in array





      I want to place reordered texts of one InDesign document into another. I did a very slow version with export and import. But I think it should be possible to do this a lot faster.


      My idea is to store the coords and textobjects of text boxes into an array, sort it upon my needs and insert it after that into a new document.

      I a first step I managed to insert the sorted content into a new document, but without any formatting information. Is it possible to insert a complete text object into an existing textframe of a new document?


      Here's the code for building the object, maybe anyone knows how to place my object:


      //Loop through every text frame of the current page and store the properties of each text frame into an associative array


      var myPage = myDocumentA.pages.item(0);

      var myTextframes = myPage.textFrames;

      var mytest = new Array();

      var mycoords = new Array();


      for(var myCounter = 0; myCounter < myTextframes.length; myCounter++)


          var mycoordstemp = myTextframes.item(myCounter).geometricBounds + " ";

          mycoords = mycoordstemp.split(",");

          var mysortcrit = truncate(mycoords[0]) * 10000 + truncate(mycoords[1]);

        var mytext = myTextframes.item(myCounter).parentStory.texts.item(0);

        mytest[myCounter] = {myTextframe:myTextframes.item(myCounter), xcoord:mysortcrit, myTextframeContent:mytext}



      var myDocumentB = app.documents.add();

      var myPageB = myDocumentB.pages.item(0);

      var myTextFrameB = myPageB.textFrames.add({geometricBounds:myGetBounds(myDocumentB, myPageB)});

      for(var myCounter = 0; myCounter < mytest.length; myCounter++)



           // missing: insert the textframecontent into  myTextFrameB


        • 1. Re: Place text object stored in array
          [Jongware] Most Valuable Participant

          The 'contents' property of a text frame only contains Unicode characters; it's a regular Javascript string.


          To duplicate formatted text, use the duplicate function on the texts property of your original one. You cannot move formatted text into a variable, you have to immediately copy it from its source to its destination.


          That said: since you are moving text frames (not "text boxes" ) around, you might as well use duplicate immediately on the original frame itself. This places a copy into your new document on something like a random position, as Paste does, so you need to move it into its correct position.

          • 2. Re: Place text object stored in array
            Stev_u25a1 Level 1

            Thanks Jongware,


            in the meanwhile I got it working almost. There is just a strange behaviour within the duplicate function. Can anybody tell me what's wrong?


            My source doc has several pages with several textframes, my target doc just one

            I still have the main loop, but inside the resulting array the TextFrame is stored:


            myTargetTextFrame.= myTargetDocumet.pages.item(0).textFrames.item(0);

            for(myPageCounter=0; myPageCounter < mySourcedoc.pages.length; myPageCounter++)


                 var mytest = new Array();

                 var myTextframes = mySourcedoc.pages.intem(myPageCounter).textFrames;

                 for(myCounter=0; myCounter < myTextframes.length; myCounter++)


                      mytest[myCounter] = {myTextframe:myTextframes.item(myCounter), xcoord:mysortcrit}




                 for(var myCounter = 0; myCounter < mytest.length ; myCounter++)


                      mytest[myCounter].myTextframe.parentStory.paragraphs.item(0).duplicate(LocationOptions.AT _BEGINNING, myTargetTextFrame.insertionPoints.item(-1));




            The following happens:

            Script processes page 1

            At the targetdocument all the content of sourcedoc.page1 is inserted in the expected order (content textbox1, content textbox2, content textbox 3)


            Script processes page 2

            At the targetdocument the content of sourcedoc.page2 is inserted after content textbox2 of page 1.


            I expect the content to be placed after content textbox3. What's my mistake?




            I think I found the problem. The content of textbox2 has a frame break at it's end. insertionPoints.item(-1) seems to interpret this as end of text.


            Does anybody know how to avoid that problem?


            Message was edited by: Stev_u25a1

            • 3. Re: Place text object stored in array
              Laubender Adobe Community Professional & MVP

              How about using a different location?


              .duplicate(LocationOptions.AFTER, myTargetTextFrame.parentStory.insertionPoints[-1]);




              • 4. Re: Place text object stored in array
                Stev_u25a1 Level 1



                I did it by sorting the array in a different way.


                Many thanks,