Use AppleScript to Create Message Templates

Use AppleScript to Create Message Templates

Apple provides a set of really Apple scripts with every computer. The vast majority of these scripts simply never see the light of day on the computer where it resides. That’s too bad because AppleScript can be a huge time saver for repetitive tasks.

AppleScript can also add missing features to an application. For example, Outlook users on Windows have long had the ability to create templates. Apple plans on including this feature in Mac OS X 10.5 with all kinds of HTML messages. So you know, we’re all going to be getting gobs and gobs of HTML messages some time in the Spring of 2008. But what if you want this feature now? And what if you really just need to send a plain text message with mostly the same text and only a few relevant changes?

You can create your own templates by either modifying Apple’s free script /Library/Scripts/Mail Scripts/Create New Message.scpt or by following the example I’ve included below. It’s great not having to look for a previous version of the message, copy the text, paste it into a new message, search for the parts that need to be edited and then hope you didn’t miss anything. Creating a script once that you will use regularly will save time, save money and keep you from missing details.

If you’re new to AppleScript it could take a couple of hours to figure out the script, edit it and make it your own, but ultimately you will save time. I have sent this service confirmation email hundreds of times.


--Get the current date
set myMonth to month of (current date) as string
set myMonthNumber to “”
set myDate to day of (current date) as string
set myYear to year of (current date) as string

--Create an list of month names
set monthList to {"January“, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}

?

--Loop through the list since you can't use an array index in AppleScript
repeat with i from 1 to 12
if myMonth is equal to item i of monthList then
set myMonthNumber to i as string
exit repeat -- no point continuing once we have what we want
end if
end repeat

-- Prompt the user for the date
set serviceDate to display dialog “What is the service visit date?” default answer myMonthNumber & “/” & myDate & “/” & myYear
set resultDate to text returned of serviceDate

–Prompt the user for the time
set serviceTime to display dialog “At what time?” default answer “10 AM”
set resultTime to text returned of serviceTime

–Prompt the user for the end time of the service visit
set serviceLength to display dialog “When will the visit end?” default answer “2 PM”
set resultServiceLength to text returned of serviceLength

–Prompt the user for the purpose of the visit
set purposeText to display dialog “For what purpose?” default answer “regular maintenance”
set resultPurpose to text returned of purposeText

–Create the subject line with date and time
set theSubject to “Service Visit Confirmation for ” & resultDate & ” ” & resultTime

–Create message body
–Edit this part to to say what you want in the body
–Notice the following:
–” ” contains a string of text
–& join a string of text together with a result from the script prompts
set theBody to “I wanted to confirm that I will be making a service visit on ” & resultDate & ” at about ” & resultTime & ” depending on other events.

I will address ” & resultPurpose & ” during my visit.

Please email me a list of additional items you would like me to address.

I expect the service visit will last until ” & resultServiceLength & ” at which time I may need to leave for another service visit.

–Your Name”

———————————————-
–WARNING: No user editable parts below here
———————————————-

– Display a list of all the user’s defined signatures. Skip if no signatures are defined.
tell application “Mail” to set everySignature to name of every signature
set theSignature to “”
if (count of everySignature) is greater than 0 then
set everySignature to {”None”} & everySignature
set theResult to choose from list everySignature with prompt ¬
“Select a signature to use with this message:” default items {”None”} without multiple selections allowed
if theResult is not equal to false then
tell application “Mail” to set theSignature to signature (item 1 of theResult)
end if
end if

 

 

 

 

 

 

?

?

?

--Execute the command to Apple Mail
tell application "Mail"
-- Properties can be specified in a record when creating the message or
-- afterwards by setting individual property values.
set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
tell newMessage
-- Default is false. Determines whether the compose window will
-- show on the screen or whether it will happen in the background.
set visible to true
if (theSignature is not equal to "") then
set message signature to theSignature
end if
end tell
-- Bring the new compose window to the foreground, in all its glory
activate
end tell

When Joshua Wait isn’t teaching himself new programming languages or computer tricks, he’s hiking, swimming, cooking and playing outdoors with his family.