Automate Mail with Applescript (up to 10.6)

ever wondered how you can setup an automated mailing list to organise e.g. your sports team?

our problem: not getting enough numbers for our weekly team sport and having to go down to the park in vain!

our solution: setup our mac mini running 10.6 using apple script to send an invitation, collect and count replies then send a numbers email. and last but not least delete all emails so the script can do its thing the following week all over again.

here the details…

step 1
get a google email for your team here.

step 2
amend your team’s contact entries in addressbook with an identifier in the notes field, e.g. soccer. create a smartgroup with the same name to collect them all, then right click on the newly created smart group and select ‘Edit Distribution List’ to select an appropriate email address.

step 3
using simple text create the body of your invitation email and save it as ‘invitation.txt’

step 4
fire up applescript editor (‘/Applications/Utilities/AppleScript Editor.app’) and create the following 3 scripts (copy and paste):

send invitation email (save as ‘invitation.scpt’)

--enter the name of the account in mail you want to send from
set theAccount to "REPLACE WITH YOUR ACCOUNT'S DESCRIPTION"

–enter your subject line
set theSubject to “REPLACE WITH YOUR SUBJECT”

–enter the exact path to the text file containing the message body, use the “HD:Folder:File” format
set theText to “REPLACE WITH PATH TO INVITATION.TXT” as alias
set theContent to read theText

–enter the address you’re mailing to, if you want to send to multiple addresses make a group in address book and put in the group name for the address
set theToRecipient to “REPLACE WITH THE SMART GROUP CREATED IN STEP 1”
set theBccRecipient to “REPLACE WITH YOUR EMAIL ADDRESS”

tell application “Mail”
activate
set newMessage to make new outgoing message with properties {account:theAccount, subject:theSubject, content:theContent}
tell newMessage
make new to recipient at end with properties {address:theToRecipient}
make new bcc recipient at end with properties {address:theBccRecipient}
set visible to true
end tell

–comment this line out if you want Mail to make but not send the message
send newMessage

end tell

count replies & send numbers email (save as ‘numbers.scpt’)

--enter the name of the account in mail you want to send from
set theAccount to "REPLACE WITH YOUR ACCOUNT'S DESCRIPTION"

–enter your subject line
set theSubject to “REPLACE WITH YOUR SUBJECT”

–enter the recipient(s)
set theToRecipient to “REPLACE WITH THE SMART GROUP CREATED IN STEP 1”
set theBccRecipient to “REPLACE WITH YOUR EMAIL ADDRESS”

tell application “Mail”
activate
check for new mail

tell account theAccount
tell mailbox “INBOX”
–set sportParticipants to sender of every message of it
–above line commented out, but can be used for adding a list of participants
set sportNumbers to (count of every message of it) + 1
end tell
end tell

set theContent to “team,” & return & return & “here this REPLACE DAY numbers: ” & sportNumbers & return & return & “EXAMPLE TEXT: if the above number is 6 plus, then see you DAY @ 0900h at PLACE. however, if it should fall below 6 it is simply not enough fun and sport is off until next week… ;0(” & return & return & “disclaimer:” & return & “even though we are grown up men and some say the weather should not keep us from having fun, and getting our gear real dirty – to ensure that you get notified on DAY morning if a snorkel is required, please provide us with an email address that you can check before coming down!”
–adjust above text to suit your requirements

set newMessage to make new outgoing message with properties {account:theAccount, subject:theSubject, visible:true}

tell newMessage
set content of it to theContent
make new to recipient at end with properties {address:theToRecipient}
make new bcc recipient at end with properties {address:theBccRecipient}
end tell

–comment this line out if you want Mail to make but not send the message
send newMessage

end tell

 

run all rules in mail (save as ‘allrules.scpt’)

tell application "Mail" to activate

–Select Inbox
tell application “System Events”
tell process “Mail”
keystroke “1” using command down
end tell
end tell
delay 0.5

–Select all messages in Inbox
tell application “Mail”
tell front message viewer to set selected messages to messages
end tell
delay 0.5

–Run rules on all selected messages
tell application “System Events”
tell process “Mail”
keystroke “l” using {option down, command down}
end tell
end tell

step 5
in ical create repeating entries (weekly) to trigger the above invitation & numbers scripts according to the deadlines provided in your emails.

step 6
to delete emails from your given account in apple mail, setup a rule (apple mail > preferences > rules) and use the ‘run all rules in mail’ script from above triggered via an ical event.

IMPORTANT: be very careful that the rule in apple mail only deletes the intended emails by your sports team and nothing else!

step 7
ensure that apple mail and ical are running at all times on your mac hosting this setup and that the sending account is the first one in apple mail.

please note the above is cobbled together from various freely available sources, amongst others also from the following:
http://discussions.apple.com/thread.jspa?threadID=1410843
http://macscripter.net/

Next Post

Previous Post