Automating Adobe InDesign 2019 - AppleScript


Paragraph Styles on Steroids

Tuesday, October 15, 2019

Paragraph Styles on Steroids

One of the advantages of using paragraph styles is that you can link them together using the Next style designation. For instance, the following story begins with a headline, followed by a byline, then a first paragraph with a drop cap. The rest of the story is styled using a style named "Body".

Sample of text styled for newsletter...Text having consistent style pattern

To set this up you will want to set the font, size, and leading for "[Basic Paragraph]" to be the same as "Body" style without a first line indent. With this in place, create a paragraph style for "Body" based on "[Basic Paragraph]" with a first line indent (or space before if no indent). Since there will be any number of "Body" paragraphs following, set Next Style to [Same style].

Next Style setting for Body paragraph style...Next Style setting for Body style

For the first paragraph (with a drop cap) you will most likely need a character style for the drop cap. The name for this paragraph could be "First" or "Dropcap" and be based on "[Basic Paragraph]" with its next style "Body".

Next style setting for First paragraph style...Next Style setting for First style

The paragraph style for the byline would be named "Byline", based on "[Basic Paragraph]", and with justification set to Left or Center as appropriate. Its Next Style would be "First".

To finish out the paragraph chain of styles, a style named "Headline" is created. Even though it will be at a much larger size than the Body or Byline styles, you would base it on "[Basic Paragraph]" if it uses the same font. Set Span to Span all columns and select Byline as its Next Style.

You get the idea. I refer to this as a "Chained Style Pattern."

Using a Chained Style Pattern

When text is entered into a text frame that has the paragraph style for its first paragraph established with a Chained Style Pattern, a line return starts the next paragraph with its styling the designated Next Style.

But what happens when text is placed from a file?

Placed Text

Plain text

When a plain text file is placed, the text comes in styled with the "[Basic Paragraph]" style. If this style is set up the same as for body text, at least some of the styling is taken care of. Using Quick Apply can help style the rest.

Microsoft Word

If the Microsoft Word document has been styled properly, the styles can be mapped to InDesign's paragraph styles as part of the import process. The problem is, even at best your InDesign document may end up with a lot of text overrides. At worst, everything may be styled "Normal". Map "Normal" to your "Body" style and, again, use Quick Apply.

Excel Files

Many firms are relying on Excel to act like a database for organizing data. This has its place, but .csv files don't retain character formatting, and tables have their own unique set of problems.

A Simple Script to the Rescue

For text that will have a uniform paragraph style pattern such as our example above, a simple script can use the Next Style designation to style paragraphs automatically. Here is one example:

Typical Newsletter

Assuming you have a document where styling has been set up as a Chained Style Pattern with the first paragraph style of the pattern being Headline, the following script can be used to import and style a chosen text file. It requires that a text frame be selected prior to running the script.

Hint: Place the script in InDesign's Scripts panel and run it from there (double click). Optionally, assign it a keyboard shortcut for even more trouble-free execution.

set firstStyle to "Headline"
try
	set insertRef to getSelectedInsert()
	set fileRef to getTextFile()
	placeAndStyle(firstStyle, insertRef, fileRef)
on error errStr
	activate
	display alert "Error: " & errStr
end try

(*Assumes paragraph styling is based on Next Style starting with name of style passed*)
on placeAndStyle(styleName, insertRef, fileRef)
   tell application "Adobe InDesign CC 2019"
	tell text import preferences
	   set use typographers quotes to true
	end tell
	set headStyle to paragraph style styleName of document 1
	set bodyStyle to paragraph style "[Basic Paragraph]" of document 1
	set applied paragraph style of insertRef to bodyStyle
	set thisStyle to headStyle
	tell insertRef
		place fileRef
	end tell
	tell parent story of insertRef
		set paraRef to a reference to paragraphs
		repeat with eachParagraph in paraRef
		    set applied paragraph style of eachParagraph to thisStyle
		    set nextStyle to next style of thisStyle
		    set thisStyle to nextStyle
		end repeat
	end tell
   end tell
end placeAndStyle

(*Has user select plain text file; verifies using kind or name extension; returns alias reference*)
on getTextFile()
   set fileRef to choose file with prompt "Select text file for placing"
   set theInfo to info for fileRef
   if kind of theInfo is "Plain Text Document" or name extension of theInfo is "txt" then
	return fileRef
   else
	error "Requires plain text file to be chosen"
   end if
end getTextFile

(*Returns frame for placing file using class of selection for text frame or insertion point*)
on getSelectedInsert()
   tell application "Adobe InDesign CC 2019"
	set selList to selection
	if selList is not {} and class of item 1 of selList is in {text frame, insertion point} then
	    set selItem to item 1 of selList
	else
	    error "Requires text frame selection"
	end if
	if class of selItem is text frame then
	    set insertRef to insertion point 1 of selItem
	else
	    set insertRef to selItem
	end if
    end tell
    return insertRef
end getSelectedInsert

To just place and style a single text file this script doesn't save a lot of time. On the other hand, considering the number of text files over a series of newsletters, the savings in time can add up. Start the newsletter document with a template that has the styles predefined, and you can start planning for a vacation with the time saved.

Onward and Upward

But wait, there's more! The same functionality can be applied to any number of documents. Think of other ways this automation could be applied. Maybe a car ad like the following that has "Make", "Model", and "Price" set up as a Linked Style Pattern. The style for "Price" takes advantage of nested styles and uses the first style ("Make") for its Next Style. Just change the value for the variable firstStyle in the script above to "Make."

Sample portion of car ad showing chained styles for Model, Description, and Price...Sample car ad items

Or maybe use this for a grocery ad having a pattern similar to the car ad only with "Item", "Description","Weight", and "Price". Set Start Paragraph (in Keep Options) for the "Item" style defined as "in Next Frame".

Example of grocery ad items set using chained paragraph styles...Sample grocery ad items

To make the script user friendly, add a choose from list dialog to the script above so user can choose the value for the first style. Change the top of the script to read as follows:

try
   set insertRef to getSelectedInsert()
   set fileRef to getTextFile()
   set firstStyle to getFirstStyle()
   placeAndStyle(firstStyle, insertRef, fileRef)
on error errStr
   activate
   display alert "Error: " & errStr
end try

Then add the following handler:

(*Returns user choice for paragraph style by name*)
on getFirstStyle()
   set stylePrompt to "Select style for first paragraph in Chained Style Patter"
   tell application "Adobe InDesign CC 2019"
      set styleList to name of paragraph styles of document 1
      set userChoice to choose from list styleList with prompt stylePrompt
      if userChoice is false then
         error "Requires selection to continue"
      end if
   end tell
   return item 1 of userChoice
end getFirstStyle

Now the script can be used for any number of documents that take advantage of Chained Paragraph styles.

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.

Comments
Post has no comments.
Post a Comment
Captcha Image
Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18424&PostID=1529778&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags

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

Archive