Okay gang, put on your propeller beanie caps. This one is going to be complicated.
One of the things I do during the day job includes a lot of corporate transactional work. As a result I have a few forms that I find myself using repeatedly. For instance, sometimes I have a client that needs a set of corporate minutes. I would like to have a system where I can run a program that prompts me for certain bits of information (i.e. date, corporate officers and directors) and then goes off and opens the form and fills in the basic information for me.
You think there would be guides all over the InterWeb for this but I couldn't find any. So I spent a few hours today learning to Applescript and came up with the script I'm reporting below. I'm no expert at this and I'm pretty sure this could get better but at least my script is functional and hopefully saves the next person from the trouble of starting from scratch.
So I'm going to list the whole script below and then I'm going to break it into pieces. So lets start with the whole script ...
-- Dialog Box to Get Information
set response to display dialog "Type in the name you want to paste" default answer "Thelonious Monk"
set name to text returned of response
tell application "Microsoft Word"
open "Macintosh HD:Users:david:Library:Application Support:Microsoft:Office:User
Templates:My Templates:Piano Legends.dot"
-- Name
set selFind to find object of selection
tell selFind
set content to "**Name**"
set content of replacement of selFind to name
execute find replace replace all
end tell
end tellSo breaking it down let me explain as best as my tiny programming brain can. If you are a complete Applescript newbie you need to first open Script Editor which can be found in the Applescript subdirectory of you Applications folder and then copy the above script in.
-- Dialog Box to Get InformationThe two dashes make this line a remark so the program basically ignores it. This sample has only one variable but the actual script has twelve variables. I named each with a remark so I can get back to where I need easily to debug if necessary.
set response1 to display dialog "Type in the name you want to paste" default answer "Thelonious Monk"This line does two things:
First it pops up a dialog box that says "Type in the name you want to paste"
Second it fills in the box with a default answer of "Thelonious Monk" (Has anyone figured out yet what a big Monk fan I am?)
set name to text returned of responseThis was the line that vexxed me the most. Simply putting up the dialog box does not create a variable that can be used to fill in a Word form. This line of code creates a new variable called "name" and fixes the problem. It took me an hour to figure this out.
tell application "Microsoft Word"Now we are getting to the good stuff. Applescript just opened Word 2008.
open "Macintosh HD:Users:david:Library:Application Support:Microsoft:Office:User Templates:My Templates:Piano Legends.dot"If you are going to be creating forms you first need to create a template in word. In this example I've created a template (.dot extension) in word called "Piano Legends.dot". Obviously the location of your document template may vary slightly. When you create the template it is important that you distinguish the phrases you plan on replacing. I did it with asterisks. For instance the name section of the document is written "**Name**". In setting it up this way you don't need to bother Applescripting the formatting because the script just uses whatever formatting you chose in the template (i.e. All Caps, bold, etc...)
-- NameAnother comment telling me I'm about to do the find and replace on the Name variable.
set selFind to find object of selectionI'm a bit clueless on this line but the script fails if it is not there. I think it selects the entire document for the find/replace action.
tell selFind
set content to "**Name**"This starts up the find and replace process. It also sets the variable "content" to the search text I placed in the template as explained above, "**Name**"
set content of replacement of selFind to nameI just told Word "Find every instance of "**Name**" and replace it with the variable "Name"
execute find replace replace allWord knows what I want it to do. Now it has to go do it.
end tell Closing the loop.
end tellClosing the loop again.
So there you have it. A rather tame Applescript that helps automate document production. You can duplicate as many variables and replacements as you need. I'm surprised about how easy this was to figure out considering I'm not much of a code jockey but it sure is handy.