I’m aware of the fact that it’s a common trend to call email a “nightmare” these days, but the truth is – email works for me. I have multiple addresses set up, I have my filters and smart folders to automate the process of filing and finding emails, and I’m enjoying the renewed interest of iOS developers in building email apps that solve old problems in new ways. But there is one thing I don’t like: Apple’s Mail app and how many clicks it takes to switch between configured accounts and signatures. As you can guess, I came up with a way to automate the process using AppleScript and (optionally) Keyboard Maestro.
I receive several messages every day to different email addresses, but I always want to reply with the same address and the same signature. Apple’s Mail app makes it easy to see all messages sent to all accounts with the unified Inbox, but it makes it surprisingly hard to set default accounts and signatures that should always be treated as, well, default ones. I don’t want to click on menus for accounts and signatures: I want to hit ⌘R and receive a new Reply window with the account and signature I want already set.
I was inspired by this old post by Shawn Blanc (he solves a different problem than mine) to fire up Keyboard Maestro and start playing around with actions; thanks to Shawn’s post, I remembered that Keyboard Maestro’s keyboard shortcuts are intercepted by OS X before the system ones – which means, if I were to use the same ⌘R shortcut that Mail uses but I assigned it to my custom action, OS X would launch my action first (instead of the default Mail one). That seemed like a good starting point to build the macro I was looking for.
Thankfully, Apple hasn’t forgotten about AppleScript, and neither has Mail.app. It may be out of fashion among the cool kids (like email, I guess), but AppleScript allows us to start a reply to the selected message, set a signature, and a specific sender.
tell application "Mail" set theSignatureName to "Signature #1" set theMessages to the selected messages of the front message viewer set theMessage to first item of theMessages set theOutgoingMessage to reply theMessage with opening window set message signature of theOutgoingMessage to signature theSignatureName set sender of theOutgoingMessage to "Federico Viticci " end tell
It was easy enough to look up Mail’s AppleScript dictionary and put the pieces together after knowing where to look. Line 2 tells Mail to look for a signature called “Signature #1”: this can be a rich text signature, and you can get its name by opening Mail’s Preferences and looking for it in the Signatures tab.
Line 3 and 4 get the currently selected message, and specifically the first item of the selection, which should ensure you won’t get multiple reply windows to open for messages with CC/BCC addresses or threaded conversations. Line 5 starts a reply in a new window, line 6 sets the message signature to the one we fetched at the beginning of the script, and line 7 sets the sender.
As you may have already noticed, the sender’s name and email address are formatted using the same format Apple uses in Mail’s “From:” dropdown menu. In AppleScript, you just need to include the same format as a text string.
Combined with a simple macro that executes an AppleScript after a keyboard shortcut has been pressed, I can now hit ⌘R – as I’ve always done – to open a reply window that sets my sender account and email signatures to the ones I want – what I’ve always wished for.
For those times when I may have to reply from another email address, I have created a variation of the macro that presents a dropdown menu with email addresses to choose from, and that still uses AppleScript to set the signature and sender upon hitting a keyboard shortcut.
To create the dropdown menu, I used Keyboard Maestro’s “Prompt for User Input” and formatted the options as suggested by the Keyboard Maestro documentation – separated by a bar (|), with the option I want as selected by default repeated at the beginning of the line. In plain text, it looks like this:
Federico Viticci <firstname.lastname@example.org>|Federico Viticci <email@example.com>|Federico Viticci <firstname.lastname@example.org>|Federico Viticci <email@example.com>
tell application "Keyboard Maestro Engine" set KMVarAccount to get value of variable "Email Account" tell application "Mail" set theSignatureName to "Signature #1" set theMessages to the selected messages of the front message viewer set theMessage to first item of theMessages set theOutgoingMessage to reply theMessage with opening window set message signature of theOutgoingMessage to signature theSignatureName set sender of theOutgoingMessage to KMVarAccount end tell end tell
Once selected, the value is saved into a variable called “Email Account”, which is then fetched by the (tweaked) AppleScript on line 2 and used on line 9.
In this way, I have to perform an additional click to choose my email account, but I retain Keyboard Maestro’s AppleScript integration with Mail and OS X. Tip: to make sure the shortcuts won’t be triggered by other apps, create a macro group in Keyboard Maestro and make it available only for Mail.app.
I have been using this solution every day for the past two weeks, and I think it’s a good example of the kind of automation that’s (still) made possible by the nature of the Mac. You can download the macros here.