This week's sponsor

Turn Touch

Beautiful Control


Automating Mail Signatures and Senders with AppleScript and Keyboard Maestro

Mail Keyboard Maestro

Mail Keyboard Maestro

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.[1] 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.

Mail Keyboard Maestro

Mail Keyboard Maestro

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.

Mail Keyboard Maestro

Mail Keyboard Maestro

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.

Mail Keyboard Maestro

Mail Keyboard Maestro

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 <viticci@macstories.net>|Federico Viticci <me@ticci.org>|Federico Viticci <viticci@macstories.net>|Federico Viticci <info@macstories.net>

Mail Keyboard Maestro

Mail Keyboard Maestro

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.

Mail Keyboard Maestro

Mail Keyboard Maestro

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.


  1. Apple’s forums, Mac OS X Hints, GitHub, and, of course, Macscripter. ↩︎

Unlock MacStories Extras

Club MacStories offers exclusive access to extra MacStories content, delivered every week; it's also a way to support us directly.

Club MacStories will help you discover the best apps for your devices and get the most out of your iPhone, iPad, and Mac. Plus, it's made in Italy.

Starting at $5/month, with an annual option available. Join the Club.

A Club MacStories membership includes:

  • MacStories Weekly newsletter, delivered every week on Friday with app collections, tips, iOS workflows, and more;
  • Monthly Log newsletter, delivered once every month with behind-the-scenes stories, app notes, personal journals, and more;
  • Access to occasional giveaways, discounts, and free downloads.