Automating Adobe InDesign 2019 - AppleScript


Automate Text Placement

Tuesday, June 18, 2019

Automate Text Placement

On InDesign's Scripting forum is not uncommon to get a request for a script that has something to do with placing text and images. A recent request states in part: I have a number of sentences that I want to distribute to pages, one text frame per page, one sentence per text frame.

One solution to the problem is to have a document set up to use a primary text frame on the master page for the sentences. This way the first text frame of the document can place the text file. If a paragraph style with start paragraph set to next frame (or next page) is applied, each paragraph will begin on a separate page as specified.

This is a fairly simple manual task, but could be something that might be done repeatedly. Let's take a look at this from the perspective of automating the process with a script.

Auto Flow Document

First we will need a document that has the primary text frame property set to true. (It will have a text frame on the master A spread which will allow autoflow). This could be built using a document preset but for demonstration we will use document and page margin preferences.

--Variables; measurements are in inches decimal
set pgWid to 5.0 
set pgHgt to 7.0
set doFacing to false
set docMargins to {1.0, 0.5, 0.75, 0.5} --top, left, bottom, right

--Main section of script
tell application "Adobe InDesign CC 2019"
   set measurement unit of script preferences to inches decimal
end tell
set docRef to autoflowDoc (pgWid, pgHgt, doFacing, docMargins)

--Handlers
(*Creates document with create primary text frame set to true*)
on autoflowDoc(pgWid, pgHgt, doFacing, docMargins)
   copy docMargins to {mTop, mLft, mBot, mRgt}
   tell application "Adobe InDesign CC 2019"
	set docRef to make document
	tell docRef
	   set properties of document preferences to ¬
{page width:pgWid, page height:pgHgt, intent:print intent, facing pages:doFacing, create primary text frame:true}
	   tell page 1 of master spread 1
		set properties of margin preferences to ¬
{top:mTop, left:mLft, right:mRgt, bottom:mBot}
	   end tell
	end tell
   end tell
   return docRef
end autoflowDoc

If you run the script at this point you will have your document with a full page primary text frame created on master spread A conforming to the default document's margin settings. Our user did not specify, but we will assume that the text frame for the paragraphs will be located toward the bottom of the page, perhaps 1 or 2 inches in height. The script will now set the geometric bounds for the primary text frame.

Primary Text Frame

For the purpose of the script we will make the text frame 1 inch high at the bottom of the page. For this we can add the following to our script above:

--In the Variables section:
set frameHgt to 1.0 --height of master text frame

--add to bottom of Main section of script
setMasterFrame (docRef, docMargins, frameHgt)

--With Handlers
(*Sets primary text frame full width at page bottom given height of frame*)
on setMasterFrame(docRef, docMargins, frameHgt)
   copy docMargins to {mTop, mLft, mBot, mRgt}
   tell application "Adobe InDesign CC 2019"
      tell docRef
         set masterPage to page 1 of master spread "A-Master"
         tell masterPage
            copy bounds to {py0, px0, py1, px1}
            if side is left hand then
               set x0 to mRgt
               set x1 to px1 - mLft
            else
               set x0 to mLft
               set x1 to px1 - mRgt
            end if
            set y1 to py1 - mBot
         end tell
         tell text frame 1 of masterPage
            set properties to {geometric bounds: {y1 - frameHgt, x0, y1, x1}}
         end tell
      end tell
   end tell
end setMasterFrame

In the setMasterFrame handler above, the test for side could be eliminated. However, to make the handler work for a document having facing pages with uneven side margins, the test was included. If you run the script at this point, your page should look like the screen capture following.

Shows page for document created with script ...Document page created.

Now that you have the document with the master test frame positioned, you need to make sure that when the text is imported it will flow to separate pages. For this the script will create a paragraph style having the start paragraph property set to next frame or next page. For our purpose, we will base the style on the paragraph style "[Basic Paragraph]". This way the style will inherit most of its properties from the default.

Start Paragraph Style

--Add to Variable section
set styleName to "NewPage"
set ptSize to "14 pt"
set fStyle to "Italic"
set baseStyleName to "[Basic Paragraph]"
set doHyphen to false
set textAlign to "center" --can be "center" or "left"; otherwise inherits justification for based on

--Add to bottom of Main section
set parastyle to startonParastyle (docRef, styleName, baseStyleName, ptSize, fStyle, doHyphen, textAlign)

--Add to Handlers
(*Creates paragraph style using based on
Justification can be set for "center" or "left" otherwise inherited from based on*)
on startonParastyle(docRef, styleName, baseStyleName, ptSize, fStyle, doHyphen, textAlign)
   tell application "Adobe InDesign CC 2019"
	tell docRef
	   set baseStyle to paragraph style baseStyleName
	   if not (exists paragraph style styleName) then
	      set styleRef to make paragraph style with properties {name:styleName}
	   else
	      set styleRef to paragraph style styleName
	   end if
	   set properties of styleRef to {based on:baseStyle, point size:ptSize,¬
  font style:fStyle, leading:auto, hyphenation:doHyphen, start paragraph:next page}
	   if textAlign is "center" then
	      set justification of styleRef to center align
	   else if textAlign is "left" then
	      set justification of styleRef to left align
	   end if
	end tell
   end tell
   return styleRef
end startonParastyle

With all of the pieces in place, the script can set the insertion point for the first text frame to the paragraph style and import the text file.

Place Text and style

--Add to the bottom of the Main section of the script
activate
set fileAlias to choose file with prompt "Choose file to read for text"
placeFile (docRef, fileAlias, parastyle)

--Add to Handlers
(*Sets paragraph style for file import and imports text file alias referenced*)
on placeFile (docRef, fileAlias, parastyleRef)
   tell application "Adobe InDesign CC 2019"
	tell text frame 1 of page 1 of docRef
	   set applied paragraph style of insertion point 1 to parastyleRef
	   place fileAlias
	end tell
   end tell
end placeFile

Test the script with a plain text file containing a number of short paragraphs and each page of your document will look similar to the following screenshot.

Page with text flowed into text frame...Page with text imported.

Now all that is needed are some images to place. Maybe add a cover and you have a document that could be saved as a PDF flip book or Publish Online.

Onward and Upward

Our purpose here was to show how each step of the process can be written and to provide handlers that could be used for any number of scripts. You will need to add your own error handling. To make the script user friendly, a custom dialog will be required which will not be easy as there are a fair number of variables that need to be defined. You could simplify the process by using a document template, or, in the minimum, import the paragraph style from a style sheet.

Disclaimer:
Scripts provided are for demonstration and educational purposes. No representation is made as to their accuracy or completeness. Readers are advised to use the code at their own risk.

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

Recent Posts


Tags

primary text frame file paths Change Text date objects Script Library tab list Chaining Paragraph Styles giving up after threaded text frames info for Image Events Autoflow Editbox find change options rest of list InDesign template table styles Keyboard Shortcuts script folder text import preferences Templates Choose from list move file paragraph styles Script panel random number live bounds Previous text frame Next Style Adjust Layout dropdowns page items Text Style Mapping Choose file Styling Text next text frame Checkbox Control draw from center Custom Dialog user interaction level file info Library Folder Write to File import XML start paragraph CALS tables lists dialog Multi-state objects radiobutton control static alignment entire path import text GREP Buttons XML tags Excel selection Border Panel Image Placement measurement editboxes Placing images Combobox sort list StandardAdditions object styles rest text item delimiters XML Create Document bookmarks map tags to styles reference to Character Styles auto tag Place text time XML structure image metadata Check for Styles try/on error repeat loops stacking order measurements active spread Find Text Apply Object Style fit grid Clearing Overrides place point handlers import styles Script Preferences Document Presets Create Text Frame Radiobutton Group Document stylesheets tabs lists of lists Read text file as list intent do script automated workflow master spread coordinates Document from Template convert to text Repeat With Enabling Groups script templates Export to PDF Adding Captions Find by Attribute choose file name

Archive