5 Replies Latest reply on Jun 25, 2013 5:43 AM by Laubender

    Copy and Paste

    BEGINNER_X Level 3

      Hi Forums,


      My request is to copy all the stories from the active document and paste in another document (template) in pages[0].textFrames[0].insertionPoints[-1]. And flow upto end of document


      My document having only 3 or 4 stories and 300 pages.




      Trying Code:

      "copy and paste all the contents in the destination document but in wrong postion and also put the content twice"


      var docSource = app.documents.item("Untitled-12.indd");

      var docDestination = app.documents.item("Untitled-13.indd")


      var myStories = app.activeDocument.stories;


          for(i = 0; i < app.activeDocument.stories.length; i++){

              app.activeDocument = docSource;

              myStory = app.activeDocument.stories[i].texts.everyItem();


              var myPageSourceItems = app.select(myStory)



              var myPageDestination = docDestination.pages[0].textFrames[0].insertionPoints[-1];






      Please help me to find the solution.




        • 1. Re: Copy and Paste
          Andreas Jansson Level 2



          One word of "warning": You should usually avoid using the select, copy, and paste commands from scripting. It's better and faster to just get and set the texts (without putting them on the clipboard of the operating system).


          Here is a simplified version of your code. It won't expand or flow the text over several pages etc (or what you might want to do), but it might give you a hint on how to change your code and go on.


          var docSource = app.documents.item("Untitled-1");

          var docDestination = app.documents.item("Untitled-2")


          for(i = 0; i < docSource.stories.length; i++){

              myStory = docSource.stories[i];

              docDestination.textFrames[0].insertionPoints[-1].contents = myStory.contents;



          Best regards,


          • 2. Re: Copy and Paste
            Andreas Jansson Level 2

            You could even do the copying on one row:


            docSource.stories.everyItem().duplicate(LocationOptions.AT_END, docDestination.textFrames[0].texts[-1]);


            Or if you need more control over each text fragmentm and would like to keep the loop, you could still use the duplicate command:


            for(i = 0; i < docSource.stories.length; i++){
                docSource.stories[i].duplicate(LocationOptions.AT_END, docDestination.textFrames[0].texts[-1]);


            Edit: Using duplicate() instead of copying or getting/setting the text contents, will keep all styles from the original story!


            Message was edited by: Andreas Jansson

            • 3. Re: Copy and Paste
              Laubender Adobe Community Professional & MVP

              I did not test your code. What problems did arise with that?
              I only see at the first glance, that you are using app.copy(), app.paste() etc.
              To paste something to a particular place, you first have to select it.


              Hopping through source and target selecting, copying, pasting…
              Sounds not reliable.


              Just a few hints:


              First off, do not think in UI terms like select/copy/paste/deselect when planning a script.
              This should be something like a "last resort".


              Instead, look at the available methods and properties in DOM documentation.

              Ok. The task here is to make a copy of a story (or several stories) to another document.


              So look up the Story object in DOM documentation.
              Jongware's DOM documentation is exceptional!




              I prefer the CHM versions, but there are also HTML versions, even online.

              Here the Story object:




              If we inspect the methods of Story (Quicklinks on the top of the page), there is no copy() or paste() method.
              Damn. Is there something else we could use?


              When we rephrase our task we will find:




              I think, now you could write that script without using copy/paste…




              var sourceDoc = app.documents.itemByName("Source.indd");
              var targetDoc = app.documents.itemByName("Target.indd");
              var sourceStory = sourceDoc.pages[0].textFrames[0].parentStory;
              var targetStoryLastInsertionPoint = targetDoc.pages[0].textFrames[0].parentStory.insertionPoints[-1];


              A second hint: make sure that all your source stories end with a paragraph sign.

              The order of stories in a document will differ from what you expect…



              1 person found this helpful
              • 4. Re: Copy and Paste
                BEGINNER_X Level 3

                Hi Laubender/Andreas,


                Thanks for your reply... Provided script working fine. Always you teach a lot....


                Could you please let me know the autoflow concept: (to flow upto end of page)


                for(i = 0; i < docSource.stories.length; i++){
                    docSource.stories[i].duplicate(LocationOptions.AT_END, docDestination.textFrames[0].texts[-1]);





                • 5. Re: Copy and Paste
                  Laubender Adobe Community Professional & MVP

                  There are a few concepts for that:


                  1. Let the user do it!

                  2. Check, if the property overflows of the parentStory of your target text is true, and if yes, add another page to the document* with another text frame and set the nextTextFrame property of the first one to the new text frame.


                  *or make the target text frame bigger

                  *or add a new text frame to the pasteboard and connect the two

                  *or …


                  You could put that  well into a while loop.
                  But be careful!
                  There might be situations a story will always overflow; so put a counter to the loop, that stops the loop after a reasonable time of iterations.


                  In case you do not, you will end up with the maximum page number possible (9999 I think) ;-)
                  And your script will run quite a time adding pages and text frames…