Use some Mail rules on an as-needed basis via the keyboard

Feb 08, '10 07:30:03AM

Contributed by: flip

If you've ever explored the possible actions offered by Mail's rules, you've probably wished that you could apply some of them to any message on command. For example, you might want to move the selected messages to one mailbox or another at the press of a hotkey, or to replicate Eudora's labels by changing the text color of messages in their mailboxes (Mail's GUI only allows changing their background color, which I dislike as much as OS X's Finder labels).

The problem is that Mail's rules are intended to run automatically on incoming or outgoing emails, so such "manual" rules cannot easily coexist with automatic rules. The good news is that it's very easy to work around that conflict using AppleScript and Keyboard Maestro.

The trick is to group the "manual" rules at the top of the list, end each of them with with 'Stop evaluating rules,' and to keep them disabled so they won't interfere with automatic rules. To run one of these rules manually, you need a Keyboard Maestro macro that will enable that particular rule via AppleScript, tell Mail to 'Apply Rules' (which will run it on the selected messages), and then to disable the rule.

For example, let's make a rule that will move the selected messages to the mailbox named Important. From Mail's preferences, create a new rule named 'Move To Important' and set it like this:

If [any] of the following conditions are met:
  [every message]

Perform the following actions:
  [Move Message] to mailbox [Important]
  [Stop evaluating rules] (don't forget that one!)
Click OK to save and close the rule. Then make sure to uncheck its "Active" checkbox and move it above any rule intended to run automatically. Don't skip this step!!

Then create a new macro in Keyboard Maestro, in a group that makes it active when Mail is frontmost, and set its actions to:
Execute AppleScript:
  tell application "Mail" to set enabled of rule "Move To Important" to true
Type the keystroke:
  ⌥⌘L (the shortcut for Mail's Message » Apply Rules menu item)
Execute AppleScript:
  tell application "Mail" to set enabled of rule "Move To Important" to false
Give this macro a hotkey, and you're done!

Alternatively, if you prefer to run those rules by clicking on buttons instead, you could use Keyboard Maestro's palette, or you could could save an AppleScript script that triggers this macro (Keyboard Maestro will give you the contents of that script) and drop it into a DragThing dock. The possibilities are almost infinite.

When I first got the idea to do this, I was afraid it could interfere with automatic rules if Mail happened to download mail at the same time I ran a manual rule, but I've used lots of such rules for more than a year, now, without any problem. Just don't trigger a manual rule while Mail is already downloading mail, and you shouldn't run into any problem either. Enjoy!

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100204220332317