You are currently browsing the archives for the Text 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 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 12 March 2008 at 6:32

Permalink to the post entryScreencast and First Script

Before we get to our first script, I thought that I’d post a quick screencast of a script in action. This screencast shows some of the power of scripting. In the screencast, I grab some data from a text file, parse that data, then send the data to Timeline to create a beautiful timeline.

Click here to view the screencast

As you can see, I’ve used this script to create a timeline of events in a litigation context. In this case, I used a simple text file as my data source, but you could also use a database like FileMaker Pro or a spreadsheet like Microsoft Excel to hold your data. (Not that there’s anything wrong with plain text, mind you.) More importantly, however, I saved valuable time by not having to enter the data twice–once into my data source and again into Timeline. And I can use the script over and over again, saving more time every time I use the script. I timed the script execution to around 2 seconds. I then manually entered the events to create the timeline and finished in 240 seconds. Every time I use this script, then, I save almost four minutes of my time. This is the power of scripting.

On to our first script!

Open Script Editor and type the following text into the editor pane:

say "Hello from AppleScript!"

Hello from AppleScript

Now click the “Run” button or press Command+R. You should have seen the text change color and then you should have heard your computer speak the words, “Hello from AppleScript”. Cool!

So what happened? When you clicked the “Run” button, Script Editor secretly compiled the program first. When Script Editor compiles a program, it checks for errors in your code, then converts your code to machine code for the computer to read. Here, the translated code sent a command—the “say” command—to the computer along with an argument for the command—the text string “Hello from AppleScript—and the computer obeyed your command and said the words, “Hello from AppleScript”. The key lesson to remember is that, in AppleScript, you “tell” your applications to do things using “commands”. Who’s the boss, now?

How do you know what commands you can send to an application? For that you need to know about AppleScript dictionaries, the subject of the next post.