Dispersion Design

< Back

Automatically Applying Paragraph Styles in InDesign

2012-04-12

Introduction

Many documents that I receive have each paragraph in the document individually formatted. The original writer carefully applied formatting, such as font family and font size, to each heading, table title and list in the document, without using any paragraph styles. This means that global document changes, such as changing the font of every heading in the document, becomes a tedious task.

When editing and formatting such a document in Adobe® InDesign®, you can use the Find/Change feature to automatically apply paragraph styles to specific paragraphs throughout the entire document, speeding up the formatting process. This can be automated by writing an InDesign script using JavaScript.

The applyParagraphStyle() function

We would like to write a generic function that searches for a specific text string of a specific text size and applies a paragraph style to that paragraph.

First we need to reset the Find/Change preferences:

app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

Then we set the text and size that we wish to search for:

app.findGrepPreferences.findWhat = grepStr;
app.findGrepPreferences.pointSize = size;

We need to select the paragraph style that we wish to apply. If the paragraph style does not exist, we will create a new paragraph style:

var styles = app.activeDocument.paragraphStyles;
var style;
try {
	style = styles.item(styleName);
	var n = style.name;
}
catch (myError) {
	style = styles.add({
		name: styleName,
		pointSize: size
	});
}
app.changeGrepPreferences.appliedParagraphStyle = style;

Now we can apply the changes:

story.changeGrep();

If our paragraph styles define automatic numbering, we may also want to remove the text that we originally searched for (e.g. If we search for "^([1-9][0-9]*\\.){2} +", "1.2. My Great Heading" becomes "My Great Heading"):

app.changeGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences.changeTo = "";
story.changeGrep();

The complete function would then be:

function applyParagraphStyle(story, grepStr, size, styleName) {
	app.findGrepPreferences = NothingEnum.nothing;
	app.changeGrepPreferences = NothingEnum.nothing;
	app.findGrepPreferences.findWhat = grepStr;
	app.findGrepPreferences.pointSize = size;
	var styles = app.activeDocument.paragraphStyles;
	var style;
	try {
		style = styles.item(styleName);
		var n = style.name;
	}
	catch (myError) {
		style = styles.add({
			name: styleName,
			pointSize: size
		});
	}
	app.changeGrepPreferences.appliedParagraphStyle = style;
	story.changeGrep();
	app.changeGrepPreferences = NothingEnum.nothing;
	app.changeGrepPreferences.changeTo = "";
	story.changeGrep();
}

Styling A Document

Now that we have the applyParagraphStyle() function, we can automatically apply multiple paragraph styles to a document. The following table shows some paragraph style examples:

Style NameFont sizeParagraph ExampleGREP string
Heading 1134. Problem Description ^[1-9][0-9]*\\. +
Heading 2102.2. Examples ^([1-9][0-9]*\\.){2} +
Heading 3107.15.1. Register Map ^([1-9][0-9]*\\.){3} +
Figure Title10Figure 8: Graph showing progress ^Figure [1-9][0-9]*: +
Table Title10Table 9^Table [1-9][0-9]* *
Bulleted List10• Elements were fully defined^• +

All the paragraph styles above can be applied to the story within an InDesign text frame with the following JavaScript code:

var doc		= app.activeDocument;
var sel		= doc.selection;
var frame	= sel[0];

if(frame && frame.constructor.name === "TextFrame") {

	var story = frame.parentStory;

	applyParagraphStyle(story, "^[1-9][0-9]*\\. +",
		13, "Heading 1");

	applyParagraphStyle(story, "^([1-9][0-9]*\\.){2} +",
		10, "Heading 2");

	applyParagraphStyle(story, "^([1-9][0-9]*\\.){3} +",
		10, "Heading 3");

	applyParagraphStyle(story, "^Figure [1-9][0-9]*: +",
		10, "Figure Title");

	applyParagraphStyle(story, "^Table [1-9][0-9]* *",
		10, "Table Title");

	applyParagraphStyle(story, "^• +",
		10, "Bulleted List");

} else {
	alert("Please select a text frame containing \
the story you wish to format");
}