16 Replies Latest reply on Oct 23, 2014 8:40 PM by biozaichenko

    Help with Combine Docs script

    myDavey Level 1

      Hi,

      I am writing a script that combines a list of docs into 1 doc.

      The script starts with a dialog with a list of all open docs.

      The user chooses the files he wants to combine.

      The script reads the list and send to the following function:

       

       function combine() {
      
          var docsPages = [];
          var pgCount;
          var newDoc = app.documents.add();
      
          for (var i=0; i<myFiles.length; i++) {
              var myDoc = app.documents.item(myFiles[i])
              docsPages[i] = myDoc.pages.everyItem().getElements();
          }
      
          for (var v = 0; v < myFiles.length; v++) {
              for (var i=0; i<docsPages[v].length; i++) {
                  docsPages[v][i].duplicate( LocationOptions.AFTER, newDoc.pages.lastItem() );
              }
          }
      
      }
      

       

      The script works almost correctly...

      The problem I am having is that the script places all the pages side by side.

      The outcome is one long spread with 18 pages!

      Here is a screenshot to explain what I mean:

      Screen Shot 2014-01-12 at 1.05.05 PM.png

       

      What am I doing wrong here?

       

      Thanks,

      Davey

       

       

      P.S. I set up of the script the way I did (2 loops) because I want to give the option to interleave pages.

        • 1. Re: Help with Combine Docs script
          Laubender Adobe Community Professional & MVP

          @Davey – are you sure? A spread with 18 pages?

          That (really! I'm not kidding!) would be absolutely fantastic:

          breaking the max. number of pages per spread limit!

           

          Does it work to export an IDML and retain the 18 pages?

          Can you upload such a file (both InDesign and IDML) for downloading?

           

          What exact InDesign version on what platform?

           

          Uwe

          • 2. Re: Help with Combine Docs script
            myDavey Level 1

            Hi Uwe - Thanks for making my problem into an excitement!

             

            Regarding the "are you sure: question:

            Screen Shot 2014-01-12 at 6.37.59 PM.png

             

            Regarding the rest:

            When I exported to IDML it changes to 19 spreads, but all the text boxes stayed ont he loooong 1st spread.

             

            I would attach the files, but I dont see how to upload them.

            I see only pictures and videos but not files...

             

             

            Davey

            • 3. Re: Help with Combine Docs script
              Jump_Over Level 5

              Hi,

               

              @Uwe, @Davey,

               

              It looks like page.duplicate() works over this limit...!

               

              @Davey

              You need to control adding spreads while duplicating pages...

              There are also some facingPages and allowPageShuffle implications...

              There could be a problem with masterSpread's pageItems as well ==> I mean lack of them in target doc.

               

              Here is a code which basically works:

              function combine() {
              var 
                   newDoc = app.documents.add(),
                   docsPages = [],
                   v, i;
              
                   for (v = 0; v < myFiles.length; v++) {
                        docsPages = app.documents.item(myFiles[v]).pages.everyItem().getElements();
                        for (i = 0; i < docsPages.length; i++) {
                             if (newDoc.pages.length % 2 == 1 ) {
                                  newDoc.spreads.add({allowPageShuffle: false});
                                  docsPages[i].duplicate(LocationOptions.AT_BEGINNING, newDoc.spreads[-1]);
                                  if (v == 0 && i == 0) newDoc.spreads[0].remove();
                                  while (newDoc.spreads[-1].pages.length > 1) newDoc.spreads[-1].pages[-1].remove();
                             } else docsPages[i].duplicate(LocationOptions.AT_END, newDoc.spreads[-1]);
                        }
                   }
              }
              

               

              myFiles should be an array with doc.names

               

              Jarek

              • 4. Re: Help with Combine Docs script
                myDavey Level 1

                Hi Jarek

                Thanks a lot - your function worked great, except that the first page ended up on the left side of the 'spread' as if it were a right bound document! - Is that yhy you wrote basically?

                 

                After a little more playing, I found that the problem started from the fact that the new docs page size was different than the old docs page size.

                When I added specific page sizes to the new doc, the duplicate worked fine.

                 

                However, if the 2 docs have different page sizes, or if within one doc there different page sizes, then it starts the same behavior - i.e., placing all pages that are different size - than the doc page size - on one spread.

                 

                However, your method works in all instances, even when the pages are different sizes. The only problem is the one I mentioned before, that the setup of the facing pages gets messed up...

                 

                 

                Thanks a lot

                Any further clarification will be very appreciated

                All the best

                Davey

                • 5. Re: Help with Combine Docs script
                  XH.XH Level 1

                  Hi, myDavey,  try the following code, always keep the original spread, not change:

                  function combine() {
                      var docsSpreads = [];
                      var pgCount;
                      var newDoc = app.documents.add();
                      newDoc.documentPreferences.allowPageShuffle=false;  ////
                      for (var i=0; i<myFiles.length; i++) {
                          var myDoc = app.documents.item(myFiles[i])
                          docsSpreads[i] = myDoc.spreads.everyItem().getElements();
                      }
                      for (var v = 0; v < myFiles.length; v++) {
                          for (var i=0; i<docsSpreads[v].length; i++) {
                              docsSpreads[v][i].duplicate( LocationOptions.AFTER, newDoc.spreads.lastItem() );
                          }
                      }
                  }
                  

                   

                  wish you good luck!

                  • 6. Re: Help with Combine Docs script
                    myDavey Level 1

                    @XH.XH

                    Your code worked to a certain extent

                    It brought in all pages no matter what the size was

                    However, after that it is impossible to bring them together with "facing pages" option.

                    They always remain on different spreads.

                     

                    So, i guess this is also not the answer...

                     

                    Thanks

                    Davey

                    • 7. Re: Help with Combine Docs script
                      Laubender Adobe Community Professional & MVP

                      Davey wrote:

                      When I exported to IDML it changes to 19 spreads, but all the text boxes stayed ont he loooong 1st spread.

                       

                      @Davey – that's too bad. But the prospect that we can do it with page.duplicate() is fantastic!

                      Another negative example of IDML as exchange format.

                       

                      Also interesting, that the page objects would not follow their pages after opening the IDML.


                      There could be (definitely: there are!) some good reasons to work beyond the limit of 10 pages per spread. But hey, I'll try it out and see what I can do to recreate the documents' original state after opening documents of that kind.

                       

                      Let's see if PDF export is able to work as expected in these situations.

                       

                      About attaching the files:

                      not possible here, but you could provide them via dropbox (or a similar service) and post the link here.

                       

                      Or you could send me the files per mail and if you want to I can upload them to my dropbox account and post the link here.

                       

                      I'll send you a personal message (PM) for the right mail address…
                      Done.

                       

                      @Jarek – could you create a document with more than 10 pages per spread?
                      And if so, how were the fundamental conditions?

                       

                      Uwe

                      • 8. Re: Help with Combine Docs script
                        Laubender Adobe Community Professional & MVP

                        @Davey & Jarek – I don't need the example files for testing. Just created a document with 11 pages in spread one!

                         

                        Before:
                        Simple document, two pages with different bounds:

                         

                        TwoDifferentPages-BEFORE-SCRIPT.png

                         

                        After running the script below:

                         

                        SpreadOneWith11pages-AFTER-SCRIPT.png

                         

                        //Adding10pagesToSpreadOne_ACTIVE-DOCUMENT.jsx
                        //Uwe Laubender
                        
                        //USING ONE INDESIGN-DOCUMENT
                        
                        /**
                        * @@@BUILDINFO@@@ EXAMPLE_Duplicate_LastPage_AFTER_FirstPage_10x.jsx !Version! Mon Jan 13 2014 11:02:14 GMT+0100
                        */
                        
                        //See discussion at:
                        
                        //myDavey
                        //Help with Combine Docs script
                        //http://forums.adobe.com/thread/1377713?tstart=0
                        
                        //TESTED WITH ONE DOUBLE SIDED DOCUMENT:
                        //2 pages
                        //Different bounds !!
                        
                        //InDesign CS5.5 on OSX 10.6.8
                        
                        //RESULT: 11 [ ! ] PAGES IN THE FIRST SPREAD !!
                        
                        var d=app.documents[0];
                        var myLimit = 10;
                        var firstPage = d.pages[0];
                        var secondPage = d.pages[1];
                        $.writeln("firstPage.bounds"+"\t"+firstPage.bounds);
                        $.writeln("secondPage.bounds"+"\t"+secondPage.bounds);
                        
                        /*
                        Just an example in MILLIMETERS:
                        
                        firstPage.bounds        0,0,296.999999999461,209.999999999936
                        secondPage.bounds       0,0,200,290
                        
                        */
                        
                        //THAT WILL THROW AN ERROR, IF FIRST PAGE is:
                        //"[1]" and not "1"
                        
                        //WILL WORK IN A TWO-PAGES DOCUMENT, IF THE PAGE BOUNDS OF PAGE 1 ARE DIFFERENT FROM PAGE 2
                        
                        for(var n=0;n<10;n++){
                            var lastPage = d.pages[-1];
                            try{
                            lastPage.duplicate(LocationOptions.AFTER,firstPage);
                            }catch(e){$.writeln(e.toSource())};
                            };
                        
                        /*
                            CASE 1:
                            All pages same bounds
                            => 10 pages are added AFTER page 1 => several new spreads
                        
                            CASE 2:
                            Two pages with DIFFERENT bounds
                        
                            2.1 Spread 1 is "[1]" INSTEAD OF "1"
                            => 9 pages are added AFTER page 1 => same spread
                        
                            ERROR:
                            Result after adding 9 pages to page 1:
                            (new Error ("Ein Druckbogen kann maximal 10 Seiten enthalten."))
                        
                            Roughly translated from my German ESTK:
                            (new Error ("One spread can contain a maximum of 10 pages."))
                        
                            2.2 Spread 1 is "1" INSTEAD OF "[1]"
                        
                            => 10 [ ! ] pages are added to spread 1 => exceeding the limit of 10 pages per spread !!
                        
                        */
                        

                         

                        //EDIT:

                        Yes! I am excited!

                        Will do further testing…

                         

                        Uwe

                         

                        Message was edited by: Laubender

                        • 9. Re: Help with Combine Docs script
                          Laubender Adobe Community Professional & MVP

                          In effect one can do something like this:

                           

                          OneSpreadSixteenPages-Rearranged.png

                           

                          Caveat: Export to IDML will ruin the day!

                           

                          And let's see, if all other functions (expecially PDF export and printing) will work as expected.

                           

                           

                           

                          Uwe

                          • 10. Re: Help with Combine Docs script
                            Jump_Over Level 5

                            Hi,

                             

                            @Uwe. No problem with export 12-pages spread as PDF (pretty nice landscape)

                             

                             

                            @ Davey

                            To keep 1st page on the right side:

                            function combine() {
                            var 
                                 newDoc = app.documents.add(),
                                 docsPages = [],
                                 v, i;
                            
                            for (v = 0; v < myFiles.length; v++) {
                                 docsPages = app.documents.item(myFiles[v]).pages.everyItem().getElements();
                                 for (i = 0; i < docsPages.length; i++) {
                                      if (v == 0 && i == 0) {
                                           docsPages[i].duplicate(LocationOptions.AT_BEGINNING, newDoc.spreads[-1]);
                                           newDoc.pages[-1].remove();
                                      } else if (newDoc.pages.length % 2 == 1 ) {
                                           newDoc.spreads.add({allowPageShuffle: false});
                                           docsPages[i].duplicate(LocationOptions.AT_BEGINNING, newDoc.spreads[-1]);
                                           while (newDoc.spreads[-1].pages.length > 1) newDoc.spreads[-1].pages[-1].remove();
                                                } else docsPages[i].duplicate(LocationOptions.AT_END, newDoc.spreads[-1]);
                                           }
                                      }
                            }
                            

                             

                            Jarek

                            • 11. Re: Help with Combine Docs script
                              myDavey Level 1

                              Hi Jarek

                              Thanks for your function!

                              It worked perfectly!

                              Just for testing, I tried changing the page layout to "non-facing pages" -

                              The first time, I get a message like this: (which seems understandable since we manually placed pages on spreads.)

                              Screen Shot 2014-01-13 at 10.07.37 AM.png

                               

                              But after that everything works PERFECT!

                               

                               

                              Thanks again!

                              All the best

                              Davey

                              • 12. Re: Help with Combine Docs script
                                biozaichenko Level 1

                                The only problem I find with this script is that you lose text threading from one spread to another, therefore defeating the whole purpose of scripting it. In other words if the document you are "importing" has 100 pages of threaded text frames, they will be un-threaded in your new document and you will have to figure out the threading separately. Any ideas on this?

                                • 13. Re: Help with Combine Docs script
                                  Laubender Adobe Community Professional & MVP

                                  @biozaichenko – that's no real problem. Just a problem of organizing things. With the methods insertLabel() and extractLabe() on the original text frames you can know which text frame belongs to what story even after duplicating pages to a new document and re-thread the text frames in intented order.

                                   

                                  You could store the values in a multi-dimensional array at the beginning of the script holding information about the story and its text containers. Look at it like organizing a move from one place to another where you label all parts of furniture before taking it apart and assemble it later.

                                   

                                  Uwe

                                  • 14. Re: Help with Combine Docs script
                                    biozaichenko Level 1

                                    @Uwe. Thanks. Yes, that is a solution. I actually stored this info in the name property of the text frames, so that I can access them without looping to find them. Unless you know how to get a specific textframe by it's label? But otherwise your solution is great! I'm using it right now - it debugged quite a few things.

                                     

                                    My scenario is moving 20 page threaded documents into clusters of about 200 pages but they have to be still threaded like originals. So before I move pages into a first document, I name each textcontainer with "storyId,frame#". Then, after all docs are placed, I find all stories Ids and loop through them and within each, loop while there are frame#'s and thread those.

                                     

                                    Do you think it is the best way to do it?

                                     

                                    Michael

                                    • 15. Re: Help with Combine Docs script
                                      Laubender Adobe Community Professional & MVP

                                      @Michael – another way would be to move() all the threaded text frames of a single story to one spread. In that scenario you keep the threading, but you have to store position and page name (or documentOffset) of the individual text frame before to move them back in position after duplicating the pages.

                                       

                                      What is the best way?

                                      Generally spoken, I think there is no best way.

                                      It would depend on the structure of your document.

                                       

                                      I would go with insertLabel(), extractLabel() instead of changing the name, so that the user is seeing the same stuff when opening the layers panel and inspecting a text frame from there after the script was executed.

                                       

                                      Uwe

                                      • 16. Re: Help with Combine Docs script
                                        biozaichenko Level 1

                                        @Uwe. Thank you. The one thing I'm not getting with insertLabel() / extractLabel() is how do you access label information that you stored in an multi-dimensional array. Let's say I stored insertLabel(story1, frame0). How do I easily access this frame now? With itemByName() it is easy but there is no such thing as itemByLabel(). With extractLabel() I have to loop and loop until found for each story which is not efficient. Or can I get a collection of textframes based on their label key?

                                         

                                        Michael