Automate Documents With Styling

In the previous post we demonstrated how using paragraph and object styles can facilitate styling documents as well as HTML export. Additionally, we allowed our object style to be associated to a paragraph style to style its content. But there is more. We can set up a chain of paragraph styes to style up a number of paragraphs within a text flow using Next Style

NEXT PARAGRAPH STYLE

As part of defining paragraph styles in a text flow, we can automate styling using Next Style. Here is how it works. The following screenshot is a typical example of an article that might appear in a magazine.

…sample of styled text

Notice the styling for each of the paragraphs, starting with a Kicker, followed by Headline1, Headline2, DropCap, and, lastly, Body. The trick is to start by setting styling for [Basic Paragraph] the same as you would set the styling for the Body text: font, size, leading, etc. but without a first line indent, spacing above or below.
Next create styling for each of the styles in the body text flow, starting with the body text (“Body”) based on [Basic Paragraph] but adding first line indent, spacing before, etc. You then work up from Body style to the first style in the flow. For the screenshot sample text above, the style to set after Body is the one for the dropcap. Style “Dropcap” based on [Basic Paragraph] with its Next Style as “Body”. From Dropcap you next set the style for Headline2, then Headline1, and, finally, Kicker. For each style select [Basic Paragraph] as the Based On style, and the style created previously as its Next Style. The horizontal rules above and below the Kicker text are Paragraph Rules as is the horizontal rule below Headline2. As you set properties for each paragraph style, make sure you override any styling not inherited from the based on style.

Now that you have created a style pattern, create an object style for a text frame based on Basic Text Frame. You might want to name it Kicker since that is the first style in the paragraph style “chain.” In the Object Style, select Paragraph Styles and designate Kicker as the Paragraph Style and check Apply Next Style.

.Setting object style

USING TEMPLATES

Once you have assigned styling for all text frames in a document, save it as a template (.indt). Where you save your templates is up to you. I like to put my templates in a folder named Templates inside InDesign’s Presets folder. (The scripts following assume that this is the folder in which the templates will be found.)

When it comes time to use a template and its styles, you can use a script to start a new document complete with page styling, object styles, and paragraph styles. The script sample below should get you started:

DocFromTemplate

set savePathPrompt to "Select file and folder for save"
set defaultPath to path to home folder from user domain
tell application "Adobe InDesign CC 2017"
   set appPath to file path as string
   set templatePath to appPath & "Presets:Templates"
   set templateList to list folder templatePath without invisibles
   set templateChoice to choose from list templateList ¬
without multiple selections allowed
   set docRef to open templatePath & ":" & templateChoice
   set savePath to choose file name with prompt ¬
savePathPrompt default location defaultPath
   --make sure name of file ends with .indd
   if savePath does not end with ".indd" then
       set fileName to fileName & ".indd"
       set savePath to fileName as alias
   end if
   --save the file without saving over existing
   save docRef to savePath without force save
end tell

You can also use a template to start a completely new project using only the styles from the template. The next sample script, demonstrates:

NewDocWithStyles

set choosePrompt to "Select template from list"
tell application "Adobe InDesign CC 2017"
   set measurement unit of script preferences to points
   set appPath to file path as string
   set templatePath to appPath & "Presets:Templates"
   set templateList to list folder templatePath without invisibles
   set stylesheetName to choose from list templateList with prompt ¬
choosePrompt without multiple selections allowed
   set stylesheetRef to templatePath & ":" & stylesheetName
   set docRef to make document
   tell docRef
	set properties of document preferences to {intent:print intent, ¬
page width:"8.5 inches", page height:"11 inches", facing pages:false, ¬
create primary text frame:true, pages per document:2}
	import styles format object styles format from stylesheetRef
	import styles format text styles format from stylesheetRef
	tell page 1
	   set frameRef to make text frame with properties ¬
{geometric bounds:{36, 36, 10 * 72, 6 * 72}}
	end tell
	if (exists object style "Kicker") then
	   set textToImport to my getImportFile()
	   set applied object style of frameRef to object style "Kicker"	
	   tell frameRef
		place textToImport
		clear object style overrides
	   end tell
	end if
	end tell
end tell
(*Allows user to choose file for placing*)
on getImportFile()
   set filePath to path to desktop from user domain
   activate
   set fileRef to choose file with prompt "choose file to import" default location filePath
   return fileRef
end getImportFile

Try It

With Adobe InDesign running in the foreground, try out the scripts above. The second script requires a text file saved in plain text format. The script designates the Desktop as the default location for the file, but the user can navigate to wherever the file is saved.

Imagine an entire page, or an entire document, composed using similar scripts to create documents, import text, and assign object styles with paragraph styles assigned to the text imported. The only problem is in having to access the script.

SAVING SCRIPTS

When you have a script working the way you want it, don’t forget to add try/on error/end try statements to catch user-produced errors. Save the script and assign a keyboard shortcut to manage and use the script.

Assigning Keyboard Shortcuts to Scripts

The most efficient way to manage scripts is to add them to your User scripts folder and assign keyboard shortcuts. Here’s how.

  • Save your script somewhere handy (maybe on the Desktop)
  • Open the Scripts panel from inside InDesign (Window > Utilities > Scripts)
  • Scroll down the list of scripts in the panel to find the User folder

…User folder in Scripts panel

  • Control-click on the User folder and select Reveal in Finder

…Reveal in Finder opens the folder in the Finder

  • In the Finder dialog that opens, drag your script(s) into the User folder
  • In InDesign, open the Keyboard Shortcuts window (Edit > Keyboard Shortcuts)
  • If you haven’t started your own set for your scripts, click on New Set and give your script set a name
  • Choose your Script Set from the dropdown under Set
  • Choose Scripts from the Product Area
  • Scroll down toward the bottom of the script ist and find the User folder
  • Click on the script to which you want to assign a keyboard shortcut
  • Click in the New Shortcut field and type in the shortcut (I used Option + Shift + N for New Document from Template)
  • Click Assign (Assignment will appear in Current Shortcuts list)
  • Once you have assigned your shortcuts, click OK

…Setting keyboard shortcuts

Be consistent in assigning keyboard shortcuts. I find the keyboard combination of option plus shift and an alpha character to be fairly open for assigning shortcuts. If you start getting a long list of Keyboard Shortcuts, you might want to keep a cheat sheet close by.

Remember

In preparing text files for import, use paragraph returns exclusively to end a paragraph and start a new one. If you need to use forced returns (shift + return) to end lines within a paragraph, save those for after you have imported the text unless you know your imported text document will not interpret the forced return as a paragraph return.

ONWARD AND UPWARD

The procedure outlined for the NewDocWithStyles script above is fairly simple but not error-proof. It works great for users who are careful in how they set up paragraph styles and in creating text files for import. For the ultimate in control, using XML is suggested. We will cover working with XML in a later blog post.