Years ago I published an AppleScript that allows you to automatically insert a recipient’s name to an email using TextExpander on your Mac. This is a cool trick because when you use it, you never get a person’s name wrong when replying to them.
Reader Greg (Twitter) wrote to me about the script a while back. One of the flaws with my script was that if the email was written in a way that listed the recipients last name first (e.g., Sparks, David), the script didn’t work correctly. As I wrote it, the script required the person’s first name to be listed first.
Greg broke out his AppleScript editor and made a few changes. The new version is below:
tell application "System Events" tell process "Mail" tell text field "To:" of window 1 if UI element 1 exists then set theToRecipient to (value of UI element 1) if ((count words of theToRecipient) is greater than 0) and (theToRecipient does not contain ",") then return word 1 of theToRecipient else if ((count words of theToRecipient) is greater than 0) and (theToRecipient contains ",") then return word 2 of theToRecipient end if end if end tell end tell end tell
The new version looks at the number of words and looks for a comma. If it finds a comma, it uses the second word as the recipient’s first name. In hindsight, this little addition is obvious. Nevertheless, Greg was kind enough to share this with the rest of the Internet and me through this post. You can download the script file right here.
To implement this, create a TextExpander snippet that runs as an AppleScript and includes this script.
You can see my shortcut phrase is “xmm” (x name in my head) but I never trigger it with that phrase. Instead I embed it in other snippets with different salutations.For instance, to start it with “Hello” and a comma after the name, you would make it look like this.
If you want to have a less formal one starting with just “Hi”, it would look like this.
I’ve run the whole board with versions for “Hey” and “Dear” as well. You can download the TextExpander snippet group, including the script, with this link.
If you have TextExpander and use Apple Mail, I recommend trying this out. I use these scripts with nearly every email I write on my Mac. I love that it got just a little bit smarter with Greg’s tweak. Take the time to set this up. You will not regret it. Also, if you’re on Twitter, tell Greg “thanks”.