A few weeks ago on the Adobe InDesign scripting forum, InDesign Guru AnneMarie Concepcion asked for a script that would allow the user to select a bunch of files and automatically place them inside an InDesign document, one after the other:

“Is there a script that, after the designers load a Place cursor with the 30-40 files (in the correct order), will allow them to single-click on page 1, and InDesign will place one after the other of the files, autoflowing as necessary? They still need them to be placed as individual InCopy files (so concatenating first wouldn’t help). I don’t think there would ever be a case of 2 stories being on the same page.”

The script I wrote for her is posted below and is called Multi-File Auto-Place, because, as the name implies, it allows you to automatically place multiple files into InDesign.

Please read the script comments carefully – especially the bit about perpetual overflow throwing the script into an endless loop!

// BS"D
// Multi-file auto-place
// An InDesign Script by TAW, (c) Id-Extras.com, 2014
// This script will allow the user to select a bunch of placeable files (Word docs, etc.)
// It will then attempt to place and auto-flow all the selected files.
// The script will start from page 1 of the active document
// and keep adding pages as needed.
// It will add text frames as needed, within the margins of the page.
// IMPORTANT: There is no error-checking for perpetual overflow!!!
// So, if something you're trying to place cannot fit within the margins,
// The script will continue adding pages to InDesign until it crashes.
// To quit the script, press ESC.

var myDoc = app.activeDocument,
   myFiles = File.openDialog('Select files to place...', undefined, true),
   i,
   currentPage = myDoc.pages[0],
   prevFrame,
   myFrame;
for (i = 0; i < myFiles.length; i++){
   myFrame = addFrame(currentPage);
   myFile = myFiles[i];
   try{
      myFrame.place(myFile, false);
   }
   catch(e){
      alert('Unable to place file: '+myFile, 'Multi-file auto-place');
       continue;
   }
   while (myFrame.overflows){
      currentPage = addPageAfter(currentPage);
      prevFrame = myFrame;
      myFrame = addFrame(currentPage);
      prevFrame.nextTextFrame = myFrame;
   }
   currentPage = addPageAfter(currentPage);
}

function addFrame(aPage){
   var pageMargins = aPage.marginPreferences,
   aFrame = aPage.textFrames.add(),
   areFacing = app.activeDocument.documentPreferences.facingPages,
   myTop = aPage.bounds[0]+pageMargins.top,
   myBottom = aPage.bounds[2]-pageMargins.bottom,
   myLeft = aPage.bounds[1]+pageMargins.left,
   myRight = aPage.bounds[3]-pageMargins.right;
   //When document.documentPreferences.facingPages == true,
   //"left" means inside; "right" means outside.
   if (areFacing && aPage.side == PageSideOptions.LEFT_HAND){
      myLeft = aPage.bounds[1]+pageMargins.right;
      myRight = aPage.bounds[3]-pageMargins.left;
   }
   aFrame.geometricBounds = [myTop, myLeft, myBottom, myRight];
   return aFrame;
}

function addPageAfter(aPage){
   return myDoc.pages.add(LocationOptions.AFTER, aPage);
}