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

Mail.app script: Find selected messages in Fastmail

For Fastmail users who also use Mail.app, here’s a script that searches for the selected messages (in Mail.app) in the Fastmail web interface.

Why would you need such a thing? One reason is that Fastmail lets users specify an infinite number of aliases which they can give to an infinite number of websites (and subsequently block, if an infinite amount of spamming ensues). Mail.app doesn’t offer a good way to change your reply address, but replying from the Fastmail web interface does the trick nicely. (Thunderbird users can use the Virtual Identity plugin for this as well.)

Here’s the script:

--Searches for the messages selected in Mail.app using the Fastmail web interface
--By Dan Byler (http://bylr.net)

tell application "Mail"
    try
        set selected_messages to selection
        set remaining to count of selected_messages
        set the_url to "https://www.fastmail.fm/mail/?MLS=MS-*;MSS=!MB-*;SMB-CS="
        repeat with the_message in selected_messages
            set remaining to remaining - 1
            set message_id to the message id of the_message
            set the_url to the_url & "msgid%3A%22" & message_id & "%22"
            if remaining > 0 then set the_url to the_url & "%20OR%20"
        end repeat
        set the_url to the_url & ";SMB-SearchAll=on;MSignal=*P-1"
        open location the_url
    end try
end tell

Or download it here.

Quickly file Exchange-based emails using Entourage

Managing the torrential flow of work email used to be an onerous task, but a healthy combination of email habits and scripts simplify the flow greatly. Personal experience has demonstrated that email is best handled with a GTD-inspired triage system, à la this one. Spotlight on the Mac and Copernicus on Windows make dredging up old emails a cinch.

Even with a simplified organization structure, it can be time-consuming (or RSI-inducing) to triage your inbox. I find that when it takes longer than a second or two to file an email, I’m more likely to just leave it in my inbox — defeating the value of the system.

Although Outlook in Windows is my preferred professional email client, I use Entourage for email triage because it’s just faster. With a few simple Applescripts, filing any number of emails is a single keystroke away.

[code lang="AppleScript"]
property destFolderName : "@ACTION"
property exchangeAccountName : "CHCSII"

tell application "Microsoft Entourage" set destFolder to folder destFolderName of Exchange account exchangeAccountName set currMsgs to (current messages) move currMsgs to destFolder end tell[/code]

Save the script as “Action \ca.scpt” in your Entourage Script Menu folder (located in ~/Documents/Microsoft User Data/) and you can activate it instantly with Control-A. Change the “\ca” suffix to change the shortcut.

Bonus #1: Change your Caps Lock key to Control in the Keyboard & Mouse control panel (under “Modifier Keys…”) and the script is even easier to activate.

Bonus #2: If you have Growl installed, add the following code below “end tell” in the above script and you’ll get an unobtrusive confirmation that your email has been filed in the right folder (useful for the oh-shoot-did-I-just-move-that-to-the-right-folder moments):

[code lang="AppleScript"]
tell application "GrowlHelperApp"

set the allNotificationsList to {"General", "Move message", "Error"}
set the enabledNotificationsList to {"General", "Move message", "Error"}

register as application ¬
    "Entourage AppleScripts" all notifications allNotificationsList ¬
    default notifications enabledNotificationsList ¬
    icon of application "Microsoft Entourage"

--  Error Notification...
if (count of currMsgs) is 0 then
    notify with name ¬
        "Error" title ¬
        "Error" description ¬
        "No message selected." application name "Entourage AppleScripts"
end if

--  1 Notification...
if (count of currMsgs) is 1 then
    notify with name ¬
        "General" title ¬
        "Exchange Notification" description ¬
        "1 message moved to " & name of destFolder application name "Entourage AppleScripts"
end if

--  Multi Notification...
if (count of currMsgs) > 1 then
    notify with name ¬
        "General" title ¬
        "Exchange Notification" description ¬
        "" & (count of currMsgs) & " messages moved to " & name of destFolder & "." application name "Entourage AppleScripts"
end if

end tell[/code]