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.