Find and Replace

In addition to creating new documents, scripts may be called upon to edit existing documents, including locating and replacing words in the main body text. The current Pages scripting dictionary does not contain a search command, so editing long text flows efficiently requires the use of a script sub-routine optimized for that purpose.

The sub-routine included in the following example script (replaceWordWithStringInBodyText) is designed to work with long documents, by targeting the search and replace process at the paragraph-level, reducing the amount of text a script processes at a time. In addition, the script works from the end of the document to the beginning, thereby avoiding any issues with possible script-triggered reflow of the document.

The Long Document Example

In this example, the script will find and replace all of the occurrences of “Alice” in the “Alice in Wonderland” book by Lewis Carroll.

DO THIS ►For use with this tutorial DOWNLOAD the example Alice in Wonderland Pages document.

DO THIS ► For execution optimization, open the example script in the AppleScript Editor application, and then install (save) it into the system-wide Script Menu (instructions). Once installed, run the script from the Script Menu.

Here’s a script for demonstrating replacing a word with a text string, in a long document:

Find-Change Example
  
01tell application "Pages"
02 activate
03 display dialog "This script will replace all occurences of “Alice” and “Alice's” with “Wanda” and “Wanda's” in the “Alice in Wonderland” example Pages document." with icon 1 buttons {"Cancel", "Download Document", "Begin"} default button 3
04 if the button returned of the result is "Download Document" then
05 open location "http://iworkautomation.com/pages/body-text-replace.html"
06 else
07 my replaceWordWithStringInBodyText("Alice", "Wanda")
08 
09 set thisTitle to "Pages: Find & Change"
10 set thisNotification to "Beginning 2nd replacement…"
11 set thisSubtitle to "“Alice” to “Wanda” completed."
12 my displaythisNotification(thisTitle, thisNotification, thisSubtitle)
13 
14 my replaceWordWithStringInBodyText("Alice's", "Wanda's")
15 
16 set thisNotification to "Replacement completed: “Alice's” to “Wanda's”"
17 set thisSubtitle to ""
18 my displaythisNotification(thisTitle, thisNotification, thisSubtitle)
19 end if
20end tell
21 
22on displaythisNotification(thisTitle, thisNotification, thisSubtitle)
23 tell current application
24 display notification thisNotification with title thisTitle subtitle thisSubtitle
25 end tell
26end displaythisNotification
27 
28on replaceWordWithStringInBodyText(searchWord, replacementString)
29 tell application "Pages"
30 activate
31 tell the front document
32 tell body text
33 -- start at the end and go to the beginning
34 repeat with i from the (count of paragraphs) to 1 by -1
35 tell paragraph i
36 repeat
37 try
38 if exists searchWord then
39 set (last word where it is searchWord) to replacementString
40 else
41 exit repeat
42 end if
43 on error errorMessage
44 exit repeat
45 end try
46 end repeat
47 end tell
48 end repeat
49 end tell
50 end tell
51 return true
52 end tell
53end replaceWordWithStringInBodyText

TIP:For another example of the use of this sub-routine for performing text replacement, view the Mail Merge example.

TOP | CONTINUE