Adding Gmail like shortcuts to Mail.app

I've been looking for this for a long time and I just got it all sorted out, so I thought I'd note it down here for all comers. It helps with my endless quest to achieve Inbox Zero.

Specifically I wanted to replicate the y hotkey functionality (yank) provided in Gmail so I don't have to go to the mouse to archive messages from my inbox. There are tools around that can help out with this, and the pick of the bunch seems to be Mail Act-On, but for what I want (one simple function) US$24.95 seems a little steep to me.

So here goes - a free solution which I find cleaner, more flexible and more powerful (once you learn a little AppleScript).

Prerequisites

There's only one thing you'll need that you might not already have if you're running OS X Lepopard ((This has only been tested on Leopard. I suspect it will work on Snow Leopard and Tiger as well, but have no idea about anything else.)), and that's the ever so brilliant Quicksilver ((If you're using OS X and not already running Quicksilver you're insane. Insane.))

Do it already

Step 1 - The script

First up we're creating an AppleScript which is going to take the currently selected messages and move them to a specific folder. I've pretty much just taken this script from a post over at Mac OS X Hints. So fire up Script Editor (it's in your Applications folder) and paste in the following:
tell application "Mail"
  set theSelectedMessages to selection
  repeat with theMessage in theSelectedMessages
    set theMailbox to "Archive"
    tell application "Mail"
        move the theMessage to mailbox theMailbox of account "name of mail account"
    end tell
  end repeat
end tell

There are a couple of bits in the code you'll need to customise for your particular setup.

  1. Change the Archive bit in the line that says set theMailbox to "Archive" to the name of the mailbox you want to move the message to.
  2. Change the name of mail account bit which is two lines down from the last change to the name of the mail account which the folder is in (you can find out in Preferences » Accounts » Account Information. It's the field labeled Description.
At this point you can test that it's working by selecting an email you want to move in Mail.app, switch back to Script Editor and click the Run button in the toolbar. Give it a sec, and hey presto, your mail should move. If there's a problem Script Editor should tell you and you've got a chance to fix it (which is why we're testing now, because it won't tell you later, but just fail silently, which sucks) ((The biggest problem you'll encounter here is making sure the name of the folder is what Mail expects. Mail is a little pedantic, especially if you're dealing with IMAP folders. Also, the behaviour with IMAP changes somewhat between the Tiger and Leopard versions of OS X.))

Now save it in a place that's logical for you (mine is in ~/Library/Scripts. You'll see a couple of options when you save, just make sure the File Format is set to Script.

Step 2 - The Quicksilver Trigger

Open the Custom Triggers preference pane in Quicksilver. ((Since you're a lover of keyboard shortcuts you should know that this can be done with just four keystrokes. First, invoke QS (my shortcut is Control+Space) then hit Cmd+' (that's a single quote mark) and you're there.))

Add a new 'Hot Key' trigger (the little plus sign at the bottom of the window). This will bring up a dialog matching the usual QS work flow of Item, Action, Target. Select the script you just created as the Item, ensure the Action is Run, and ignore the Target. Click Save.

Next we need to define the hot key which invokes the saved command. If the tray isn't already open, click the little 'i' for info icon in the bottom right. Choose your hot key under the settings tab and make sure only the first checkbox (On Press) is checked.

Finally, I defined the scope so that this hot key will only work when I'm in Mail.app. It'll be useless elsewhere and I didn't want strange things happening if I hit it by mistake. Just type Mail.app into the big text box and hit enter.

There you have it

A two stroke keyboard shortcut for moving messages to an archive folder in Mail.app using nothing but free stuff.

If you're on the ball you've probably noticed that this exact same method could be used for making all kinds of sweet shortcuts. You could, if you're ambitious, emulate all the relevant Gmail hot keys. And, hell, why stop at Mail.app. The trick is getting to know AppleScript. Go nuts.