CHAPTER 6: Text and Type Finding and Changing Text 100
Finding and Changing Text
The find/change feature is one of the most powerful InDesign tools for working with text. It is fully
supported by scripting, and scripts can use find/change to go far beyond what can be done using the
InDesign user interface. InDesign has three ways of searching for text:
X You can find text and/or text formatting and change it to other text and/or text formatting. This type
of find/change operation uses the
findTextPreferences and changeTextPreferences objects to
specify parameters for the
findText and changeText methods.
X You can find text using regular expressions, or āgrep.ā This type of find/change operation uses the
findGrepPreferences and changeGrepPreferences objects to specify parameters for the findGrep
and
changeGrep methods.
X You can find specific glyphs (and their formatting) and replace them with other glyphs and formatting.
This type of find/change operation uses the
findGlyphPreferences and changeGlyphPreferences
objects to specify parameters for the
findGlyph and changeGlyph methods.
All the find/change methods take one optional parameter,
ReverseOrder, which specifies the order in
which the results of the search are returned. If you are processing the results of a find or change operation
in a way that adds or removes text from a story, you might face the problem of invalid text references, as
discussed earlier in this chapter. In this case, you can either construct your loops to iterate backward
through the collection of returned text objects, or you can have the search operation return the results in
reverse order and then iterate through the collection normally.
About find/change preferences
Before you search for text, you probably will want to clear find and change preferences, to make sure the
settings from previous searches have no effect on your search. You also need to set some find/change
preferences to specify the text, formatting, regular expression, or glyph you want to find and/or change. A
typical find/change operation involves the following steps:
1. Clear the find/change preferences. Depending on the type of find/change operation, this can take one
of the following three forms:
Z //find/change text preferences
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
Z //find/change grep preferences
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
Z //find/change glyph preferences
app.findGlyphPreferences = NothingEnum.nothing;
app.changeGlyphPreferences = NothingEnum.nothing;
2. Set up search parameters.
3. Execute the find/change operation.
4. Clear find/change preferences again.