Archive messages with a single keystroke in Mail.app

Aug 2011 Lion update: the script works but is quite slow in Lion (it’s zippy in Snow Leopard). I’m looking for a workaround, but this appears to be a Mail.app bug.

TL;DR version: Archive Mail messages with a single keystroke:

  1. Download this script and Fastscripts (free for up to 10 hotkeys)
  2. Move the script to ~/Library/Scripts/Applications/Mail/
  3. Set your hotkey in Fastscripts (Using a letter key is possible but not recommended. Try another character like `\/=- or an F-Key.)

The script will then move selected message(s) to a folder named “Archive”.

More information:

Existing shortcuts to file messages in Mail.app get you down to two or three keystrokes, but that just isn’t good enough for someone who has tasted the sweet, sweet bliss of single-keystroke archiving in Postbox or Gmail.

In my original reply to this superuser thread, I suggested using an AppleScript to move selected messages to an archive, and triggering the script using a single-key shortcut using Fastscripts. That script is simple and straightforward, but it has a major shortcoming: it leaves you hanging with no next message selected, so you have to manually select your next message. Not ideal.

My new script archives messages with a bit more smarts. Here’s what it does:

  • If a mailbox is in the foreground, the script moves selected messages to the folder named “Archive” and selects the next available message. Boom.

  • But you don’t want to accidentally archive messages whenever you hit your archive key. If the frontmost window isn’t a mailbox, the script will ignore the archiving functions and (optionally) type some text wherever you are. This is useful if you use a single key to trigger the script; without this function, you would never be able to type that key into a mail message because it’s intercepted by FastScripts before getting to the Compose window.

For interested scripters, here were some challenges:

  • Select next message: The solutions I found online select the next message by sequential Message ID, which usually means that it only works if your mailbox is sorted by Date Received. Using visible messages gets messages in the order in which they’re displayed. Watch out, though: if message threading is turned on, the top-level thread item is not selectable. See my workaround in the script.

  • Enter the keystroke that was captured: Merely telling System Events type the character that triggered the script will trigger the script again, resulting in a virtual infinite loop. I used a paste routine to work around this.

That’s it… happy archiving!

22 Mar 2011 update: fixed bug that caused selection to be lost when only the topmost message in a mailbox is selected.

applescripticon.gif

Download: Archive Selected Messages