CHAPTER 6: Text and Type Finding and Changing Text 102
var myDocument = app.documents.item(0);
//Clear the find/change preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeTextOptions.caseSensitive = false;
app.findChangeTextOptions.includeFootnotes = false;
app.findChangeTextOptions.includeHiddenLayers = false;
app.findChangeTextOptions.includeLockedLayersForFind = false;
app.findChangeTextOptions.includeLockedStoriesForFind = false;
app.findChangeTextOptions.includeMasterPages = false;
app.findChangeTextOptions.wholeWord = false;
//Search the document for the 24 point text and change it to 10 point text.
app.findTextPreferences.pointSize = 24;
app.changeTextPreferences.pointSize = 10;
myDocument.changeText();
//Clear the find/change preferences after the search.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
Using grep
InDesign supports regular expression find/change through the findGrep and changeGrep methods.
Regular-expression find/change also can find text with a specified format or replace the formatting of the
text with formatting specified in the properties of the
changeGrepPreferences object. The following
script fragment shows how to use these methods and the related preferences objects (for the complete
script, see FindGrep):
var myDocument = app.documents.item(0);
//Clear the find/change grep preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
//Regular expression for finding an email address.
app.findGrepPreferences.findWhat = "(?i)[A-Z]*?@[A-Z]*?[.]...";
//Apply the change to 24-point text only.
app.findGrepPreferences.pointSize = 24;
app.changeGrepPreferences.underline = true;
myDocument.changeGrep();
//Clear the find/change preferences after the search.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
NOTE: The findChangeGrepOptions object lacks two properties of the findChangeTextOptions object:
wholeWord and caseSensitive. This is because you can set these options using the regular expression
string itself. Use
(?i) to turn case sensitivity on and (?-i) to turn case sensitivity off. Use \> to match the
beginning of a word and
\< to match the end of a word, or use \b to match a word boundary.
One handy use for grep find/change is to convert text mark-up (i.e., some form of tagging plain text with
formatting instructions) into InDesign formatted text. PageMaker paragraph tags (which are not the same
as PageMaker tagged-text format files) are an example of a simplified text mark-up scheme. In a text file
marked up using this scheme, paragraph style names appear at the start of a paragraph, as shown below: