Automating Adobe InDesign CC 2015


Adding Pages to InDesign Document

Monday, May 16, 2016

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

Two pages added to one page document with allow page shuffle set to false

This gives us a three-page spread, but the problem remains that the page in the middle (the spine) needs to be changed in size to the width of the spine. To change the width of a page:

  • first: set all of the affected margins to 0.
  • next: resize the page

Resizing the middle page will need to be done before the third page is added. Change the add page section of the scripts above to read as follows:

AppleScript

   --add page section 
   tell spread 1
	set allow page shuffle to false
	set pageRef to make page at end
	tell pageRef
	    set properties of margin preferences to {top:0, left:0, bottom:0, right:0}
	    resize in inner coordinates from left center anchor by replacing current dimensions with values {72, 648}
	end tell
	make page at end
   end tell

ExtendScript

   //add page section
   docRef.spreads.item(0).allowPageShuffle = false;
   var pageRef = docRef.spreads.item(0).pages.add(LocationOptions.AT_END);
        pageRef.marginPreferences.left = 0;
        pageRef.marginPreferences.right = 0;
        pageRef.resize(CoordinateSpaces.innerCoordinates, AnchorPoint.leftCenterAnchor, ResizeMethods.replacingCurrentDimensionsWith, [72, 648]);
   docRef.spreads.item(0).pages.add(LocationOptions.AT_END);

Resize center page to create spine for book cover spread

Onward and Upward

Scripts are written for demonstration purposes only. Hopefully the code above will give you a headstart when you have the occasion to add pages or change page sizes within a document. Feel free to use the above code as a starting point, but make sure that you trap for any possible errors in your final scripts.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18153&PostID=1461217&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags

say Video Media enable redraw Change Text Choose from list Read text file as list lists text concatenation import HTML Find Change automated workflow Adobe InDesign CC XML Embed Document Library Folder GPS automation Export HTML save template Radiobutton Group Metadata massage text measurements transform again Border Panel parent page InDesign template resolve AppleScript Find by Font BrdgeTalk XML structure Index of blogs and scripts for year 2016 with timeout arrays text item delimiters Excel cut and paste Glossary script folder user interaction level Autoflow Custom Dialog External Object Scripts Caption Variable Hyperlinks Templates AppleScript Templates selection active spread scripting HTML Export table styles PDF security wild cards watermark Checkbox Control pasteboard InDesign CC Fixed Layout ePub Tables Numbering text placement Find Text trimText Repeat With repeat loops InDesign dialogs XML tags new document import XML Document stylesheets paragraph styles System Events Text Cleanup CALS tables script templates Editbox Table cells PDF Choose file path Adding Captions functions fit Nested Styles Bullets AppleScript dialogs file paths measurement editboxes do script GPS metadata resize metacharacters Find by Attribute Automating animations try/on error Combobox Placing images doScript Document from Template Publish Online reframe handlers dropdowns transformation matrix CSS folder directory Export to PDF ePub for iPad if/then Character Styles import text integer editboxes image metadata Fixed Format ePub entire path records find change options ePub for Kindle tutorial Apple apps placeable Custom CSS Style Sheets object styles Sound page items images parent Mail Create Text Frame Document Presets styles PDF export preferences Image Placement stylesheets Find by Color Create Document coordinates Custom Buttons Interactive Publications Styling Text automating InDesign Script Preferences Enabling Groups transform text editbox Preferences

Archive