Applescript: reorder mail rules

Hi Community,

I can't find the syntax for reordering existing mail rules using applescript anywhere on the web. If someone could just post an example of how to do this that would be awesome. Thanks!

Posted on Sep 29, 2020 9:14 AM

Reply
5 replies

Sep 29, 2020 4:16 PM in response to apple_leolabs

So the order of the rules does matter.

Yes, the order matters in Mail, but the order pulled by getting the rules may not be changeable except by deleting all of them and inserting them via AppleScript in the order you wish.


And, as I already stated, if you don’t see it in the Library, there isn’t a command to do it (whatever the “it” is).


I tried to get the list of rules and edit it, but it did not take. You would have to extract each rule and store that copy so you could rebuild the list through inserting the copy into the list.

Sep 29, 2020 9:34 AM in response to apple_leolabs

Open the Mail library in Script Editor. That will tell you everything you can do with Mail using AppleScript.

There isn't an order to rules except maybe inserting them into the application in the order you wish.

If the position in the list matters to Mail, then you could insert your rules into the list of rules where you want it.

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateListsofItems.html

Sep 29, 2020 12:41 PM in response to Barney-15E

According to https://support.apple.com/guide/mail/use-rules-to-manage-emails-you-receive-mlhlp1017/mac "If you have multiple rules, they’re applied in the order in which they appear in the list of rules; drag a rule up or down to change the order."


So the order of the rules does matter.


And it appears that you're correct when creating a rule. The following applescript syntax can be used:


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


Notice the "at end of rules" syntax. This implies (at least during the creating of a rule) that it's location in the list of rules can be set. Not exactly sure what the syntax would be to put it in a specific location. Any thoughts on that within the above code?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript: reorder mail rules

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