Dispersion Design

< Back

Cleaning Up Text Imported Into InDesign (Part 2)

Written: 2012-04-11

Introduction

In Part 1, I explained how excess spacing within text in Adobe® InDesign® can be quickly removed using GREP mode in the Find/Change… dialog box. We can now write a script to automatically perform the Find/Change… tasks, allowing text to be cleaned up with one click.

Selecting the Story to Operate On

First, since we want our script to operate on the currently selected story, we need to use the following commands to locate that selection:

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

Now we have the currently selected object, but what if nothing was selected or the selection is not a text frame? Let’s check for that:

if (!frame || frame.constructor.name !== "TextFrame") {
	var msg = "Please select a text frame containing";
	msg += " the story you wish to format";
	alert(msg);
}

Now that we know that we have a text frame, we need the 'story' associated with that frame:

var story = frame.parentStory;

Setting Find/Change Options With JavaScript

Now we need to reset the Find/Change options, making sure that any previous settings do not remain set:

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

Now we can set the ‘Find what:’ and ‘Change to:’ fields with:

// Remove all multiple spaces
app.findGrepPreferences.findWhat = "  +";
app.changeGrepPreferences.changeTo = " ";

Performing the Find/Change

Now that we have set the Find/Change options and selected the story to operate on, we can actually make the changes:

story.changeGrep();

Making Additional Changes to the Same Story

If we wish to perform additional changes to the same story, we can just change the appropriate Find/Change options and perform a changeGrep() again:

// Remove empty paragraphs and
// excess space at the end of paragraphs
app.findGrepPreferences.findWhat = " *\\r[ \\r]*";
app.changeGrepPreferences.changeTo = "\r";
story.changeGrep();

One important note is that backslashes must be 'escaped' in JavaScript. That means that you need to type TWO backslashes every time a backslash occurs in a field. It can get a little tricky with all the backslashes. For example, the expression to find multiple spaces between sentences: "([\.\!\?]) +" (see Part 1) must be typed in JavaScript as "([\\.\\!\\?]) +".

Putting it All Together

Our completed script is now:

// Get the current selection
var doc   = app.activeDocument;
var sel   = doc.selection;
var frame = sel[0];

if (!frame || frame.constructor.name !== "TextFrame") {
	var msg = "Please select a text frame containing";
	msg += " the story you wish to format";
	alert(msg);
} else {
	var story = frame.parentStory;

	// Reset the Find/Change options
	app.findGrepPreferences = NothingEnum.nothing;
	app.changeGrepPreferences = NothingEnum.nothing;

	// Remove all multiple spaces
	app.findGrepPreferences.findWhat = "  +";
	app.changeGrepPreferences.changeTo = " ";
	story.changeGrep();

	// Remove empty paragraphs and
	// excess space at the end of paragraphs
	app.findGrepPreferences.findWhat = " *\\r[ \\r]*";
	app.changeGrepPreferences.changeTo = "\\r";
	story.changeGrep();
}

Please feel free to download the completed RemoveExcessSpacing script and try it for yourself.