You are currently browsing the archives for the Word category.

Posted on 1 July 2008 at 15:13

Permalink to the post entrySearch-And-Replace Templates

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.

Posted on 9 June 2008 at 8:34

Permalink to the post entryContemplating Templating

In many professions, knowledge workers use forms with special fields to be filled in—templates, in other words—to speed along their daily tasks. Legal professionals are no exception. We use templates to efficiently create documents for our clients. Bankruptcy petitions, trademark applications, copyright applications, and real estate closing documents are just a few examples of templates used by legal professionals. AppleScript can help improve the efficiency in creating and tracking these documents.

A typical template includes pre-defined fields where data from a database or spreadsheet gets inserted. For example, we might have the following greeting in a template:

Dear FIRST_NAME LAST_NAME:

With this template, we would replace the FIRST_NAME field in the template with data from our data source. We would then do the same for the LAST_NAME field. At a basic level, this resembles a merge that you might find as a feature in Word or WordPerfect. But we can use AppleScript to control all aspects of this “merge” including calculating values and making decisions depending on the values in your data source. In addition, by using AppleScript, you are not necessarily tied to one word processing vendor.

There are two ways to accomplish this task. With the first method, we can recreate the entire document from scratch every time we run our script. Our script will hold the template, use variables to replace the fields in the template, then write out the entire document to a file. The second method uses search and replace to find the template variables and change them to the proper variables.

The advantage of the first method is that the final script need not be dependent on your word processor. You can simply copy the resulting template to the clipboard and the paste it into your preferred word processor. The advantage of the second method is that you can use existing templates to the extent that they exist. In addition, if you want to change the template, you don’t have to fiddle with your script.

In the next two posts, we’ll look at each method of using templates to create documents.

Posted on 3 June 2008 at 20:00

Permalink to the post entryFun with…Rocket Matter?

Rocket Matter is a Web-based case management system for lawyers and law firms. When I first heard about Rocket Matter, I immediately inquired about an API (Application Programming Interface) for accessing one’s data via AppleScript (or any other programming language). I told the developers at Rocket Matter that I wanted to access my data from Rocket Matter’s secure databases and push that data to various applications on my Mac. The developers graciously granted my request and we’ve been working on an API to allow such access. Although the API is still experimental, I was able to produce this video to show you that Web applications need not be a burden to your scripting. Enjoy.

UPDATE: You may need to turn up your volume. I need to get a microphone for these screencasts.

Scripting Rocket Matter