Skip to main content
Known Participant
November 1, 2008
Question

Script to interleave two InDesign files of equal page counts

  • November 1, 2008
  • 14 replies
  • 9458 views
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
This topic has been closed for replies.

14 replies

Known Participant
November 2, 2008
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
Participant
May 13, 2009

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

Inspiring
November 2, 2008
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
Known Participant
November 2, 2008
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
Loic.Aigon
Legend
November 2, 2008
Impressive, Dave. Wish I could write scripts as quick as you one day.
Known Participant
November 1, 2008
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
Inspiring
November 1, 2008
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
Participant
March 19, 2014

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.

Known Participant
November 1, 2008
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
Loic.Aigon
Legend
November 1, 2008
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.
Inspiring
November 1, 2008
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
Known Participant
November 1, 2008
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