Your prescription for increased productivity and profitability
A recent post in the InDesign Scripting forum asked about working with paragraph styles using JavaScript. With this in mind, a blog post that deals with working with this subject using AppleScript seemed appropriate.
Styles can be created for an active document as well as for the application. If created for the application, the style is persistent, at least until removed or InDesign’s preference file is recreated (in which case the default preferences are restored). A style created at the application level is inherited by every document created thereafter.
To create a paragraph style at the document level, it is created within a tell statement to the document. The following script is a typical example. It first checks for the existence of the style. If the style does not exist, it is created. If it does exist, its properties are changed for use within the document only.
tell application "Adobe InDesign CC 2019" tell document 1 if (not (exists paragraph style "MyStyle")) then make paragraph style with properties {name:"MyStyle"} end if set parastyle to paragraph style “MyStyle” end tell tell parastyle set applied font to "Arial" & tab & "Regular" set point size to 18 set hyphenation to false set leading to auto set auto leading to 120 set justification to center align end tell end tell
To apply a style to text, a script first needs to verify that text or an insertion point is selected. After the paragraph style is verified. the text selected can be styled. Notice how the script from above is modified to serve as a handler that makes or modifies the style.
tell application "Adobe InDesign CC 2019" set docRef to document 1 tell docRef set selList to selection if selList is not {} and class of item 1 of selList is in {text, insertion point} then set selItem to item 1 of selList set parastyle to my getParaStyle("MyStyle", docRef) set applied paragraph style of selItem to parastyle else activate display alert ("Requires selection of type text or insertion point") end if end tell end tell (*Handler that checks for style by name. If not exists, the style is created. *) on getParaStyle(styleName, docRef) tell application "Adobe InDesign CC 2019" 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 tell parastyle —set properties as above end tell end tell return parastyle end getParaStyle
InDesign provides a paragraph style by default named “[Basic Paragraph]”. This style can be referred to by name or as one of the following:
paragraph style 2 of application applied paragraph style of text defaults
In creating paragraph styles, the default style often serves as the style on which other styles are created. In the example above the handler is modified to base the font being created on the style that is passed in the variable basicStyle. Notice how the properties for the style are passed as a record to the handler.
set styleName to "Head1" tell application "Adobe InDesign CC 2019" set docRef to document 1 set propRecord to {font style:"Bold", point size:24, hyphenation:false, leading:"24 pt", justification:center align} tell docRef set basicStyle to paragraph style "[Basic Paragraph]" end tell set newStyle to my getParaStyleBasedOn(styleName, docRef, basicStyle, propRecord) end tell newStyle (*Handler that tests for style and creates it as defined*) on getParaStyleBasedOn(styleName, docRef, basicStyle, propRecord) tell application "Adobe InDesign CC 2019" tell docRef if (not (exists paragraph style styleName)) then make paragraph style with properties {name:styleName, based on:basicStyle} end if set parastyle to paragraph style styleName end tell --docRef tell parastyle to set properties to propRecord end tell --application return parastyle end getParaStyleBasedOn
What is interesting in using based on is that when properties of the based on style change, so do the properties for the styles on which it is based. For this reason, the font is often the only property that is ever set for the based on style.
In creating a document you may decide to create a “master” style (such as a sans serif “Headline”) and leave [“Basic Style”] to serve as the based on style for styles which are serif. Imagine a template with the paragraph styles set up this way. The template could easily be used for just about any document meeting its basic structure. Simply change the font for the paragraph styles “[Basic Style]” and “Headline”; then fill with contents.
The next time you create a document that needs a number of styles, see if a script might provide some consistency and save you some time.
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.