Publishers of technology books, eBooks, and videos for creative people

Home > Blogs > Mac Productivity: Quick Scripts and Workflows - Archiving Selected Mail Messages

Mac Productivity: Quick Scripts and Workflows - Archiving Selected Mail Messages

By  Mar 12, 2012

Topics: Apple, Operating Systems, Productivity

Keeping up with email often seems like a losing battle.  One thing that can help is reducing the number of messages in your inbox.  Filing messages into individual mailboxes can be time consuming and inefficient.  While dumping them all into a single mailbox may seem like a way to create a big mess, Mail’s search capabilities actually make it quite easy to locate specific messages.

Taking a lesson from Gmail’s All Mail feature, Mail in Lion includes a new option for archiving messages.  Just select a message, choose Message > Archive from the menu bar, and the message is moved into an Archive mailbox, which Mail will create if it doesn’t exist.  This feature gives Mail users a quick and easy way to file messages, getting them out of that bloated inbox.

I’d love to use Mail’s built-in Archive feature.  However, it only archives to the mail server (for IMAP accounts, anyway, which is what I have).  I keep my email archive locally.  So, I used a combination of AppleScript and Automator to create my own Archive feature.  Here’s how...

1. Launch Automator (in /Applications)

2. When prompted to choose a type of workflow, select Service, and click Choose

Creating an Automator Service workflow

3. Set the popup menus at the top of the workflow area to receive no input in Mail, and leave the Output replaces selected text checkbox de-selected

Setting the Service workflow to appear in Mail

4. Automator doesn’t include a built-in action for moving messages in Mail.  So, we have to do it with AppleScript.  Search for the Run AppleScript action, and drag it to the workflow area.

5. Enter the following AppleScript into the action:

tell application "Mail"
	set theMessages to selection
	set theArchiveMailboxName to "Archive"
	if (mailbox theArchiveMailboxName exists) = false then
		make new mailbox with properties {name:theArchiveMailboxName}
	end if
	repeat with aMessage in theMessages
		move aMessage to mailbox theArchiveMailboxName
	end repeat
end tell

(This script gets any selected messages, and moves them to a mailbox named Archive on your Mac.  If that Mailbox doesn’t exist, then the script creates it for you automatically.)

The completed workflow

6. Save the workflow, and name it Mail • Archive Selected Messages

Saving the Service workflow

7. Launch System Preferences and go to the Keyboard preference pane

8. Click the Keyboard Shortcuts tab, and select Services in the left-hand list.  Then, in the right-hand list, scroll down to Mail • Archive Selected Message.  Double-click to its right and type the keyboard shortcut Control + Option + A.

Assigning a Keyboard Shortcut to the Service

That’s it.  The workflow should now appear in the Services menu when you’re in Mail.  To begin archiving, select a message or two.  Then, choose Mail > Services > Mail • Archive Selected Messages from the menu bar.  Or, you can trigger it by pressing the keyboard shortcut Control + Option + A.

Triggering the Service

NOTE: If the keyboard shortcut doesn’t work, try manually triggering the workflow from the Services first.  If the Service doesn’t run immediately, give it a few seconds to register with Mac OS X.

For more on AppleScript and Automator, check out my book  Automator for Mac OS X: Visual QuickStart Guide, as well as my video podcast series Mac Automation Made Simple.