Find and Change

Many scripts work on the result of some find operation, which can be a Text find or a GREP one. We’ll start with some basics, outlining how you can automate find-and-replace operations. After that we turn to some more powerful applications of InDesign incredibly powerful GREP feature.

The Basics

Find (and change) are the start and at the heart of many scripts. In principle, scripting offers the same possibilities as the UI does, with one important exception: in the UI, the scope of the search can be set to either of five areas (all documents, current document, story, to end of story, or selection), whereas in scripting, you can search and replace in tables only or in footnotes only as well.

Of the four types of Find/Change available in InDesign’s interface, (Text, GREP, Glyph, and Object) we deal with only the first two, Text and GREP. GREP Find/Change is far more powerful than Text Find/Change, but it’s a bit more involved so we start with Text and show later how to deal with GREP.

Before you can do a Find/Change, you need to set some options. Each option is a separate line and corresponds with an option in the Find/Change dialog:

image with no caption

From left to right the symbols correspond with these lines:

app.findChangeTextOptions.includeLockedLayersForFind = true; app.findChangeTextOptions.includeLockedStoriesForFind = true; app.findChangeTextOptions.includeHiddenLayers = true; app.findChangeTextOptions.includeMasterPages ...

Get Scripting InDesign CS3/4 with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.