C
HAPTER
6: Text and Type
Finding and Changing Text 106
--You'll have to fill in a valid file path for your system.
set myFilePath to "Macintosh HD:scripting:styles.indd"
--Create a new document.
set myDocument to make document tell myDocument
--Import the styles from the saved document.
--importStyles parameters:
--Format options for text styles are:
-- paragraph styles format
-- character styles format
-- text styles format
--From as file or string
--Global Strategy options are:
-- do not load the style
-- load all with overwrite
-- load all with rename import styles format text styles format from myFilePath global strategy load all with overwrite end tell
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: