FIND/CHANGE

When it comes to the subject of find and change with InDesign, this is one area where InDesign offers some real versatility especially for those who understand the power of scripting. But with power comes complexity. Not that it is hard, it is that the possibilities are almost limitless.

FIND/CHANGE CLASSES

First, understand that there are five main InDesign classes that are exposed to scripts for find and/or change routines:

  • text (literal text: words and/or collections of letters)
  • glyphs (special characters in text)
  • grep (searches for text based on Regular Expressions)
  • objects
  • transliterate (for multilingual publishing: includes finding and changing numbers between different languages)

For each of these classes there is a find command (method) and a change command (method) supported at both the application and the document level.

To define the target and/or its properties for the find/change, there is a find preference and a change preference for each class at the application level. For instance for finding and changing literal text there is a find text preference and change text preference.

These preference objects control the item and any corresponding properties (attributes) that will be considered for each find/change operation. For this reason, you need to clear these preferences before each find/change operation to make sure that the properties you want (or don’t want) are considered as part of the find or change operation. You will also want to clear these preferences to nothing once your script is through using them just to make sure settings established in the script do not corrupt find/change operations that may be done manually.

FIND AND/OR CHANGE TEXT

To understand the concept, let’s look as some example scripts for working with literal text. To find the occurrence of a specific word in a document a script could read as follows:

Find Literal Text

   --Establish text for find; 
   set findWord to "false"
   tell application "Adobe InDesign CC 2015"
      --Clear find text preferences
      set find text preferences to nothing
      --establish properties for find
      set find what of find text preferences to findWord
      --instigate the find text method
      tell document 1
         set foundSet to find text
      end tell
      set find text preferences to nothing
   end tell

The result of this script (foundSet) will be a list of the text found referenced similar to:

   {text from character 8 to character 12 of story id 36 of document id 1 of application "Adobe InDesign CC 2015", ...}

If no occurrences of the text specified for findWord is found, the result is an empty list.

Find/Change Literal Text

To find and change all occurrences of the text specified, the following script could be used:

   --establish text for find
   set findWord to "false"
   --establish text for change
   set changeWord to "true"
   tell application "Adobe InDesign CC 2015"
      --Clear find text preferences
      set find text preferences to nothing
     --Clear change text preferences
      set change text preferences to nothing
      --establish properties for find
      set find what of find text preferences to findWord
      --establish properties for change
      set change to of change text preferences to changeWord
      --perform change text operation
      tell document 1
         change text
      end tell
      set find text preferences to nothing
      set change text preferences to nothing
   end tell

Before executing the change text method you might want to determine if there is text to be changed. For this you will want to test the result of the find what method to see if it returns an empty list. For this, change the tell document 1 block in the script above to read as follows:

   tell document 1
      set foundSet to find text
      if foundSet is not {} then
         change text
      end if
   end tell

USING ATTRIBUTES

Should you wish to further refine the text to be found (and replaced) you just add the desired attributes to find text peferences and change text preferences as needed. For example, suppose you want to change all “true” text to “false” but only when it is colored Gold.

For this you would add the following line to the script above after the line that sets find what of find text preferences to findWord

      set fill color of find text preferences to "Gold"

That’s it. To get a feel for all of the attributes that can be set for both find text preferences and change text preferences, open AppleScript’s dictionary for your version of InDesign. (From the File menu in Script Editor select Open Dictionary… and choose your version of InDesign from the list of supported applications that will be presented. When the dictionary opens, look for either find text preference or change text preference in the Preferences suite.)

To change attributes without any consideration for actual text, do not set values for find what (for find text preferences) or change to (for change text preferences). The following will change all text in the document having fill color “Gold” to “Paper.”

Change Attributes Only

   tell application "Adobe InDesign CC 2015"
      set find text preferences to nothing
      set fill color of find text preferences to "Gold"
      set change text preferences to nothing
      set fill color of change text preferences to "Paper"
      tell document 1
         change text
      end tell
      set find text preferences to nothing
      set change text preferences to nothing
   end tell 

DISPLAY RESULT

Finally, to make the scripts above more friendly, you might want to give the user a message with the result of the find/change. For this, change the tell document 1 block in the script above to read as follows:

      tell document 1
         set foundSet to find text
         if foundSet is not {} then
            set userMsg to "There were " & length of foundSet  & " instances found and changed"
            change text
         else
            set userMsg to "No text found as defined for find what"
         end if
      end tell
      activate
      display alert userMsg 

FIND CHANGE OPTIONS

Each of the find/change classes also has a find change options object that allows the scope of the find/change operation to be defined further. For text this is the find change text option. The following properties can be set to true or false:

  • case sensitive
    Set case sensitive to true if you only want to find text with the exact capitalization pattern defined for find what. When this option is set to true, the capitalization for change to is also used.
  • ignore diacritics
  • include footnotes
  • include hidden layers
  • include locked layers for find
  • include locked stories for find
  • include master pages
  • whole word
    Set whole word to true if you don’t want to find variations of the text defined for find what (such as for plural versions)
  • width sensitive
  • search backwards
    if true, search the text from back to front

METACHARACTERS

A script can use metacharacters for both find and change operations. For a quick reference for metacharacters, open the find/change dialog in InDesign (Command/Control + F). Click the special character icon to the right of either the Find what: or Change to: field. From the context menu presented, select the special character desired. Copy the metacharacter that is entered in the field to your script.

As an example, suppose you have received a document from a client full of bullet points. The client has decided he wants the fill color for the bullet points to change from Paper to Gold and the size to be changed to 20 point.

The following script could do the job.

Before running script to change bullet points

Metacharacter Change

   set findWord to "^8" --metacharater for bullet
   tell application "Adobe InDesign CC 2015"
      set find text preferences to nothing
      set change text preferences to nothing
      set find what of find text preferences to findWord
      set fill color of find text preferences to "Paper"
      set fill color of change text preferences to "Gold"
      set point size of change text preferences to 20
      tell find change text options
	set whole word to true
	set case sensitive to false
      end tell
      tell document 1
         set foundSet to find text
	 if foundSet is not {} then
	   change text
	 end if
      end tell
   end tell 

After running the Metacharacter Change script

Of course, if the bullet characters were created as part of a paragraph style, you would change the definition for the paragraph style to make the change. For this you would create a character style for the bullet (if one was not used originally). Then in the Bullets and Numbering panel for the paragraph style set the value for the Character Style field to the name of the character style created. Otherwise, change the properties for the character style defined for the bullet.

WILD CARDS

Wild cards can only be used for a find what operation. The wild card symbol is a carat (Shift + 6). If you want to find multiple variations of a word, you can use a wild card character as part of the find what text definition. For instance, to find occurrences of both “run” and “ran” in a document, substitute a wild card for the vowel, as in:

   set findWord to "r^n"

ONWARD AND UPWARD

Using a script for find/change has little advantage over using the Find/Change dialog (Command/Control + F) in InDesign. However, if you consistently have similar occurrences for find/change you may want to create a script with a user dialog that simplifies the operation. Moreover, find/change procedures often find their way as part of a complex script that does more than just a find/change. See if you can write the above sample scripts as handlers that can be added to your next “do-it-all” script.