How to make AppleScripts Execute Correctly under Mail Rules
How to make AppleScripts Execute Correctly under Mail Rules
How to make AppleScripts Execute Correctly under Mail Rules
For my small software enterprise I constantly rely on Mail Rules. PayPal will notify me by email about every transaction and I have set up Mail Rules to respond to the incoming mail such that the customers get a purchase receipt and a thank you email. The Mail Rules do this but triggering AppleScripts and keeping the emails organized by moving emails to appropriate mailboxes. This is managed by our Mac mini which is located under our television set. In addition to playing Teletubbies and Netflix it is always running in the background performing this business mail server duty.
The problem: Ever since Mountain Lion, the Mail Rules have behaved erratic and inconsistently. Troubleshooting reveals that the rules are triggered correctly but often the AppleScripts are not executed properly. For example, the rule will move the incoming emails to the correct mailbox but a response email fails to be sent out. Very frustrating for myself and for the customers. Even more bizarre, the scripts are run just fine everytime if I trigger them manually from Mail by selecting the emails and hitting “Apply Rules” from the menu bar. I have browsed support groups and scripting websites for years and also filed a bug report with Apple’s radar system.
The fix: There are probably multiple issues with having Mail Rules executing AppleScripts. But I have found that the most problems appear when running scripts are combined in a rule that also moves the message to another mailbox. It is almost like the moving will appear asynchronous such that the script fails to find it and therefore cannot parse the necessary info it needs to run right. Here is what I do:
1. Don’t mix move commands and scripts in rules that handle the same email.
2.Instead, have the AppleScript handle the message shuffling. Here is a code example:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set theAccount to name of account of mailbox of eachMessage
-- This makes sure the response emails are send from the right email address later
set emailFrom to extract address from reply to of eachMessage
try
set recipientAddress to emailFrom
set theSubject to "The message subject goes here"
set theSendContent to "The message body goes here"
set theSender to "The outgoing email address goes here"
set theOutMessage to make new outgoing message with properties {sender:theSender, subject:theSubject, content:theSendContent}
tell theOutMessage
make new to recipient at end of every to recipient with properties {address:recipientAddress}
end tell
send theOutMessage
end try
-- THIS IS THE TRICK!
move eachMessage to mailbox "Name of mailbox goes here" of account theAccount
end repeat
end tell
end perform mail action with messages
end using terms from
Next problem: The emails that are sent to the customers is typically the text added by the AppleScript, as in the example above. The minor issue with that is that it will be added as plain text with no formatting options other than the generic outgoing style. But I think the customer deserves that I use bold type face, various text sizes and even a mix of fonts and embed URL links etc. How to do that?
The fix: Add the text as a signature rather than as email body text! This tip was sent to me by the bright guys at Bee Software. Signatures in Mail can hold all those formats and the AppleScript can be told to add a certain signature. Simply draft a couble of signatures and have the script pick the one you want to use. The added bonus is that signatures are quick to modify so you don’t have to fire up the Script Editor every time you want to update you outgoing message. Here is the example above spiked in with the signature trick, which is highlighted below:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set theAccount to name of account of mailbox of eachMessage
-- This makes sure the response emails are send from the right email address later
set emailFrom to extract address from reply to of eachMessage
try
set recipientAddress to emailFrom
-- HERE IS THE TRICK...
set theSignature to "The name of the signature has the formatted content goes here"
set theSubject to "The message subject goes here"
set theSendContent to "The message body can now be empty if you wish"
set theSender to "The outgoing email address goes here"
set theOutMessage to make new outgoing message with properties {sender:theSender, subject:theSubject, content:theSendContent}
tell theOutMessage
make new to recipient at end of every to recipient with properties {address:recipientAddress}
end tell
-- ...AND HERE IS THE TRICK
set the message signature of theOutMessage to signature theSignature
send theOutMessage
end try
move eachMessage to mailbox "Name of mailbox goes here" of account theAccount
end repeat
end tell
end perform mail action with messages
end using terms from
That’s it for now, folks. If you like these tips, please thank me by hitting one of them pesky google adsense links on your right. Not that it’ll cover the cost for this server hosting but anyway... Cheers!
Friday, August 2, 2013