You are currently browsing the archives for the AppleScript category.

Posted on 7 July 2008 at 14:06

Permalink to the post entryHelp Request: Table of Authorities

Dear readers, I’m writing a script that generates a Table of Authorities from a Pages document and I need your help. I’ve determined 2 ways to delimit a citation for inclusion in the Table and I’m interested in your thoughts on how to implement this all-important feature.

First, the user could highlight the citation, then select the type of citation (case or statute) from a dialog box. Microsoft Word for Windows uses this method. I don’t particularly like this method, but it’s familiar to many switchers.

The second way is to delimit the citations using a markup language. For example, one might use something like \case{A v. B., 123 U.S. 456 (2008)} within their text to delimit the case citation. With my background in LaTeX and HTML, I prefer this method as it’s easier to parse the document. Moreover, the user would not have to go back and highlight each citation, hoping not to miss a citation. Instead, the user would simply delimit the cases as he or she types.

Here’s a quick movie of what I have so far. I’d love to get your feedback on how you might delimit the citations. Thanks!

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 17 June 2008 at 22:18

Permalink to the post entryScript-Based Templates

In our last post, we discussed using templates and how you can integrate a template with a data source such as a database to create documents. We also discussed two ways to solve this problem. The first solution involves using writing the template into your script while the second solution involves searching and replacing for key words in a document. This post explores the first solution.

When writing a script that incorporates your template, you should be aware of a few things. First, you need to remember that if your template changes, then you need to change your script. This can be trivial, but it can also lead to some problems if you aren’t careful. Second, you need to be aware of any escape characters in your script. Escape characters are used when you want AppleScript to ignore its normal behavior when dealing with certain characters and, instead, behave according to different rules. For example, if we want to set a variable named sampleText to a text string, we would do this:

set sampleText to "This is a text string."

But what if we wanted to use a literal quote in our text string? The immediate response is to simply add the quotation marks like so:

set sampleText to "This is a "quoted" text string."

AppleScript will raise an error for this code because it will come to the second quotation mark (the one before the letter “q” in “quoted”) and interpret that mark as the closing quote for the first quotation mark. So what do we do? We escape the quotation mark using the forward slash escape character, “\”.

set sampleText to "This is a \"quoted\" text string."

So if your template contains lots of quotation marks, you’ll have to be careful of those escape characters.

Back to our script. Another issue to consider involves what to do with the output of your script. Unlike the second type of script where we will search-and-replace from within a Word document, in this script, we will create the entire template in our script. The next step involves moving the template output from the script to your word processor. A simple way to do this involves copying the final template output to the clipboard, then pressing Command+V in the word processor of your choice.

Let’s look at a simple template. Suppose that we want to write a brief letter to a client with the following template:


Dear FIRST_NAME:

Please call me on APPOINTMENT_DATE at APPOINTMENT_TIME to discuss your case.

Sincerely,

Me

Let’s further suppose that we have a FileMaker database that holds the data for this client’s FIRST_NAME, APPOINTMENT_DATE, and APPOINTMENT_TIME. Because we do not have a FileMaker database setup for this, we’ll just assume that we’ve taken the data out of FileMaker and we have stored it in some variables:


set firstName to "Larry"
set appointmentDate to "June 18, 2008"
set appointmentTime to "9:00"

Before going further, note the use of descriptive variable names. While not required, I recommend using descriptive variable names to help you keep track of your variables. Imagine if you named each variable x, y, and z. After several uses of each, you would quickly get confused.

On to the template. Now that we have our variables, we can simple plug them into the template. Type the following into Script Editor and run it:


set firstName to "Larry"
set appointmentDate to "June 18, 2008"
set appointmentTime to "9:00"

set templateOutput to "Dear " & firstName & ":" & return & return & "Please call me on " & appointmentDate & " at " & appointmentTime & " to discuss your case." & return & return & "Sincerely," & return & return & "Me"

 

The first three lines are the variables from above. The next variable, templateOutput, holds our final template. We begin with the first line with “Dear “. Note the space after “Dear “. We need this because our variable does not know about spaces before or after itself and a variable follows “Dear “. Next we see the ampersand character. The ampersand character tells AppleScript to concatenate the text on either side of the ampersand. Here, we concatenate “Dear ” and our firstName variable. We then concatenate a colon on to the end of the first line. Next, we concatenate a return. return is a reserved word in AppleScript that does just what you think it does&emdash;it inserts a return at the end of a line. In our case, we concatenate another return to effect two lines. The rest of the script follows these same principles.

Now that we have our output, we need to copy it to the clipboard. Add this line to your script:

set the clipboard to templateOutput

Compile and run your script. You should not see any output in the “Result” tab in the bottom pane of Script Editor. Now open TextEdit and press Command+V to paste your output into TextEdit. Now open up Word, Pages, NeoOffice, or your favorite word processor and paste your output into the frontmost document. Because we’ve copied the output to the clipboard, we can paste that output anywhere we can use the clipboard.

Well, you’ve done it! You created a simple template and inserted variables into the template for insertion into a document. Next, we’ll look at using search-and-replace to insert those variables into a document.
 

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