PARAGRAPH STYLES

In starting a new document, having to set up a number of paragraph styles can seem like a lot of extra work. A simple script that I often use is one that creates a number of paragraph styles based on the default paragraph style (“[Basic Paragraph]”). For this script, I start with a document that has only the default paragraph style. Then I open the script from InDesign’s Scripts panel and change the list at the top that defines the paragraph styles. Run the script and most of the work is done. Of course a custom dialog could be added to allow the script to be run from the Scripts panel or using a keyboard shortcut. Using a script such as this works best for those projects that use a consistent naming convention for naming the styles.

Create Styles Based On

set styleList to {{"Kicker", 18, 18, 0, "Italic"}, {"Headline", 24, 24, 18, "Bold"}, ¬
{"Byline", 14, 14, 18, "Bold Italic"}, {"Quickread", 14, 16, 12, "Italic"}, ¬
{"First", 10, 12, 12, "Regular"}, {"Body", 10, 12, 0, "Regular"}}
set basicFont to "Minion Pro" & tab & "Regular"
set flIndent to 12 --first line indent for "Body"
tell application "Adobe InDesign CC 2019"
   set headJust to center align --justification for headlines
   set measurement unit of script preferences to points
   set docRef to document 1
   tell docRef
      set basicStyle to paragraph style "[Basic Paragraph]"
      set applied font of basicStyle to basicFont
   end tell
   repeat with i from 1 to length of styleList
      copy item i of styleList to {xName, xSize, xLead, xBefore, xStyle}
      set propRecord to {point size:xSize, leading:xLead, space before:xBefore, font style:xStyle}
      my getParaStyleBasedOn(xName, docRef, basicStyle, propRecord, headJust, flIndent)
   end repeat
end tell

(*Creates style if not found. Sets properties for style*)
on getParaStyleBasedOn(styleName, docRef, basicStyle, propRecord, headJust, flIndent)
   tell application "Adobe InDesign CC 2019"
      if styleName is in {"First", "Body"} then 
         set xRecord to {based on:basicStyle, justification:left align, hyphenation:true, first line indent:flIndent}
      else if styleName begins with "Head" then
         set xRecord to {based on:basicStyle, justification:headJust, first line indent:0, hyphenation:false}
      else
         set xRecord to {based on:basicStyle, first line indent:0}
      end if
      tell docRef
         if (not (exists paragraph style styleName)) then
            make paragraph style with properties {name:styleName}
         end if
         set parastyle to paragraph style styleName
      end tell --docRef
      tell parastyle to set properties to (propRecord & xRecord)
   end tell --application
return parastyle
end getParaStyleBasedOn

Notice in the getParaStyleBasedOn handler how property records can be combined to set different hyphenation and justification styling for headlines and body text.

CHAINED STYLES (NEXT STYLE)

One big advantage of using paragraph styles is in taking advantage of the Next Style Property. This allows a number of paragraph styles to be applied in a set pattern automatically as paragraphs are created. Add the following changes to the script above to define the pattern in which the next style property will be applied:

    1. At the top of the script add the following. The variable nextList defines the order in which the paragraph styles will have the next style property set:
set nextList to {"Kicker", "Headline", "Byline", "Quickread", "First", "Body"}
    1. After the end repeat at the bottom of the main portion of the script add a call to the createNextStyles handler:
my createNextStyles(docRef, nextList)
  1. Add the handler <strong<>createNextStyles to the bottom of the script:</strong<>
(*Sets up next style pattern for styles*)
on createNextStyles(docRef, nextList)
   tell application "Adobe InDesign CC 2019"
	tell docRef
	   set thisStyleName to item 1 of nextList
	   repeat with i from 1 to (length of nextList) - 1
		set thisStyle to paragraph style thisStyleName
		set nextStyleName to item (i + 1) of nextList
		set next style of thisStyle to paragraph style nextStyleName
		set thisStyleName to nextStyleName
	   end repeat
	end tell
   end tell
end createNextStyles

With the Create Styles Based On script modified, run it with a new document open. Then, inside of a text frame, select the first paragraph style from the nextList (“Kicker”). Type in text for six paragraphs (with a return after each). Notice that the text styling advances to the next style in the list with each paragraph return,

Note: To end a line without a new paragraph (to keep the styling for the lines the same), add shift with the return.

OBJECT STYLES

If you have a number of text frames that use the same text styling pattern (and even if you don’t), set up an object style using the first style of the paragraph style pattern as its Paragraph Style with Apply Next Style checked.

shows Create Object Style dialog with Apply Next Style checked…New Object Style dialog

You could also use a script to create the object style (more on that next week).

With this object style created, add a new text frame to your document and set its style to your object style. Type in six paragraphs to this text frame as above.

Save your document somewhere handy. If you have administrator privileges, you might want to use the folder named Styles in the Application’s folder.

ADD TO OBJECT STYLE

Once you have an object style established with paragraph styles set up with a Next Style pattern as above, you can apply the styles automatically to text that is imported as well as to text that is entered within the document. The following script assumes that there is an object style set up for text frames with apply next style enabled. You will also want a text file with at least six paragraphs for testing

(*Assumes object style named exists for active document*)
set objectStyleName to "ChainedStyles"
try
   set frameRef to applyObjStyle(objectStyleName)
   importText(frameRef)
on error errStr
   activate
   display alert errStr
end try

(*Assumes object style named exists. Applies object style to selected text frame*)
on applyObjStyle(objectStyleName)
   tell application "Adobe InDesign CC 2019"
	set selList to selection
	if selList is not {} and class of item 1 of selection is text frame then
	   set objStyle to object style objectStyleName of document 1
	   set frameRef to item 1 of selection
	   set applied object style of frameRef to objStyle
	else
	   error "Requires text frame selection"
	   end if
	end tell
   return frameRef 
end applyObjStyle

(*imports and styles text based on object style*)
on importText(frameRef)
   set fileRef to choose file with prompt "Select file for text import"
   tell application "Adobe InDesign CC 2019"
      tell frameRef
	 place fileRef
      end tell
      tell frameRef
	 clear object style overrides --overrides to clear paragraph only
      end tell
   end tell
end importText

The script above uses clear object style overrides to make the paragraph styles effective. It would be improved to avoid throwing an error) if it were to check for the existence of the object style.

Apply Object Style

Alternatively InDesign also has the  apply object style command which can be used to clear overrides so the paragraph style chaining will work. The script below first checks for a selected text frame, then makes sure the document has the necessary object style. With a reference to these two objects, it has the user choose the text file to import. Notice that the text is imported before the apply object style command is applied.

(*Requires document with object style ChainedStyles*)
set objectStyleName to "ChainedStyles"
try
   set frameRef to getSelectedFrame()
   set objectStyleRef to getObjectStyle(objectStyleName)
   importAndStyleText(frameRef, objectStyleRef)
on error errStr
   activate
   display alert errStr
end try

(*Verifies existence of object style using name passed*)
on getObjectStyle(objectStyleName)
   tell application "Adobe InDesign CC 2019"
      tell document 1
	 if exists object style objectStyleName then
	    set objStyle to object style objectStyleName
	 else
	    error "Object style " & objectStyleName & " not found in active document"
	 end if
      end tell
      return objStyle
   end tell
end getObjectStyle

(*Returns reference to selected text frame if selected*)
on getSelectedFrame()
   tell application "Adobe InDesign CC 2019"
      set selList to selection
      if selList is not {} and class of item 1 of selection is text frame then
	 set frameRef to item 1 of selection
      else
	 error "Requires text frame selection"
      end if
   end tell
   return frameRef
end getSelectedFrame

(*imports and styles text from chosen text file based on object style*)
on importAndStyleText(frameRef, objStyleRef)
   set fileRef to choose file with prompt "Select file for text import"
   tell application "Adobe InDesign CC 2019"
      tell frameRef
	 place fileRef
	 apply object style using objStyleRef with clearing overrides
      end tell
   end tell
end importAndStyleText

ONWARD AND UPWARD

Think of a recurring project that uses paragraph styles in a consistent pattern. Set up a document with these styles as part of an object style. The next time you need to create a document with these styles, create your new project using the template. More about this in our next blog post.

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.