While working on a private script project, I also wrote a new AppleScript library for creating eMail messages with Apple Mail.
As a lot of Mac users seem to be interested in scripting Apple Mail, I decided to publish my small library.
The AppleScript library was successfully tested with:
¢ Mac OS X 10.5.1
¢ Apple Mail 3.1
There is also a test function named testlib() in the library, which explains how to use it. As soon as you are familiar with the library, you can simply remove the testlib() function.
To use the library in your own script, just open the long scriplet at the end of this post in your Script Editor and copy the whole script object to your code. Then you can write code as follows:
set visibility to true
-- creating a new eMail message in Apple Mail, which is visible
my AppleMailMessage's createnew(visibility)
-- adding a recipient to the eMail message
my AppleMailMessage's addtorecipient("Harry Smith", "harry.smith@examplemail.com")
-- adding a subject line to the eMail message
set msgsubject to "Thanks for sending me so much spam mails!"
my AppleMailMessage's addsubject(msgsubject)
set msgcontent to "I just want to say thank you for sending me so many spam mails!"
my AppleMailMessage's addcontent(msgcontent)
-- more code follows
Alternatively, save the contents of the script object as a separate script file and load it into your own script(s) at run time as follows:¨
set maillib to (load script file ("your:path:here:" & "maillib.scpt"))
¨¨maillib's AppleMailMessage's createnew(true)
-- more code follows
(Thanks to Nigel Garvey for the formulation!)
Using the the library you can conveniently:
¢ create an eMail message in Apple Mail and then:
¢ set the subject line
¢ add TO, CC & BCC recipients
¢ set the senders account
¢ set the contents/body
¢ add a signature
¢ add attachments
Moreover the library allows to get:
¢ all signatures available in Apple Mail
¢ all sender accounts in Apple Mail
-- created: 30.01.2008
-- version: 0.1
-- tested with:
-- ¢ Mac OS X 10.5.1
-- ¢ Apple Mail 3.1
-- This script is a library for creating eMail messages with Apple Mail
-- uncomment the code line below and then choose to run the script
-- in the Script Editor to test the library
-- my testlib()
-- A small test routine to check the library
on testlib()
set visibility to true
-- creating a new eMail message in Apple Mail, which is visible
my AppleMailMessage's createnew(visibility)
-- adding some recipients to the eMail message
my AppleMailMessage's addtorecipient("Harry Smith", "harry.smith@examplemail.com")
-- No recipient name available for this contact, so we are
-- using «missing value»
my AppleMailMessage's addccrecipient(missing value, "info@examplemail.com")
-- adding a subject line to the eMail message
set msgsubject to "Thanks for sending me so much spam mails!"
my AppleMailMessage's addsubject(msgsubject)
-- we are choosing the first senderaccount, just for demo purposes
set senderaccount to item 1 of (my AppleMail's getsenderaccounts())
-- setting the sender account of the eMail message
my AppleMailMessage's setsenderaccount(senderaccount)
-- creating the content plus a signature, if available
set msgcontent to "I just want to say thank you for sending me so many spam mails!"
set signaturenames to my AppleMail's getsignaturenames()
if signaturenames is not {} then
set signaturename to item 1 of signaturenames
set signcontent to my AppleMail's getsigncontent(signaturename)
set msgcontent to msgcontent & return & return & signcontent
end if
-- adding content to the eMail message
my AppleMailMessage's addcontent(msgcontent)
-- adding a file as an attachment
tell application "Finder"
set desktopfiles to every file in desktop
end tell
if desktopfiles is not {} then
set atmfile to POSIX path of ((item 1 of desktopfiles) as Unicode text)
my AppleMailMessage's addattachments({atmfile})
end if
end testlib
-- Different functions for getting data from Apple Mail
script AppleMail
-- This code is mostly copied from the
-- «Create New Message.scpt» by Apple
-- Thank you, unknown Apple developer!
-- I'm returning all available sender accounts
on getsenderaccounts()
set senderaccounts to {}
tell application "Mail"
set allaccounts to every account
repeat with useraccount in allaccounts
set useraddresses to email addresses of useraccount
if (useraddresses is not equal to missing value) then
repeat with useraddress in useraddresses
set senderaccounts to senderaccounts & {(full name of useraccount & " <" & useraddress & ">") as Unicode text}
end repeat
end if
end repeat
end tell
return senderaccounts
end getsenderaccounts
-- I'm indicating if a given senderaccount exists
on hassenderaccount(senderaccount)
if senderaccount is in my getsenderaccounts() then
return true
else
return false
end if
end hassenderaccount
-- I'm returning the names of the available signatures
on getsignaturenames()
tell application "Mail"
return (name of every signature) as list
end tell
end getsignaturenames
-- I'm indicating if a given signature name exists
on hassignaturename(signaturename)
if signaturename is in my getsignaturenames() then
return true
else
return false
end if
end hassignaturename
-- I'm returning the content of a given signature name
on getsigncontent(signaturename)
tell application "Mail"
set signcontent to content of signature signaturename
end tell
return signcontent
end getsigncontent
end script
-- Controls the creation of new eMail messages in Apple Mail
script AppleMailMessage
property curmsg : missing value
-- I'm creating a new blank eMail message
on createnew(visibility)
tell application "Mail"
set curmsg to make new outgoing message with properties {visible:visibility}
end tell
return curmsg
end createnew
-- I'm setting the subject line of the eMail message
on addsubject(subjectline)
tell application "Mail"
tell curmsg
set subject to subjectline
end tell
end tell
end addsubject
-- I'm adding text content (the body) to the eMail message
on addcontent(emailcontent)
tell application "Mail"
tell curmsg
set content of curmsg to emailcontent
end tell
end tell
end addcontent
-- I'm setting the sender account of the eMail message
on setsenderaccount(senderaccount)
tell application "Mail"
tell curmsg
set sender to senderaccount
end tell
end tell
end setsenderaccount
-- I'm adding TO recipients
on addtorecipient(rname, raddress)
my _addrecipient("TO", rname, raddress)
end addtorecipient
-- I'm adding CC recipients
on addccrecipient(rname, raddress)
my _addrecipient("CC", rname, raddress)
end addccrecipient
-- I'm adding BCC recipients
on addbccrecipient(rname, raddress)
my _addrecipient("BCC", rname, raddress)
end addbccrecipient
-- I'm the general recipient adder
on _addrecipient(rkind, rname, raddress)
tell application "Mail"
tell curmsg
if rkind is "TO" then
if rname is missing value then
make new to recipient at end of to recipients with properties {address:raddress}
else
make new to recipient at end of to recipients with properties {name:rname, address:raddress}
end if
else if rkind is "CC" then
if rname is missing value then
make new cc recipient at end of cc recipients with properties {address:raddress}
else
make new cc recipient at end of cc recipients with properties {name:rname, address:raddress}
end if
else if rkind is "BCC" then
if rname is missing value then
make new bcc recipient at end of bcc recipients with properties {address:raddress}
else
make new bcc recipient at end of bcc recipients with properties {name:rname, address:raddress}
end if
end if
end tell
end tell
end _addrecipient
-- I'm adding attachments to the eMail message
-- Note: File names must be passed in a list as Posix paths
on addattachments(atmlist)
tell application "Mail"
tell content of curmsg
repeat with atm in atmlist
make new attachment with properties {file name:atm} at after last paragraph
end repeat
end tell
end tell
end addattachments
-- I'm setting the used signature of the eMail message
on setsignature(signaturename)
tell application "Mail"
set sign to signature signaturename
tell curmsg
set message signature to sign
end tell
end tell
end setsignature
-- I'm sending the eMail message
on send()
tell application "Mail"
send curmsg
end tell
end send
end script