Move To… and Open Mailbox Hotkeys for Apple Mail

Posted August 23rd, 2010 in Software by Justin

I’m a huge fan of keyboard shortcuts and a very hand application of them is in mail applications.  GMail has a move command which I use regularly by pressing v.  I wanted a hotkey in Apple Mail to do the same thing.  Below is a two step process to create a similar move command for Apple Mail with AppleScript.  At the bottom is a similar script that will open a mailbox of your choice with the keyboard.

1. Create a Mail Service to run the applescript

1.   Open Automator and create a new Service

Create Automator Service

2.   For the scope at the top, select the Mail application

Scope

3.   Drag the “Run Actionscript” action from the left pane into the workflow on the right.

"Run Actionscript" Action

3.   You’ll see the “Run Applescript” action appear on the right.

Run Applescript

Paste the following code into the text box replacing the text “(* Your script goes here *)”.

 tell application "System Events"
 activate
 
 ----- ----- BEGIN Config ----- -----
 --You should only need to edit these variables
--Change EmailName below to the name of your email account that appears in the left pane of the mail app
 set myAccount to "EmailName"
--List all the mailboxes from the account above that you wish to provide as options for the Move command
--These must match the mailbox names exactly
 set myMailAccounts to {"Inbox", "1 To-Do", "2 Wait", "3 Completed", "Archive"}
--Copy the name of a mailbox above to use as the default
 set myDefault to {"1 To-Do"}
 ----- ----- END Config ----- -----
 
 choose from list myMailAccounts with title "Move To..." with prompt "Choose Mailbox" default items myDefault
 set selectedMailbox to result as string
 end tell
 
 --Activate Mail in case the dialogue box was cancelled out of
 tell application "Mail"
 activate
 end tell
 
 --loop through options and decide which mailbox was selected
 repeat with i from 1 to count of myMailAccounts
 set this_item to item i of myMailAccounts
 
 if selectedMailbox is this_item then tell application "Mail"
 --if this mailbox was selected, open it
 activate
 set s to selection
 repeat with eachMessage in s
 move eachMessage to mailbox selectedMailbox of account myAccount
 end repeat
 end tell
 end repeat

4.   Change the settings in the CONFIG section of the code to suit your needs and save as whatever you like.  I called mine “Mail – Triage”

Note: You could also save this code as a standalone applescript; then set up something such as QuickSilver to call the script with a trigger of your choice. In theory this would work great.  However, QuickSilver trigger scopes would not work correctly for me.  If you have a fix for this, please let me know.

2. Assign the Service to a hotkey

  1. Open keyboard preferences and choose the “Keyboard Shortcuts” tab
  2. Click services on the left
  3. Find your Mail service on the right under the General heading
  4. Check the checkbox and assign a hotkey by clicking on the right

I’ve set mine up to use the hotkey Shift + Command + V. It may seem like a lot of keys, but it matches well with my other hotkeys (i.e. launching QuickSilver is Shift + Command + Space).

Usage

  • Select at least one email (or have an email window open and selected) then press the hotkeys.  The following dialogue should open.

Move To... Dialogue

  • To choose the mailbox quickly, just type the first letter or two then press enter.
  • That’s it!

Open Mailbox Hotkey

Repeat all the steps above, but use this script instead. You’ll notice a special mailbox used here called “Threaded”, this is a combination of Inbox and Sent items to make the experience more similar to GMail.

       tell application "System Events"
		activate
 
 ----- ----- BEGIN Config ----- -----
--You should only need to edit these variables
--Change EmailName below to the name of your email account that appears in the left pane of the mail app
set myAccount to "EmailName"
--List all the mailboxes from the account above that you wish to provide as options for the Open command
--These must match the mailbox names exactly
set myMailAccounts to {"Threaded", "Inbox", "Sent", "1 To-Do", "2 Wait", "3 Completed", "Archive"}
--Copy the name of a mailbox above to use as the default
set myDefault to {"Threaded"}
 ----- ----- END Config ----- -----
 
	choose from list myMailAccounts with title "Open Mailbox" with prompt "Choose Mailbox" default items myDefault
		set selectedMailbox to result as string
	end tell
 
	--Activate Mail in case the dialogue box was cancelled out of
	tell application "Mail"
		activate
	end tell
 
	--loop through options and decide which mailbox was selected
	repeat with i from 1 to count of myMailAccounts
		set this_item to item i of myMailAccounts
 
		if selectedMailbox is this_item then tell application "Mail"
			--if this mailbox was selected, open it
			activate
			if selectedMailbox is "Threaded" then
				set the selected mailboxes of the front message viewer to {item 1 of inbox, item 1 of sent mailbox}
				set the sort column of the front message viewer to date received column
				set the sorted ascending of the front message viewer to false
			else if selectedMailbox is "Inbox" then
				set the selected mailboxes of the front message viewer to {item 1 of inbox}
			else if selectedMailbox is "Sent" then
				set the selected mailboxes of the front message viewer to {item 1 of sent mailbox}
			else
				set selected mailboxes of the front message viewer to {mailbox this_item of account myAccount}
			end if
		end tell
 
	end repeat

If you have any suggestions for improvements, drop them in the comments below!