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)
}