Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript: creating mail rules

Hi Community,

I can't find the syntax for creating a mail rule in applescript anywhere on the web. If someone could just post all the different options in one snippet (Move Message, Play Sound, Set Color, Delete Message, and run applescript) that would be awesome. Thanks!

Posted on Aug 23, 2011 10:13 AM

Reply
Question marked as Best reply

Posted on Aug 23, 2011 10:31 AM

you can find a sample rule action on your own computer. look in /Library/Scripts/Mail Scripts/Rule Actions/


basically the syntax is as follows:


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule


-- ...

tell application "Mail"


-- ...

end tell

end perform mail action with messages

end using terms from


you need the 'using terms from' bit otherwise the script won't compile. You can do whatever you want, though most simple rule actions don't require applescript at all. for instance, to move or delete a message you just choose Move Message or Delete Message in the 'perform the following actions' section of the mail rule.

13 replies
Question marked as Best reply

Aug 23, 2011 10:31 AM in response to chumponimys

you can find a sample rule action on your own computer. look in /Library/Scripts/Mail Scripts/Rule Actions/


basically the syntax is as follows:


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule


-- ...

tell application "Mail"


-- ...

end tell

end perform mail action with messages

end using terms from


you need the 'using terms from' bit otherwise the script won't compile. You can do whatever you want, though most simple rule actions don't require applescript at all. for instance, to move or delete a message you just choose Move Message or Delete Message in the 'perform the following actions' section of the mail rule.

Aug 23, 2011 10:52 AM in response to chumponimys

That's not hard to do.


This rule will match any message that contains the text 'woohoo' and forward it to my @me.com address:


tell application "Mail"

set newRule to make new rule at end of rules with properties {name:"Woohoo rule", forward message:"camelot@mac.com"}

tell newRule


makenewrule conditionat end of rule conditionswith properties {rule type:message content, qualifier:does contain value, expression:"woohoo"}

end tell

end tell


You can look at Mail.app's dictionary to see the other actions your rule can take.


It's a little odd in that you first define the rule actions (e.g. forward, reply, move, delete, etc.) and then you define the rule conditions, which is the opposite of how the rules are presented in Mail.app's preferences, but the outcome is the same.

Aug 23, 2011 11:09 AM in response to Camelot

I was playing with this while Camelot was responding. it seems that there are some limitation to this that are noteworthy:

  • apparently you can only effectively use one rule action at a time - when I try to do multiples (a move and a flag command, for instance) only the first shows up. that may fix itself after a restart of Mail, I don't know.
  • apparently Mail reuses plist entiries, so if you reuse a rule name old setting may be preserved unintentionally.

There may be a more effective way of doing this by editing the Mail plist directly - I'll have to look into it later.

Aug 23, 2011 12:01 PM in response to twtwtw

apparently you can only effectively use one rule action at a time


I don't agree with this statement.


Here's an example that forwards a message, highlights it in green and marks it as read:


set newRule to makenewruleat end of ruleswith properties {name:"test rule", mark read:true, highlight text using color:true, color message:green, forward message:"camelot@mac.com"}

Sep 12, 2011 10:27 AM in response to twtwtw

I agree with twtwtw. Most of the rules I use simply organize the inbox into separate mailboxes. So with a list of 150 or so students each semester, it would be great to have this script work..it creates the rules, one for each student number, but then the rule won't move the incoming messages. Hmm...


So if anyone figues out why, it would be great to let us know!


tell application "Microsoft Excel"

set hpath to "Macintosh-HD:Users:bg:20f11:Hmwk:graded:"

set theGrades to open workbook workbook file name "Macintosh-HD:Users:bg:20f11:20f11_grades.xlsx" update links do not update links

set email_list to value of every cell of range "e_mail"

set theMailbox to "20f11"


activate

set sn to value of every cell of range "sn" --sn is student number

end tell

tell application "Mail"

repeat with j from 1 to length of sn

set newRule to makenewruleat end of ruleswith properties {name:itemj of sn), enabled:false}

tell newRule


makenewrule conditionat end of rule conditionswith properties {rule type:from header, expression:itemj of email_list), qualifier:does contain value, move message:mailboxtheMailbox}

end tell

end repeat

end tell

Sep 12, 2011 11:58 AM in response to notYetRated

but then the rule won't move the incoming messages


I haven't tried, but it seems obvious to me:


set newRule to makenewruleat end ofruleswith properties {name:itemj ofsn), enabled:false}

You're making a rule that is disabled ('enabled: false'), so it's not going to run against the incoming emails.

Switch this to 'enabled: true' and you should be good, no?

Sep 12, 2011 1:20 PM in response to Camelot

Enabled: false is not the problem. The reason it is set that way above was that after trying to get it to work all morning with Enabled: true I decided to disable the rule so I could then manually enable it. Usually if one manually enables a rule--a rule created outside of applescript--mail asks if you want run it. This always works or seems to. But for some reason when the rule is created by the above applescript, it fails to respond whatsoever. It seems that twtwtw is right...just doesn't fly.


Thanks in any case for looking at the script.

Applescript: creating mail rules

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.