Monday, January 21, 2008

Tutorial - Making Nice with Rules

Tutorial Introduction

I got set up to use Gmail's IMAP, and wrote a bit about it in this tutorial. One of the next steps I wanted to take, was to somehow manage all the mail that comes in from subscriptions, or, regular announcements from colleagues regarding common reporting milestones. In Microsoft Entourage, I was using an AppleScript and Rules, so I reckoned I could use a similar approach in

This tutorial describes how I solve the challenge, marking my subscriptions and regular mails as "Expiring", so that I can use a smart folder to view and prune them regularly.


Here's what you need to follow this tutorial:
  1. Leopard
  2. Andreas Amann's Mail Scripts
  3. John Maisey's YAI You Are Invited
  4. Scott Morrison's Mail Act On

Let's get started.

Understanding Some Initial Failures

At first, I went down the path of attempting to use Scott Morrison's MailTags beta for Leopard to try to tag incoming mails with a keyword and project, but in the end, I was forced to give up using this excellent bundle. It seems that Google Mail's IMAP implementation is simply non-standard, and it does not play well with what MailTags needs to do, which as I understand it is to write tag information into custom mail headers. So if you use Gmail and IMAP, you're out of luck to be able to use MailTags at this point.

Secondly, I was thinking to use my technique from Entourage, which was to use an AppleScript fired by a rule for all incoming mail, to check a list of recipients. This was not very easy to use though, because I was forever opening up the script to add new addresses. Not very elegant.

In the end, what worked was to check to see whether a sender was in an Address Book group called Expiring, then check for this membership in a rule, moving the message into the Expiring folder. Rules Intro

You access rules in preferences. Cmd-, while in Mail will bring it up, as in all Mac apps. The rule tab after being populated with some rules looks like this. Rules Expiring

There are a few points to note about rules:

  • Rules are processed from top to bottom.
  • will stop processing a rule either when it encounters a rule with the "stop evaluating rules" Action enabled, or, when a rule moves a mail to another mailbox.
  • There is no complex nested boolean logic to use within rules, so you need to make a few of them to cover your cases. You are limited to checking if All criteria or Any criteria are true, which translates to an AND or an OR on the criteria you enter. You cannot make a statement like, if 1 AND 2 are true, OR 3 is true, then do the following actions.
  • You can apply the criteria of when a message is "From a given Address Book group" but you cannot check when a message is "To a given Address Book group".
  • It is best to be specific when creating rules, especially with regard to exact senders and recipients. Leaving To undefined, for example, may cause some unexpected problems.

Creating a Rule - If Sender in Expiring Group AND Message is To Me

In the Rules preference panel, click Add Rule to add, then fill it out. This rule checks to see if a sender is in the "Expiring" group in Address Book, AND if the message is addressed To me are true (using the If All selection), then move the message to the Expiring folder, and set its color to purple. Rules Expiring to Cogley

Because the rule moves the message to another folder, stops evaluating further rules. You can create one of these rules for each of your main mail addresses, that is, move any mails From members of Expiring group to me@corporate, or me@gmail.

Note - there is a bug in Leopard 10.5.1, which does not allow you to set color first before moving and therefore stopping the evaluation of the rule. I tried to enter the actions in the correct order for doing this, but, rearranges the rules, putting the color change second, and therefore, it does not apply color consistently.

Easily Add Senders to Expiring

To make adding senders to an "Expiring" (or any other) group easy, use "Add Addresses" from Andreas Amann's excellent Mail Scripts. Prepare to use it by adding the group first, in Address Book. Then, when a mail comes in From:, To: me@corporate (for example), you can select the message, and then choose "Add Addresses" from the Mail Scripts folder in your Scripts folder. Select the addresses to add, and choose the group and label, as appropriate, which you have created in Address Book beforehand.

Mailscripts Add Addresses

Senders who are in Address Book group Expiring will trigger the rule, and it will move the message to the specified folder.

Other Cases - Mail is From and To the Same Sender

Many times, newsletters are sent To: and From: the same address. In this case, your above rule will not work, and you have to create specific rules for each case. To make this easy, use "Create Rule" from Mail Scripts.

In your mailbox, select a newsletter with To: and From: that do not fit your catch-all rule, then choose Create Rule in your Mail Scripts folder in your Scripts folder. Create Rule has intelligence built in, and will detect the addresses on the mail, and add them to the dialog for you to easily select.

Create To-From Rule with Mail Scripts

You can apply the new rule by ctrl-clicking the mail, and choosing Apply Rules from the context menu.

Apply Rules in Context Menu

In my case, I have a number of mails like this that once they are read, I know I will not refer to them again. I repeated this rule create step for each one, and it is quick enough to do when one comes in that you forgot about.

You're Invited and OmniFocus

John Maisey's YAI is an AppleScript written to deal with the problem of not being able to process the TimeZone information that is embedded in Microsoft Outlook / Exchange calendar invites. You can open the invites in a text editor, and replace the offending line with a correct one, but, it is so much easier to get YAI to do it for you. The YAI installer creates a rule, that sits on top of your rule stack, and fires for every mail. It looks like this: You Are Invited YAI

Basically, it says for every message, run the AppleScript at such-and-such path. Another example of such a rule is the rule that can be installed with OmniGroup's superb OmniFocus which is an app that helps you implement GTD, or Getting Things Done. Here, you can see that if detects the OmniFocus "secret code" of a subject line starting with two dashes, then it will run the AppleScript to put the mail into OmniFocus where you can act upon it. Rules Send to OmniFocus

Mail Act On

Another application that uses rules is Scott Morrison's Mail Act On. While MailTags did not work in my environment (Gmail), Mail Act On does, and gives a popup bezel to apply a particular rule to a selected message or messages. I use this to move certain mails in Expiring to another folder called Reference. To use Mail Act On after installing it, you press a key sequence to bring up the bezel (mine is set to Shift-Cmd-`) and then hit the key to apply the rule. So, Shift-Cmd-` then r means "move to reference" for me. See if you can make it work, and note that Mail Act On rules appear at the bottom of your rules list, after an initial one that has a "stop evaluating" action.


In this tutorial, you read some basic pointers about's Rules processing, and can process messages that will "expire" in an effective manner, so that you can later prune or archive them. The cast of characters in this play are Andreas Amann's Mail Scripts, John Maisey's YAI You Are Invited, and Scott Morrison's Mail Act On as well as Apple's

Decision Summary

During this setup, we made the following decisions:

  • To forgo MailTags, but use Mail Act On to allow us to apply rules selectively.
  • To reference in Rules an Address Book group called "Expiring", the members of which are senders of newsletters and such like.
  • To use Mail Scripts to assist in making operations easier.
  • To use YAI to automate changing the timezone of Outlook calendar invites.

Good luck, and if you find better ways of doing things in a similar environment, please do not hesitate to comment.


Adam said...

You mention a bug in which does not allow you to set color first before moving messages. This has been driving me nuts for quite some time as it requires that I manually Apply Rules on all my incoming messages in order to get them color coded after they have been automatically moved.

I haven't been able to find much documentation on this bug or a viable workaround. Any tips or suggestions? (e.g. An Apple Script that would apply color rules to a message after it is moved.)

P.S. Thanks for the tutorial.

Rick Cogley said...

You're welcome, and I'm glad you're finding it useful. Unfortunately, it seems it's still the same as before. I'm upgraded to the latest, and I've set and reset those things occasionally, but it has not improved nor have I found a workaround.

Thanks for reading.


Adam said...

Bummer. FYI I've posted a question on the Apple discussion board with a cross-reference to your blog in hopes that someone has a solution. Seems like we can't be the only two people who want to color then file messages! Thanks again.

Justin said...

Helpful stuff. Thanks very much.

Rick Cogley said...

@Justin - thanks for dropping by!

M said...

Hi, thanks for a wonderful tute on rules. What I have been trying to implement is a check to see if an identical message is in the Junk folder and if so move the message there. I get cases where multiple spams are sent to me over many days and this would mean I just have to flag ome message and the rest ....

Any ideas would be welcome as it seems that there is no inbuilt mail rule that checks for duplicate content.


Rick Cogley said...

Hi Miro - you are welcome and thank you for stopping by. I use MailSteward to do the dupe-removal before archiving. It keeps things slim and outside the IMAP mailboxes, so I can just make sure I have my MailSteward archive DB, and kind of delete with abandon.