ADDING PAGES TO DOCUMENTS

When you add a page to a document using a script, you can specify whether the page will be added to its beginning or end. You can also specify that the page will be added before or after a referenced page.

AppleScript

   tell application "Adobe InDesign CC 2015"
	set docRef to make document
	set properties of document preferences of docRef to ¬
{page height:"5 in", page width:"5 in", pages per document:4, facing pages:false}
	repeat with i from 1 to 4
	   tell page i of docRef
	      set frameRef to make text frame with properties ¬
{geometric bounds:{".5 in", ".5 in", "4 in", "4 in"}, contents:("page " & i)}
	      set properties of paragraph 1 of frameRef to ¬
{point size:"72 pt", justification:center align, color:black}
	   end tell
	end repeat
	tell docRef
	   make page at after page 1
	end tell
   end tell 

ExtendScript

   var docRef = app.documents.add();
   with (docRef.documentPreferences) {
      pageHeight = "7 in";
      pageWidth = "5 in";
      pagesPerDocument = 4;
      facingPages=false;
   }
   var pageRef = docRef.pages.item(0);
   for (i = 0; i < 4; i++) {
      var frameRef = docRef.pages[i].textFrames.add();
      frameRef.geometricBounds = [".5 in", ".5 in", "6.5 in", "4.5 in"];
      frameRef.contents = ("page " + i);
      frameRef.textFramePreferences.verticalJustification=
VerticalJustification.CENTER_ALIGN;
      frameRef.paragraphs[0].pointSize="72 pt";
      frameRef.paragraphs[0].justification= Justification.CENTER_ALIGN;
   }
   docRef.pages.add(LocationOptions.AFTER, pageRef);

You can also use a reference to a spread for adding pages, as you will see in the next section.

CENTER FOLD-OUT

Suppose that you are working on a brochure that needs a center page fold out. You’ve seen it before: all of the pages in the document are two-page spreads, but the center page on the right hand side folds out to make a three-page spread. The key to creating this is allow page shuffle for the affected page.

Here’s a simple example. The following scripts create a document with four facing pages. Just for illustration, it then creates a text frame on each page with text displaying the index of the page in 72 point type.

AppleScript

   tell application "Adobe InDesign CC 2015"
      set measurement unit of script preferences to points
      set docRef to make document
      tell docRef
	 tell document preferences
	     set properties to ¬
{page height:648, page width:432, facing pages:true, pages per document:4, ¬
page orientation:portrait, create primary text frame:false, intent:print intent, allow page shuffle:false}
	 end tell
	 repeat with i from 1 to count of pages
	      tell page i
		  set framePrefs to ¬
{text column count:1, vertical justification:center align}
		  set frameRef to make text frame with properties ¬
{geometric bounds:{36, 36, 612, 396}, fill color:"None", contents:("Page " & i)}
		  set properties of text frame preferences of frameRef to framePrefs
		  set properties of paragraph 1 of frameRef to ¬
{point size:72, justification:center align}
	      end tell
	end repeat
      end tell
   end tell

ExtendScript

   app.scriptPreferences.measurementUnit=MeasurementUnits.POINTS;
   var docRef = app.documents.add(true);
   with (docRef.documentPreferences) {
      pageHeight = 648;
      pageWidth = 432;
      facingPages = true;
      pagesPerDocument = 4;
      pageOrientation=PageOrientation.PORTRAIT;
      createPrimaryTextFrame=false;
      intent=DocumentIntentOptions.PRINT_INTENT;
      allowPageShuffle = true;
    }
    //add page section will go here
   var pageCount = docRef.pages.count();
   for (var i = 0; i < 4; i++) {
      var frameRef = docRef.pages[i].textFrames.add();
      with (frameRef) {
        geometricBounds = [36, 36, 612, 396];
        fillColor = "None";
        contents = ("Page " + i);
        textFramePreferences.textColumnCount = 1;
        textFramePreferences.verticalJustification = 
VerticalJustification.CENTER_ALIGN;
        paragraphs[0].pointSize = 72;
        paragraphs[0].justification = Justification.CENTER_ALIGN;
      }
   }

Notice that the value for allow page shuffle for the document in the above was set to true. This is its setting by default. When a page is added inside the document, this setting allows pages to shift their position within the document to allow for the new page. We can demonstrate this by adding the following code to our sample scripts above. This will be placed before the repeat loop that creates the text frames.

AppleScript

   --add page section
   tell spread 2
      make page at end
   end tell

ExtendScript

   //add page section
   docRef.spreads.item(1).pages.add(LocationOptions.AT_END);

Page added to document with allow page shuffle set to true

By changing the value of allow page shuffle from true to false, we can force the spread to add the page without shuffling the pages. Try the script by changing the above added statement block to:

AppleScript

--add page section
   tell spread 2
      set allow page shuffle to false
      make page at end
   end tell 

ExtendScript

   //add page section
   docRef.spreads.item(1).allowPageShuffle = false;
   docRef.spreads.item(1).pages.add(LocationOptions.AT_END);

 

With page shuffle set to false, a tree page center foldout is created

BOOK COVER WITH SPINE

Another common situation that involves page shuffling is in creating a book cover with a narrow spine between the front and back covers. For this example, the script will create a one page document. This page will represent the left-facing page or back cover for the book. The script will then add two pages: one for the spine and one for the front page.

AppleScript

   tell application "Adobe InDesign CC 2015"
	set measurement unit of script preferences to points
	set docRef to make document
	tell docRef
	   tell document preferences
		set properties to {page height:432, page width:648, facing pages:false, pages per document:1, ¬
page orientation:portrait, create primary text frame:false, intent:print intent, allow page shuffle:true}
	   end tell
           --add page section
	   tell spread 1
		set allow page shuffle to false
		make page at end
		make page at end
	   end tell
	end tell
   end tell

ExtendScript

   app.scriptPreferences.measurementUnit=MeasurementUnits.POINTS;
   var docRef = app.documents.add(true);
   with (docRef.documentPreferences) {
       pageHeight = 648;
       pageWidth = 432;
       facingPages = false;
       pagesPerDocument = 1;
       pageOrientation=PageOrientation.PORTRAIT;
       createPrimaryTextFrame=false;
       intent=DocumentIntentOptions.PRINT_INTENT;
       allowPageShuffle = true;
   }
   //add page section
   docRef.spreads.item(0).allowPageShuffle = false;
   var pageRef = docRef.spreads.item(0).pages.add(LocationOptions.AT_END);
   var pageRef = docRef.spreads.item(0).pages.add(LocationOptions.AT_END);