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

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

Archive