Indesign — Merging All Files

I recently had to combine hundreds of indesign files into a single document, so I made this quick js-based extension. Originally made for CS5; not sure how it works with any others. Feel free to use it.

Download it here or copy-paste from below.

/********* CODE START **********/

// DESCRIPTION: Mass combine a bunch of files (files have one document only) into one.
// Simon Wiscombe
// 6 July 2013
// INSTRUCTIONS
// Put all the files you want to combine into one document in a folder, named in the order
// you want them to appear in the book (e.g. “001.indd”, “002.indd”, etc. or something) so
// they appear correctly when sorted by name in finder/explorer.

#target indesign-7.0
// set the destination document as the active document.
var destination_doc = app.activeDocument;
// select the source folder
var sourceFolder = Folder.selectDialog(“Select a folder with source InDesign files.”);
// if we’re not in the source folder, stop running the script.
if ( !sourceFolder ) {
exit(0);
}
// set file list and run through each file. Sorting them alphanumerically.
var fileList = sourceFolder.getFiles();
fileList.sort();
// run through each file
for ( var i = 0; i < fileList.length; i++ ) {
var source_file = fileList[i];
// making sure it’s an indesign file…
if ( source_file instanceof File && source_file.name.match(/\.indd$/i)) {
app.open(source_file);
var source_doc = app.documents.item(source_file.name);
var sourcePages = source_doc.pages.item(0);
// break the master page items so they can be moved onto the new document.
var masterItems = sourcePages.masterPageItems;
if ( masterItems.length > 0 ) {
for ( var j=0; j<masterItems.length; j++ ) {
masterItems[j].override(sourcePages);
}
}
// removing the applied master (this can mess up some files if not done)
sourcePages.appliedMaster=null;
// duplicating it in the original file (due to errors) and them moving it to
// the destination document.
sourcePages.duplicate(LocationOptions.AFTER, source_doc.pages.item(0));
sourcePages.move(LocationOptions.AFTER, destination_doc.pages.item(-1));
// closes the file that was opened without saving (to avoid memory problems)
app.activeDocument.close(SaveOptions.NO);
}
}

/********* CODE END **********/

 

I’ve been asked to put a version for multiple-document indesign files, so here is a code that SHOULD work.
WARNING: THIS HAS NOT BEEN TESTED. It may not work.

 

/********* CODE START **********/

// DESCRIPTION: Mass combine a bunch of documents (with multiple pages) into one.
// Simon Wiscombe
// 19 Nov 2013
// INSTRUCTIONS
// Put all the files you want to combine into one document in a folder, named in the order
// you want them to appear in the book (e.g. “001.indd”, “002.indd”, etc. or something) so
// they appear correctly when sorted by name in finder/explorer.

#target indesign-7.0

// set the destination document as the active document.
var destination_doc = app.activeDocument;

// select the source folder
var sourceFolder = Folder.selectDialog(“Select a folder with source InDesign files.”);

// if we’re not in the source folder, stop running the script.
if ( !sourceFolder ) {
exit(0);
}

// set file list and run through each file. Sorting them alphanumerically.
var fileList = sourceFolder.getFiles();
fileList.sort();

// run through each file
for ( var i = 0; i < fileList.length; i++ ) {
var source_file = fileList[i];

// making sure it’s an indesign file…
if ( source_file instanceof File && source_file.name.match(/\.indd$/i)) {
app.open(source_file);
var source_doc = app.documents.item(source_file.name);

for ( var count = 0; count < source_doc.pages.length; count++ ) {
var sourcePages = source_doc.pages.item(count);

// break the master page items so they can be moved onto the new document.
var masterItems = sourcePages.masterPageItems;
if ( masterItems.length > 0 ) {
for ( var j=0; j<masterItems.length; j++ ) {
masterItems[j].override(sourcePages);
}
}

// removing the applied master (this can mess up some files if not done)
sourcePages.appliedMaster=null;

// duplicating it in the original file (due to errors) and them moving it to
// the destination document.
sourcePages.duplicate(LocationOptions.AFTER, source_doc.pages.item(0));
sourcePages.move(LocationOptions.AFTER, destination_doc.pages.item(-1));
}

// closes the file that was opened without saving (to avoid memory problems)
app.activeDocument.close(SaveOptions.NO);
}
}

/********* CODE END **********/

20 thoughts on “Indesign — Merging All Files

  1. Lauren says:

    Hi,

    I’ve tried using your script and it mostly works but its adding all of the pages into one spread. And so it is maxing out at 28 pages but the script does know that, because it continues to add pages (that are not showing up) to the document. I tried this with a blank document so I don’t think there was any existing master page issues that would cause this.

    Has anyone else had this problem. I am using CS5 on a mac. I’m happy to send you the document I created to troubleshoot.

    Also in your script since it closes down each document as it goes by the time it gets to the end it closes the file with added pages before saving. In order to avoid this I removed it from the script but now its cumbersome in InDesign with all of the files open. Is there a better way?

    Thanks for your help!
    Lauren

  2. admin says:

    Both bugs are likely due to the spread. I should have mentioned that this script was used to add pages to a non-spread document (i.e. with the “facing pages” option turned off when you open a new document).

    Does your version need to have a spread? If not, I’d suggest trying to turn off “facing pages” and trying again.

    And that closes each document was precisely because of the memory problems that come with having all the files open. I’m surprised that it closes the last file — might be due to, again, the spread option.

  3. Alberto says:

    Hi Simon, first of all thanks for publishing your script. I have 20 indesign CS6 files that i want to merge into one. I’ve put them in a folder, named 001, 002… Then i make an empty document with no facing pages. Run the script, select the source folder and after working its magic, i get a merged document with the first page of each source files.
    Anyway to merge the entire contents of every file?

    Thanks in advance!

  4. admin says:

    Hey Alberto, wanted to comment with more room on the sidebar, so posted as a direct reply to my own post. See below!

  5. admin says:

    Hi Alberto,

    It’s possible, but you’ll need to add a for loop that runs through each of the pages in the document. Should be pretty simple (depending on how familiar you are with coding…), but you need to do it after you declare the var souce_doc and close the loop before the “app.activeDocument.close etc” line.

    Sooo, you might have to tinker with it a bit to test it, but this would be my hunch:

    var source_doc = app.documents.item(source_file.name);

    for ( var count = 0; count < source_doc.pages.length; count++ ) { var sourcePages = source_doc.pages.item(count); // break the master page items so they can be moved onto the new document. var masterItems = sourcePages.masterPageItems; if ( masterItems.length > 0 ) {
    for ( var j=0; j

  6. Alberto says:

    Thanks again Simon. I’m afraid my coding is below cero…

    I’ve implemented the changes in the script body and i get an error on the line: “for ( var j=0; j masterItems[j].override(sourcePages);”

    It expects something like “;” but i can’t figure out

    We’re getting closer… ;-) Thanks.

  7. simon says:

    Something didn’t copy-paste. See original code and change it:

    for ( var j=0; j

  8. Egikas says:

    Hi, Simon. It’s really a great script, thank you for your work!! The same question as Alberto has and the same problems. If you could post the hole code for merging all pages, not only firsts, it would be just fantastic!!

  9. simon says:

    I’m updating the post with the full text of the multi-page document. Warning though: it’s not a tested code so if it doesn’t work I can’t help you get all the way there.

    But theoretically, it’s how you’d do it.

  10. Egikas says:

    Thank you, Simon. Your script works fine! Though there is still some bugs. But it really works!! Thank you one more time.

  11. Felipe Santos says:

    Hi,

    Is there any chance this script works in CS6? I tried it, but didn’t work

    Regards

  12. Edward Lipsett says:

    Felipe,

    You need to change
    #target indesign-7.0
    to match your version. CS6 would be
    #target indesign-8.0

  13. Christina Cummings says:

    The script will work but after a few pages I get an error..any ideas? it’s not after a certain # of pages, it happens randomly…possibly an error in the source files?

    Error number: 45
    ERror String: object is invalid

    engine: main
    file: **location of my script**
    Line: 25
    Source: for ( var count = 0; count < source_doc.pages.length; count++)
    {

  14. Christina Cummings says:

    Actually I was wrong..it is after a certain # of pages..it’s after 10 pages i get that error. I’m not using facing pages…any ideas?

  15. simon says:

    Hey Christina, sorry I didn’t see this sooner. For some reason it got filtered into spam on my end. But I also saw that you resolved it by combining…

    app.open(source_file);
    var source_doc = app.documents.item(source_file.name);

    Into one line of…

    var source_doc = app.open(source_file);

    Just leaving this here in case someone else comes across it and is having similar problems.

  16. Oliver says:

    Hi Simon!
    Your script works perfectly for me but it sets every imported file to a new section which i have to remove manually afterwards.
    Any idea how to remove this behavior?

  17. simon says:

    Sorry not really sure how to fix that problem. It’s likely because you have a more recent version of indesign and it changes subtly with each update.

  18. Kelly says:

    Hi Simon, Thank you for your wonderful script!
    I’m still getting multiple identical pages even though the total page count seems correct.
    Not sure how to fix it. Do you have any suggestions?

  19. Michael says:

    Here is a more enhanced alternative. It works in CS 5.5 for sure. I didn’t test it in other versions but it should work in CS5 and CS6 much the same.

    /*Written and updated by Michael Z.
    Allows merging of files from active book or all opened documents or any chosen folder with all its subfolders.
    Allows to change the sequence of files before producing the final file.
    Allows to remove unneeded files before producing the final file.
    Allows to maintain page numbers from original documents.
    Allows to maintain original sections.
    */

    #targetengine session

    var list = {}, folder = “”, lst = [], sn, pn, run = false;

    var comp = new Window (“dialog {margins:5, spacing:5, text:’Merger’}”);
    var grComp = comp.add (“group {margins:5, orientation:’column’}”);

    var fileLst = grComp.add (“listbox”, [0,0,210,250]);
    fileLst.helpTip = “Press up or down keys to move items in the list.\nPress Delete to remove an item.”;
    fileLst.addEventListener (“keydown”, function (k) {
    if (k.keyName == “Up”) {
    var n = fileLst.selection.index;
    if (n > 0) {
    swap (fileLst.items [n-1], fileLst.items [n]);
    }
    }
    if (k.keyName == “Down”) {
    var n = fileLst.selection.index;
    if (n fileLst.items.length-1)
    fileLst.selection = fileLst.items.length-1;
    else
    fileLst.selection = sel;
    }
    });

    var rbComp = comp.add (“panel {margins:5, spacing:0, orientation:’column’, size:[210,90]}”);
    var aodocs = rbComp.add (“radiobutton {text:’All opened documents’}”, [0,0,195,20]);
    aodocs.onClick = function () {
    fileLst.removeAll ();
    if (app.documents.length>0) {
    for (i = 0; i 0) {
    for (i = 0; i < app.activeBook.bookContents.length; i ++) {
    fileLst.add ("item", app.activeBook.bookContents[i].name);
    list [app.activeBook.bookContents[i].name] = app.activeBook.bookContents[i].fullName;
    }
    } else {
    alert ("No books opened.");
    abdocs.value = false;
    return;
    }
    }

    var flddocs = rbComp.add ("radiobutton {text:'All documents in a folder'}", [0,0,195,20]);
    flddocs.onClick = function () {
    fileLst.removeAll ();
    var bf = Folder.selectDialog ("Select folder with files.");
    if (bf) {
    var myFileList = bf.getFiles ("*.indd");
    for (var i = 0; i < myFileList.length; i++) {
    var myFile = myFileList [i];
    list [myFile.displayName] = myFile.fsName;
    fileLst.add ("item", myFile.displayName);
    folder = bf.fsName;
    }
    }else {
    alert ("No folder selected.");
    flddocs.value = false;
    return;
    }
    }

    var fldAlldocs = rbComp.add ("checkbox {text:'Including subfolders'}", [15,0,180,20]);
    fldAlldocs.onClick = function () {
    if (Folder (folder).exists && fldAlldocs.value == true) {
    fileLst.removeAll ();
    var myFileList = getSubFolders (Folder (folder));
    for (m in myFileList) {
    fileLst.add ("item", m);
    }
    }else {
    alert ("No folder selected.");
    fldAlldocs.value = false;
    return;
    }
    }

    var secComp = comp.add ("panel {margins:5, spacing:0, orientation:'column', size:[210,50]}");
    var sec = secComp.add ("checkbox {text:'Keep sections'}", [0,0,195,20]);
    var pgNum = secComp.add ("checkbox {text:'Keep page numbers'}", [0,0,195,20]);
    sec.onClick = function () {
    if (sec.value == false)
    pgNum.value = false;
    }
    pgNum.onClick = function () {
    if (sec.value == false)
    sec.value = true;
    }

    var btnComp = comp.add ("panel {margins:5, orientation:'row', size:[210,35]}");
    var okBtn = btnComp.add ("button {text:'OK', size:[90,20]}");
    var cancBtn = btnComp.add ("button {text:'Cancel', size:[90,20]}");

    function swap (x, y) {
    var temp = x.text;
    x.text = y.text;
    y.text = temp;
    }

    okBtn.onClick = function () {
    for (i = 0; i -1; a –) {
    var tmpdoc = app.open (File (list[lst[a]]), false, OpenOptions.OPEN_COPY);
    tmpdoc.documentPreferences.facingPages = false;
    tmpdoc.pages.everyItem ().duplicate (LocationOptions.BEFORE, doc0.pages[0]);
    if (sn)
    doc0.pages[0].appliedSection.continueNumbering = false;
    if (pn) {
    doc0.sections.add (doc0.pages[0], {pageNumberStart:tmpdoc.pages[0].appliedSection.pageNumberStart});
    doc0.pages[0].appliedSection.pageNumberStyle = tmpdoc.pages[0].appliedSection.pageNumberStyle;
    }
    tmpdoc.close (SaveOptions.NO);
    }
    if (!sn && doc0.sections.length > 1)
    doc0.sections.itemByRange (1, doc0.sections.length-1).remove ();
    doc0.documentPreferences.facingPages = face;
    app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;
    }, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, “Merge Documents”);
    }

    function getSubFolders (theFolder) {
    var myFileList = theFolder.getFiles ();
    for (var i = 0; i < myFileList.length; i++) {
    var myFile = myFileList [i];
    if (myFile instanceof Folder) {
    getSubFolders (myFile);
    }
    else if (myFile instanceof File && myFile.name.match (/\.indd$/i)) {
    list [myFile.displayName] = myFile.fsName;
    }
    }
    return list;
    }

  20. Vivien says:

    Hello Michael,

    I work on InDesign CC 2015, and I tried with your code. I have an error message that appears when I start the script.

Leave a Comment

Your email address will not be published. Required fields are marked *