Site logo

Page functions

Add pages function

if (story.overflows) {
	AddPages(story);
}

function AddPages(story) {
	var textFrame = story.textContainers[0],
	newPage, gb, newTextFrame, currentTextFrame,
	currentPage = textFrame.parentPage;

	while (story.overflows) {
		newPage = doc.pages.add(LocationOptions.AFTER, currentPage);
		currentPage = newPage;
		gb = GetBounds(currentPage);
		newTextFrame = currentPage.textFrames.add({geometricBounds: gb});
		textFrame.nextTextFrame = newTextFrame;
		textFrame = newTextFrame;
	}
}

Get page number by Dave Saunders

var myDoc = app.activeDocument;
var myObj = app.selection[0];
$.write("The selected object is on page " + findPage(myObj).name + "\n") ;
 
function findPage(theObj) { 
    var thePage = theObj; 
    if (thePage.hasOwnProperty("baseline")) { 
      thePage = thePage.parentTextFrames[0]; 
    } 
    while (thePage.constructor.name != "Page") { 
      var whatIsIt = thePage.constructor.name; 
      switch (whatIsIt) { 
        case "Story" : 
          thePage = thePage.textFrames[-1].parent; 
          break; 
        case  "Character" : 
          thePage = thePage.parentTextFrames[0]; 
          break; 
        case "Cell" : 
          try { 
            thePage = thePage.insertionPoints[0].parentTextFrames[0]; 
            break; 
          } catch (e) { 
            // must be overset, so ignore 
            return null; 
          } 
        case "Application" : 
          // must be off page, so ignore 
          return null; 
      } 
      thePage = thePage.parent; 
    } 
    return thePage 
  }

Get page number by Marc Autret

TextFrame.prototype.getPage = function()
// -----------------------------------------------
// Returns the containing Page of this TextFrame
{
var p = this.parent,
     pc;
while( pc=p.constructor )
     {
     if( pc == Page )
          return p;
     if( pc == Spread || pc == MasterSpread )
          throw Error("The textframe is placed on a spread!");
     if( 'parentTextFrames' in p && !(p=p.parentTextFrames[0]) )
          throw Error("The textframe's container overflows!");
     p=p.parent;
     }
}
 
// Sample code (assuming a text frame is selected)
// -----------------------------------------------
var tf = app.selection[0],
     pg;
     
try {
     pg = tf.getPage();
     alert( "Page: " + pg.name + " Side: " + pg.side);
     }
catch(e)
     {
     alert( e );
     }

Add pages at end

function AddPagesAtEnd(howMany) {
	var page, margPrefs, gb, textFrame,
	master = doc.masterSpreads.item("BBB-Blank"),
	docPrefs =doc.documentPreferences;
	
	do {
		page = doc.pages.add(LocationOptions.AT_END, doc);
		margPrefs = page.marginPreferences;
		if (master.isValid) page.appliedMaster = master;
		gb = [margPrefs.top, margPrefs.left, docPrefs.pageHeight - margPrefs.bottom, docPrefs.pageWidth - margPrefs.right];
		textFrame = page.textFrames.add({geometricBounds: gb});
		textFrame.previousTextFrame = doc.pages[-2].textFrames[0];

		howMany--;
	} while (howMany > 0)
}

Merge documents (via duplicate pages)

Merges all documents in a folder copying the pages — one by one — from every document at the end of the first document. Name the files in the order you want them to appear in the merged document (e.g. "001.indd", "002.indd", etc. or something) so they appear correctly when sorted by name in finder/explorer.

Main();

function Main() {
	var folder = new Folder("~/Desktop/Test/");
	var inddFiles = folder.getFiles("*.indd");
	
	MergeDocs(inddFiles);
}

function MergeDocs(inddFiles) {
	var inddFile, firstDoc, currentDoc, page;
	
	for (var i = 0; i < inddFiles.length; i++) {
		inddFile = inddFiles[i];
		
		if (i > 0) {
			currentDoc = app.open(inddFile);
			
			for (var p = 0; p < currentDoc.pages.length; p++) {
				page = currentDoc.pages[p];
				page.duplicate(LocationOptions.AT_END, firstDoc.pages[firstDoc.pages.length - 1]);
			}
		
			currentDoc.close(SaveOptions.NO);			
		}
		else {
			firstDoc = app.open(inddFile);
		}
	}
	
	firstDoc.close(SaveOptions.YES);
}