Bill Lovett

Filing Email with AppleScript

Posted on August 4th, 2006

Let's create an AppleScript that moves email messages in Apple Mail to a predetermined folder, similar to the way Gmail's Archive button works. To Google!

Aaron Swartz has something that looks like it'll fit the bill nicely. In Mail.app mutt, he offers a fairly complicated script that only needs to know the name of your target folder and the name of your account. Even better, we can apparently assign a keyboard shortcut to the script by appending three underscores and the desired keys to the filename. Easy!

Not so fast. This script is old-- the post was from October 2003 and carries a warning about the script probably only working in Mac OS X 10.3 Panther. This turns out to be true. The current version of OS X won't take kindly to this script. Among other things, the keyboard shortcut trick is no longer viable. And it turns out the script could be simplified quite a bit.

The wisdom from people who know about such things and have left their opinions scattered on blog comments and mailing lists suggests that all we really need here is something like this:

••
tell application "Mail"
	set target_mailbox to mailbox "archive" of account "IMAP on example.com"
	set selmsgs to selection
	repeat with eachmsg in selmsgs
		move eachmsg to target_mailbox
	end repeat
end tell

I got the name of the account from opening up the preferences window and looking at the account list. The mailbox name is what you'd expect it to be. If you select a message in Mail, then switch back to Script Editor and run the script, the selected message should get moved. This is a handy testing maneuver.

That leaves the keyboard shortcut question. We need a third-party utility since Apple's Script Menu doesn't allow shortcuts to be assigned to scripts, even though it was apparently able to in the past. The Keyboard Shortcuts screen of the Keyboard & Mouse preference window won't cut it. FastScripts will. As long as the script is saved to the Library/Scripts folder in your Home directory (it doesn't need to be saved as an application), it should show up in the FastScripts menu and become eligible for shortcut assignment.

Back to the index of all blog entries