Your prescription for increased productivity and profitability
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.
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.
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.
…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.
--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.
--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 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.
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.