Categories
How To Mac OS X

How to Use Keyboard Maestro to Archive Email in Mail.app

A while back, we published a short tutorial showing how to archive mail with a keyboard shortcut. This tip in itself is a huge timesaver, but one reader wanted to take things a little further and asked how to use a keyboard shortcut to move messages to specific mailboxes and not only the archive.

After better understanding the requirements, the answer was obvious to me — use Keyboard Maestro, a premiere Mac automation tool. Keyboard Maestro may seem daunting at first, but once you get a grasp for its mechanics, bending your Mac to your will becomes child’s play.

Creating a Palette

First you need to create a palette to hold all your destination mailboxes. This can be achieved easily by grouping all your macros together.

  1. Create a new Macro group by pressing ⇧⌘N (or clicking the bottom left +) and give it a name;
  2. Click Available in all applications and change to Available in the following applications:, then choose Mail.app. This will ensure the palette only displays when Mail.app is active;
  3. Click Always Activated and choose Show a palette for one action when:. Finally assign the desired keyboard shortcut (I went with ⇧⌘E).
Step1.png
Your new macro group ready for macros.

Adding Macros

Now that you have your macro group created, it’s time to populate it with macros.

  1. Press ⌘N (or click the + in the bottom middle) and give it a name. Note that the name you choose is important since Keyboard Maestro will sort your macros in alphabetical order. Fortunately, there is a small trick that will allow you to manually sort your actions. By simply prefixing the name with 2 digits and a closing bracket such as 00), Keyboard Maestro will hide the numbers, but still use them to sort your macros.
  2. Click New Trigger and add a Hot key trigger. Since it’s possible to click on a macro inside the palette, this step is optional, however I strongly advise it.
  3. Click New Action, from the panel that appears, click on Execute and then double click Execute and AppleScript;
  4. Leave Execute Text Script as is and change ignore results to display results briefly.
  5. Finally paste in the following script. Remember to adapt it to suit your particular needs: on line 7, change App Testing to your desired mailbox and iCloud to your account name (Gmail, Personal, Work etc.).
try
tell application "Mail"
set msgs to selection
if length of msgs is not 0 then
repeat with msg in msgs
try
move msg to mailbox "App Testing" of account "iCloud"
on error errmsg
return errmsg
end try
end repeat
return "Moved " & length of msgs & " messages to App Testing in iCloud Account'"
else
return "Please Choose Some Messages."
end if
end tell
on error errmsg
return errmsg
end try

 

Tip: If you’re unsure what your accounts are named, simply open Script Editor and paste in tell application "Mail" to get accounts. This will give you a list of accounts. Similarly, tell application "Mail" to get mailboxes of account "iCloud" will give you a list of all mailboxes in a specified account.

Step2.pngA good trick is to add a small hint as to which hotkey is assinged to the macro in the macro name.

Now the first macro is complete, duplicate it for as many mailboxes as desired (select the macro and press **⌘D**), remembering of course to change the appropriate values such as the hot key trigger, macro name and AppleScript.

Why Stop There

There’s no reason to stop there though. Why not take this time to create a few macros that’ll allow you to jump to specific mailboxes too.

  1. Duplicate one of the above macros and change its name to reflect the new action: Go to mailbox X (don’t forget to change the hot key trigger too);
  2. In the Execute an AppleScript action, click display results briefly and change it back to ignore results;
  3. Now, replace the previous script with the one below, remembering to change the appropriate values;
try
tell application "Mail"
activate
set selected mailboxes of message viewer 1 to {mailbox "App Testing" of account "iCloud"}
end tell
on error errmsg
return errmsg
end try

If all went well, you should now have a macro palette which can be triggered when Mail.app is active. Trigger it, then either click the desired action or press its hotkey if you assigned one.

Screen Shot 2015-06-07 at 10.19.13

The final output. Create empty macros with just a name (or spaces) to create the seperators seen above.

As you can see, Keyboard Maestro is immensely powerful and easy to use once you’re comfortable with it. If you don’t already own Keyboard Maestro, I strongly advise you download the 30 day trial and see where else you can integrate it.

By Pedro Lobo

I put words together to express my opinionated views on software, hardware or anything that strikes my fancy.