In our last post, we looked at script-based templates, which are those templates that are stored within the script. In this post, we’ll look at search-and-replace templates. With these templates, our script searches a template with preset tags and replaces those tags with our data. As with the last example, we’ll use Microsoft Word as our word processor. We’ll look at LaTeX and Pages in later posts.
Before we get to our script, we need to create a template with preset tags. Open up Microsoft Word and type the following:
Dear First_Name:
Please call me at Appt_Time on Appt_Date to discuss your case.
Save your document to your desktop as “template.doc”. As you can see, this template includes 3 tags: First_Name, Appt_Time, and Appt_Date. Our script will search for these tags and replace the tags with data from our data source.
Let’s again suppose that we retrieved the relevant data from our data source and stored the data in 3 variables:
set firstName to "Larry"
set appointmentDate to "July 4, 2008"
set appointmentTime to "9:00"
Now that we have our data, we need to open up our template document, search for each tag, replace the tag with the variable data, and then save the document as a different name. Here goes:
tell application "Microsoft Word"
set doc to open ((path to desktop as string) & "template.doc")
set findRange to find object of selection
tell findRange
execute find find text "First_Name" replace with firstName replace replace all
execute find find text "Appt_Time" replace with appointmentTime replace replace all
execute find find text "Appt_Date" replace with appointmentDate replace replace all
end tell
save as active document file name "letter.doc"
end tell
What’s going on here? In the second line, we open the template file using a special command called “path to desktop”. This command returns the path to the desktop as an alias, which we convert to a string and concatenate our template file name. The document is now open. In the third line we create a find object for our document. This find object is specific to Microsoft Word. The find object specifies a range of text to execute our search and replace. Because nothing is selected, Word assigns the entire document to the find object. In lines 4–8, we tell our find object to execute a find by finding text and replacing the text with the data stored in our variables. At the end of lines 5–7, we need to tell the find object to “replace all” occurrences of the found text. In line 9, we save the document as “letter.doc” to preserve our template.
Using this method, you only have to prepare the script once and the script will run properly. If you want to change the template, you do not need to change the script (unless you want to add variables). This type of template scripting (mostly) separates your logic from your presentation and is generally easier to maintain.
In the next lesson, we’ll look at search and replace using a third-party OSAX.
Grace Suarez on July 9th, 2008 at 13:42 says:
I get it. This is getting so close to what I want more than anything else: document assembly. Keep it up.