27 Replies Latest reply on Aug 4, 2016 3:57 AM by davidn46356087

    Script to interleave two InDesign files of equal page counts

    Al Ferrari Level 1
      I need to merge two documents so that the new one has pages alternating one from each of the source documents. The source files have the same page dimensions and page counts. Or it could be done by inserting pages from one into the other to produce the interleaved merged document.

      This could also work if the source files were sections in a book and a script would print pages alternating one from each section of the book. The script could print to Adobe pdf and produce an interleaved pdf.

      Although my purpose is different, this would be needed by some one scanning documents with a non duplexing scanner to merge the scanned pages into one pdf. I have searched the forums with no results.

      Does anyone know of any scripts to do this, or similar ones that can be modified to do it?

      Thanks,

      Al
        • 1. Re: Script to interleave two InDesign files of equal page counts
          Loic.Aigon Adobe Community Professional
          Hi,
          Maybe a smart way to get your goal without messing indesign files and dealing with document properties is to use your pdfs.

          I mean,

          1. either you wrote a javascript that you use into Acrobat for creating a new PDF file with the files that are opened or files that you may want to choose.

          2. either you place the pdfs into a new indesign file (as all the page dimensions are the same) dealing with the disposition, then exporting to a new pdf. Obviously the safest way in my opinion.
          Your pdf can be indesign generated pdf in the first part of the script or pdf that you call from a choos file dialog. Up to you.

          3. You can of course create a mixed indesign file dealing with copy & paste but I think it's a lot of work for nothing.

          Will try to write something for you.

          Loic
          • 2. Re: Script to interleave two InDesign files of equal page counts
            Al Ferrari Level 1
            Hi Loic,

            Thanks for the response.

            I have no scripting experience, so starting from scratch is not an option for me.

            I agree that the copy/paste is out of the question. My two source files (either ID or pdf) have 200+ pages each. So, either a javascript for InDesign or for Acrobat would work. A choose files dialog would be a nice enhancement, but an Acrobat script that would work with two open files to produce the interleaved merged file would work. Let me know if you need more information.

            Thanks,

            Al
            • 3. Re: Script to interleave two InDesign files of equal page counts
              Loic.Aigon Adobe Community Professional
              OK I think I have all I need.
              I will have a look for the smartest approach, Acrobat or Indesign.
              Right now, I would say Acrobat cause it doesn't take care of individual file formats and the native process may work faster. Will see.
              Loic
              • 4. Re: Script to interleave two InDesign files of equal page counts
                Level 1
                If we're talking CS3 or later, another approach would be to create a third document and then place pages from the other two alternatively.

                Dave
                • 5. Re: Script to interleave two InDesign files of equal page counts
                  Al Ferrari Level 1
                  Hi Dave,

                  Yes CS3. Is there already a script to alternatively place pages from two source IDCS3 files? I am aware of Olav Kvern 's script for placing pdf pages, and Scott Zanelli's for placing pdf or ID pages, but those deal with single file sources.

                  Thanks,

                  Al
                  • 6. Re: Script to interleave two InDesign files of equal page counts
                    Level 1
                    Al,

                    I doubt it. It's not really a common requirement.

                    What to do if the two documents are of different length? Leave the pages blank for the shorter one? Or should the script just stop in its tracks if the two documents don't match in this regard?

                    Dave
                    • 7. Re: Script to interleave two InDesign files of equal page counts
                      Loic.Aigon Adobe Community Professional
                      Of course Dave,
                      Completely forgot this one ;-)
                      Loic
                      In fact, this may be the quickest way to achieve the process although as you underline, it needs CS3. Well thought.
                      • 8. Re: Script to interleave two InDesign files of equal page counts
                        Al Ferrari Level 1
                        It may not be really common, but not that unusual either. As mentioned in my OP, it would be useful to users scanning long paper documents to pdf with a non duplexing scanner.

                        In my case the counts are equal. In the scanning case they would not differ by more than one page, in the case of an odd page count, assuming that any intermediate blank pages from the long paper document are either included in the scanning or inserted as needed in Acrobat as can be done with a blank page at the end to the shorter one to make them equal.

                        As for what to do with files of greater unequal length, the script could optionally stop at the shorter count, or insert alternating blank pages to complete the long count. But I agree that this would involve additional programing just to accommodate truly uncommon cases.

                        Al
                        • 9. Re: Script to interleave two InDesign files of equal page counts
                          Level 1
                          Al,

                          Here you go. I've only tested it with CS4, but I'm fairly sure it will work with CS3. It does, however, expose a bug in CS4. After the first placed page, the master items don't appear in the placed pages.

                          This is a very nasty bug. I trust that Adobe sits up and takes notice and issues a quick fix, although experience says we'll be lucky to see 4.01 inside of six months.
                          //DESCRIPTION: Merge the two open documents
                          

                          (function() {
                            if (app.documents.length != 2) {
                              alert("Please open two InDesign documents and try again."); return;
                            } else {
                              var doc1 = app.documents[0];
                              var doc2 = app.documents[1];
                              if (checkPageSizes(doc1, doc2)) {
                                // docs have same page sizes
                                var pHt = doc1.documentPreferences.pageHeight;
                                var pWd = doc1.documentPreferences.pageWidth;
                                if (!doc1.saved || !doc2.saved) {
                                  if (!confirm("Both documents will be saved before closing. Continue?")) {
                                    return;
                                  }
                                }
                                var doc1Lim = doc1.pages.length;
                                var doc2Lim = doc2.pages.length;
                                var file1 = doc1.fullName;
                                var file2 = doc2.fullName;
                                doc2.close(SaveOptions.yes);
                                doc1.close(SaveOptions.yes);
                                app.documentPreferences.facingPages = false;
                                newDoc = app.documents.add();
                                newDoc.documentPreferences.properties = {
                                  pagesPerDocument:Math.max(doc1Lim, doc2Lim)*2,
                                  pageHeight:pHt,
                                  pageWidth:pWd
                                }
                                app.importedPageAttributes.importedPageCrop = ImportedPageCropOptions.cropContent;
                                var j = 0;
                                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
                                while (true) {
                                  app.importedPageAttributes.pageNumber = j + 1;
                                  if (doc1Lim > j) {
                                    newDoc.pages[j * 2].place(file1);
                                  }
                                  app.importedPageAttributes.pageNumber = j + 1;
                                  if (doc2Lim > j) {
                                    newDoc.pages[(j * 2) + 1].place(file2);
                                  }
                                  j++;
                                  if (j > Math.max(doc1Lim,doc2Lim)) break;
                                }
                                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                              } else {
                                alert("Two documents must have the same page size"); return;
                              }
                            }
                            function checkPageSizes(doc1, doc2) {
                              var d1ht = doc1.documentPreferences.pageHeight;
                              var d1wd = doc1.documentPreferences.pageWidth;
                              var d2ht = doc2.documentPreferences.pageHeight;
                              var d2wd = doc2.documentPreferences.pageWidth;
                              if (d1ht != d2ht) return false;
                              if (d1wd != d2wd) return false;
                              return true;
                            }
                          }())
                          Dave
                          • 10. Re: Script to interleave two InDesign files of equal page counts
                            Al Ferrari Level 1
                            Hey Dave,

                            Thank you very much. I will not be able to test it till Monday, as my home machine is a G3, unable to run IDCS3.

                            Al
                            • 11. Re: Script to interleave two InDesign files of equal page counts
                              Loic.Aigon Adobe Community Professional
                              Impressive, Dave. Wish I could write scripts as quick as you one day.
                              • 12. Re: Script to interleave two InDesign files of equal page counts
                                Al Ferrari Level 1
                                Hi Loic,

                                Thank you also for any time you may have spent on this project. If by chance you have something partially worked up using the Acrobat approach, perhaps you could post that.

                                Al
                                • 13. Re: Script to interleave two InDesign files of equal page counts
                                  Level 1
                                  I'm getting feedback that the CS4 bug I mentioned doesn't always happen. I'll research that some more later today, when I get the chance.

                                  Dave
                                  • 14. Re: Script to interleave two InDesign files of equal page counts
                                    Al Ferrari Level 1
                                    I couldn't wait, so I came to the shop to test the script.

                                    I made a two page non-spreads doc with numerals 2 and 4 on pages 1 and 2 respectively, and another similar doc with numerals 1 and 3 on corresponding pages. With the odd numbers doc front most I ran Dave's script and was rewarded in a flash with a new doc having pages numbered 1, 2, 3, 4. Way to go Dave!

                                    It's worth noting that with the even numbers doc front most, the result is a new doc with pages numbered 2, 1, 4, 3, showing the importance of having the open files in the proper order before running the script.

                                    Thanks again Dave.

                                    Al
                                    • 15. Re: Script to interleave two InDesign files of equal page counts
                                      skooges

                                      I'd like to thank you as well Dave, for a different reason. I'm kind of a newbie scripter, setting up some automated processes for my company and your script showed me how the correct syntax for changing placed page numbers. So I just wanted to say, thanks a ton!

                                      -Tim Porath

                                      • 16. Re: Script to interleave two InDesign files of equal page counts
                                        bchavezgd

                                        tried this in CS5, on a mac.

                                         

                                        froze the program, fyi.

                                         

                                        edit: although it did freeze up, it was working in and generated about 1/3 of what i needed, i guess i'll have to let it run a while longer for the size of the file i'll be generating.

                                        • 18. Re: Script to interleave two InDesign files of equal page counts
                                          Peter Kahrel Adobe Community Professional & MVP

                                          Some coying played a trick on you. Replace this line:

                                           

                                          if (doc1Lim > j) {

                                           

                                          with this:

                                           

                                          if (doc1Lim > j) {

                                           

                                          Peter

                                          • 19. Re: Script to interleave two InDesign files of equal page counts
                                            tlotzer Level 1

                                            Thanks, pkahrel. I didn't think of comparing the pasted text to the above original. I.ve written applescript, not so much Java.

                                            • 20. Re: Script to interleave two InDesign files of equal page counts
                                              tlotzer Level 1

                                              pkahrel,

                                              I fixed the code as suggested above. I checked every line against the original.

                                              I open two 20 pg Indesign docs, run the script and now get this error.

                                              It closes the Doc A and Doc B, creates a new Untitled doc that is correctly 40 pages.

                                               

                                              I wish could step through the script and see the results of each variable change to track down the problem.

                                               

                                              BTW, the Doc A.indd does exist on the desktop as well as Doc B. innd.

                                               

                                              I thought maybe it was having trouble since both docs are using Master page A, so in doc B, I changed its master to "B".

                                              I also told each 20 pages to Override Master page items.

                                               

                                              Still no luck. Any help is appreciated. Using in Indesign CC 2015.

                                               

                                              Java error2.jpg

                                              • 21. Re: Script to interleave two InDesign files of equal page counts
                                                Peter Kahrel Adobe Community Professional & MVP

                                                Sorry, I don't know that script at all, I just spotted that error.

                                                I can't tell what's causing the problem at line 37.

                                                 

                                                > I wish could step through the script and see the results of each variable change to track down the problem.

                                                 

                                                You can:

                                                1. Open the script in the ESTK (right-click it in the Script panel, then choose Edit Script).

                                                2. Go to the Debug menu and select Step Over. This starts running the script and pauses at every line. In the console you can type a value to inspect it, and the Data browser shows details of the contents of various variables.

                                                3. To go to the next line, select Step Over again.

                                                 

                                                The ESTK has its quirks (to put it mildly) but it's the only debugging tool for JavaScripts, and it's workable enough. You can read up on it in the JavaScripts Tools Guide (see the ESTK's Help menu), chapter 2.

                                                 

                                                Peter

                                                • 22. Re: Script to interleave two InDesign files of equal page counts
                                                  tlotzer Level 1

                                                  Tried that. Stepping through, the ESTK does not work within Indesign. Even though you might have the 2 docs opened, while stepping, it doesn't know Indesign has the docs opened. It errors saying, "open two documents". Real frustrating.

                                                  • 23. Re: Script to interleave two InDesign files of equal page counts
                                                    davidn46356087

                                                    Bit of an old thread, I got the script to work that Dave_Saunders posted above, it's great. I just wanted to know if it's possible to include the bleed from the original two documents through to the final interwoven document? I have a work around that involves changing the document sizes to include the bleed, but it would be great if the script could do this.

                                                     

                                                    Thanks

                                                    • 24. Re: Script to interleave two InDesign files of equal page counts
                                                      tlotzer Level 1

                                                      There is a line in the code above from Dave Saunders:

                                                      app.importedPageAttributes.importedPageCrop = ImportedPageCropOptions.cropContent;

                                                       

                                                      Can it be changed to include the bleed instead of crop only?

                                                      • 25. Re: Script to interleave two InDesign files of equal page counts
                                                        Peter Kahrel Adobe Community Professional & MVP

                                                        Replace cropContent with cropBleed.

                                                         

                                                        Peter

                                                        2 people found this helpful
                                                        • 26. Re: Script to interleave two InDesign files of equal page counts
                                                          tlotzer Level 1

                                                          Thanks pkahrel.

                                                           

                                                          I tested it. The art comes in with bleed but places at 0,0 instead of -.125", -.125" coordinates offsetting the page content. Not sure where to make the script offset. It doesn't have to be variable, we always use .125" bleed.

                                                          2 people found this helpful
                                                          • 27. Re: Script to interleave two InDesign files of equal page counts
                                                            davidn46356087 Level 1

                                                            Yeah it would be great if the placed page content was placed at the bleed bounds rather than page bounds.

                                                             

                                                            I've tweaked the script so that it takes the bleed from 'Doc1' and checks that 'Doc2' has the same size bleed. I've also tweaked it so that the bleed amount is added into the newly created document. However i'm just not sure how to specify the position of the place page content.

                                                             

                                                            My tweaked script is here.

                                                             

                                                            (function() {
                                                              if (app.documents.length != 2) {
                                                                alert("Please open two InDesign documents and try again."); return;
                                                              } else {
                                                                var doc1 = app.documents[0];
                                                                var doc2 = app.documents[1];
                                                                if (checkPageSizes(doc1, doc2)) {
                                                            
                                                            
                                                            // docs have same page sizes
                                                                  var pHt = doc1.documentPreferences.pageHeight;
                                                                  var pWd = doc1.documentPreferences.pageWidth;
                                                                  var myDocBleed = doc1.documentPreferences.documentBleedTopOffset;
                                                                  if (!doc1.saved || !doc2.saved) {
                                                                    if (!confirm("Both documents will be saved before closing. Continue?")) {
                                                                      return;
                                                                    }
                                                                  }
                                                            
                                                            
                                                                  var doc1Lim = doc1.pages.length;
                                                                  var doc2Lim = doc2.pages.length;
                                                                  var file1 = doc1.fullName;
                                                                  var file2 = doc2.fullName;
                                                                  doc2.close(SaveOptions.yes);
                                                                  doc1.close(SaveOptions.yes);
                                                                  
                                                                
                                                                  app.documentPreferences.facingPages = false;
                                                                  newDoc = app.documents.add();
                                                                  newDoc.documentPreferences.documentBleedTopOffset = myDocBleed;
                                                                  newDoc.documentPreferences.documentBleedUniformSize = true;
                                                                  newDoc.documentPreferences.properties = {
                                                                    pagesPerDocument:Math.max(doc1Lim, doc2Lim)*2,
                                                                    pageHeight:pHt,
                                                                    pageWidth:pWd
                                                                  }
                                                            
                                                            
                                                                  app.importedPageAttributes.importedPageCrop = ImportedPageCropOptions.cropBleed;
                                                                  var j = 0;
                                                                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
                                                                  while (true) {
                                                                    app.importedPageAttributes.pageNumber = j + 1;
                                                                    if (doc1Lim > j) {
                                                                      newDoc.pages[j * 2].place(file1);
                                                                    }
                                                                    app.importedPageAttributes.pageNumber = j + 1;
                                                                    if (doc2Lim > j) {
                                                                      newDoc.pages[(j * 2) + 1].place(file2);
                                                                    }
                                                                    j++;
                                                                    if (j > Math.max(doc1Lim,doc2Lim)) break;
                                                            
                                                                  }
                                                                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                                                                } else {
                                                                  alert("The two documents must have the same page size and bleed amount. Change these and re-run the script."); return;
                                                                }
                                                              }
                                                              function checkPageSizes(doc1, doc2) {
                                                                var d1ht = doc1.documentPreferences.pageHeight;
                                                                var d1wd = doc1.documentPreferences.pageWidth;
                                                                var d1bleed = doc1.documentPreferences.documentBleedTopOffset;
                                                                var d2ht = doc2.documentPreferences.pageHeight;
                                                                var d2wd = doc2.documentPreferences.pageWidth;
                                                                var d2bleed = doc2.documentPreferences.documentBleedTopOffset;
                                                                if (d1ht != d2ht) return false;
                                                                if (d1wd != d2wd) return false;
                                                                if (d1bleed != d2bleed) return false;
                                                                return true;
                                                              }
                                                            }())