AppleScript for Creating Form Documents in Word 2008

properller.jpg

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 …

scripteditor.gif

— 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 tell

So 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 Information

The 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 response

This 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…)

— Name

Another comment telling me I’m about to do the find and replace on the Name variable.

set selFind to find object of selection

I’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 name

I just told Word “Find every instance of “**Name**” and replace it with the variable “Name”

execute find replace replace all

Word knows what I want it to do. Now it has to go do it.

end tell

Closing the loop.

end tell

Closing 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.

30 Comments AppleScript for Creating Form Documents in Word 2008

  1. kpdurand@hotmail.com

    Thanks for this information! I am also a transactional attorney who uses Macs in the office and have been searching for a way to automate document production. This will come in very handy.

    Reply
  2. kpdurand@hotmail.com

    Thanks for this information! I am also a transactional attorney who uses Macs in the office and have been searching for a way to automate document production. This will come in very handy.

    Reply
  3. kpdurand@hotmail.com

    Thanks for this information! I am also a transactional attorney who uses Macs in the office and have been searching for a way to automate document production. This will come in very handy.

    Reply
  4. kpdurand@hotmail.com

    Thanks for this information! I am also a transactional attorney who uses Macs in the office and have been searching for a way to automate document production. This will come in very handy.

    Reply
  5. kpdurand@hotmail.com

    Thanks for this information! I am also a transactional attorney who uses Macs in the office and have been searching for a way to automate document production. This will come in very handy.

    Reply
  6. yvonne@nilsonreport.com

    Thank you very much!! It was exactly what I needed to open frequently used templates. Thanks again! Now I just need more info on the language such as Tell. Is there a dictionary somewhere?

    Reply
  7. yvonne@nilsonreport.com

    Thank you very much!! It was exactly what I needed to open frequently used templates. Thanks again! Now I just need more info on the language such as Tell. Is there a dictionary somewhere?

    Reply
  8. yvonne@nilsonreport.com

    Thank you very much!! It was exactly what I needed to open frequently used templates. Thanks again! Now I just need more info on the language such as Tell. Is there a dictionary somewhere?

    Reply
  9. yvonne@nilsonreport.com

    Thank you very much!! It was exactly what I needed to open frequently used templates. Thanks again! Now I just need more info on the language such as Tell. Is there a dictionary somewhere?

    Reply
  10. yvonne@nilsonreport.com

    Thank you very much!! It was exactly what I needed to open frequently used templates. Thanks again! Now I just need more info on the language such as Tell. Is there a dictionary somewhere?

    Reply
  11. mail@joelafferty.com

    brilliant, simple but it works – just new to mac and wanted to create a script to open a document with few standard templates.
    cut and pasted your work and hey presto, i’m up and running!
    thanks!!
    Joe

    Reply
  12. mail@joelafferty.com

    brilliant, simple but it works – just new to mac and wanted to create a script to open a document with few standard templates.
    cut and pasted your work and hey presto, i’m up and running!
    thanks!!
    Joe

    Reply
  13. mail@joelafferty.com

    brilliant, simple but it works – just new to mac and wanted to create a script to open a document with few standard templates.
    cut and pasted your work and hey presto, i’m up and running!
    thanks!!
    Joe

    Reply
  14. mail@joelafferty.com

    brilliant, simple but it works – just new to mac and wanted to create a script to open a document with few standard templates.
    cut and pasted your work and hey presto, i’m up and running!
    thanks!!
    Joe

    Reply
  15. mail@joelafferty.com

    brilliant, simple but it works – just new to mac and wanted to create a script to open a document with few standard templates.
    cut and pasted your work and hey presto, i’m up and running!
    thanks!!
    Joe

    Reply
  16. damon@solutionarts.net

    Well done. Just a few comments on your script to make things tidier and avoid any other “special” names. Your variables should try not to use any simple names like “response” and “name” as you have. Coming up with some type of naming convention for your variables like varResp or varName instead will keep your variables variables, and not trample on any built in (depending on the application you are scripting) special saved names. Also looking for these things after the fact becomes easier when the new version of MS Word breaks your script etc.

    Below is just a cleaned up version of your opening lines sanded down a bit.

    set response to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
    set name to text returned of response

    Can be turned into

    set varName to the text returned of (display dialog “Type in the name you want to paste” default answer “Thelonious Monk”)

    It all takes a bit of time to get the hang of things and with most things the more you use it, the more you’ll use it.

    macscripter.net (who I am no longer affiliated with) has many examples and offers tutorials adinfinitum on AppleScripting and has a very active and helpful admin as well as user base.

    Reply
  17. damon@solutionarts.net

    Well done. Just a few comments on your script to make things tidier and avoid any other “special” names. Your variables should try not to use any simple names like “response” and “name” as you have. Coming up with some type of naming convention for your variables like varResp or varName instead will keep your variables variables, and not trample on any built in (depending on the application you are scripting) special saved names. Also looking for these things after the fact becomes easier when the new version of MS Word breaks your script etc.

    Below is just a cleaned up version of your opening lines sanded down a bit.

    set response to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
    set name to text returned of response

    Can be turned into

    set varName to the text returned of (display dialog “Type in the name you want to paste” default answer “Thelonious Monk”)

    It all takes a bit of time to get the hang of things and with most things the more you use it, the more you’ll use it.

    macscripter.net (who I am no longer affiliated with) has many examples and offers tutorials adinfinitum on AppleScripting and has a very active and helpful admin as well as user base.

    Reply
  18. damon@solutionarts.net

    Well done. Just a few comments on your script to make things tidier and avoid any other “special” names. Your variables should try not to use any simple names like “response” and “name” as you have. Coming up with some type of naming convention for your variables like varResp or varName instead will keep your variables variables, and not trample on any built in (depending on the application you are scripting) special saved names. Also looking for these things after the fact becomes easier when the new version of MS Word breaks your script etc.

    Below is just a cleaned up version of your opening lines sanded down a bit.

    set response to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
    set name to text returned of response

    Can be turned into

    set varName to the text returned of (display dialog “Type in the name you want to paste” default answer “Thelonious Monk”)

    It all takes a bit of time to get the hang of things and with most things the more you use it, the more you’ll use it.

    macscripter.net (who I am no longer affiliated with) has many examples and offers tutorials adinfinitum on AppleScripting and has a very active and helpful admin as well as user base.

    Reply
  19. damon@solutionarts.net

    Well done. Just a few comments on your script to make things tidier and avoid any other “special” names. Your variables should try not to use any simple names like “response” and “name” as you have. Coming up with some type of naming convention for your variables like varResp or varName instead will keep your variables variables, and not trample on any built in (depending on the application you are scripting) special saved names. Also looking for these things after the fact becomes easier when the new version of MS Word breaks your script etc.

    Below is just a cleaned up version of your opening lines sanded down a bit.

    set response to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
    set name to text returned of response

    Can be turned into

    set varName to the text returned of (display dialog “Type in the name you want to paste” default answer “Thelonious Monk”)

    It all takes a bit of time to get the hang of things and with most things the more you use it, the more you’ll use it.

    macscripter.net (who I am no longer affiliated with) has many examples and offers tutorials adinfinitum on AppleScripting and has a very active and helpful admin as well as user base.

    Reply
  20. damon@solutionarts.net

    Well done. Just a few comments on your script to make things tidier and avoid any other “special” names. Your variables should try not to use any simple names like “response” and “name” as you have. Coming up with some type of naming convention for your variables like varResp or varName instead will keep your variables variables, and not trample on any built in (depending on the application you are scripting) special saved names. Also looking for these things after the fact becomes easier when the new version of MS Word breaks your script etc.

    Below is just a cleaned up version of your opening lines sanded down a bit.

    set response to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
    set name to text returned of response

    Can be turned into

    set varName to the text returned of (display dialog “Type in the name you want to paste” default answer “Thelonious Monk”)

    It all takes a bit of time to get the hang of things and with most things the more you use it, the more you’ll use it.

    macscripter.net (who I am no longer affiliated with) has many examples and offers tutorials adinfinitum on AppleScripting and has a very active and helpful admin as well as user base.

    Reply
  21. hoabsproralah@mail.ru

    Allstate – Auto Indemnity Quotes Online – Formal Milieu on Passenger car Insurance, Refuge Guaranty, Economic Products & More Rates and security options (and their availability) vary according to your state’s regulations.All you hold to do is mingle your native and auto insurance with Allstate, and you strength preserve up to 20%. Hands down, it’s the easiest speed to shelter a lot of wampum without sacrificing trait protection.

    Reply
  22. hoabsproralah@mail.ru

    Allstate – Auto Indemnity Quotes Online – Formal Milieu on Passenger car Insurance, Refuge Guaranty, Economic Products & More Rates and security options (and their availability) vary according to your state’s regulations.All you hold to do is mingle your native and auto insurance with Allstate, and you strength preserve up to 20%. Hands down, it’s the easiest speed to shelter a lot of wampum without sacrificing trait protection.

    Reply
  23. hoabsproralah@mail.ru

    Allstate – Auto Indemnity Quotes Online – Formal Milieu on Passenger car Insurance, Refuge Guaranty, Economic Products & More Rates and security options (and their availability) vary according to your state’s regulations.All you hold to do is mingle your native and auto insurance with Allstate, and you strength preserve up to 20%. Hands down, it’s the easiest speed to shelter a lot of wampum without sacrificing trait protection.

    Reply
  24. hoabsproralah@mail.ru

    Allstate – Auto Indemnity Quotes Online – Formal Milieu on Passenger car Insurance, Refuge Guaranty, Economic Products & More Rates and security options (and their availability) vary according to your state’s regulations.All you hold to do is mingle your native and auto insurance with Allstate, and you strength preserve up to 20%. Hands down, it’s the easiest speed to shelter a lot of wampum without sacrificing trait protection.

    Reply
  25. hoabsproralah@mail.ru

    Allstate – Auto Indemnity Quotes Online – Formal Milieu on Passenger car Insurance, Refuge Guaranty, Economic Products & More Rates and security options (and their availability) vary according to your state’s regulations.All you hold to do is mingle your native and auto insurance with Allstate, and you strength preserve up to 20%. Hands down, it’s the easiest speed to shelter a lot of wampum without sacrificing trait protection.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *