Scripting in Mail - grab first name

I'd like a way to be able to paste a message which pulls the email sender's first name into my mail reply.

e.g. "Hey (firstname), blah blah"

Hey @eranbeard,

I have a fairly sophisticated AppleScript that does basically that (and a few other things) for Apple Mail.

Is that the app you're using?

And are you willing to install an AppleScript Extension (Satimage.osax) that adds regular expression support to AppleScript? (The most up-to-date version can be downloaded from this page.)

If so I'm willing to clean it up enough for you to use.


This macro is designed to be triggered from within the Mail application, when the message to which you wish to reply is selected. Rather than clicking on the Reply button within Mail, you double-tap the section sign (§) and a reply is created with auto-generated text content.

tell application "Keyboard Maestro Engine" to set input to ¬
	the value of the variable named "Input"

tell application "Mail"
	set [M] to (the selection as list)
	set n to extract name from M's sender
	if the number of n's words > 1 then
		if n contains "," then -- "Surname, Firstname"
			set n to n's second word
		else -- "Firstname Surname"
			set n to n's first word
		end if
	end if
	reply M
end tell

-- Substitute the {{name}} placeholder for the sender's name
set the text item delimiters to {n, "{{name}}"}
set input to text items of the input as text

Mail Snippet.kmmacros (3.1 KB)

This sort of thing is great for creating a canned response, but it does not work well for replying to a message and getting properly formatted and quoted reply-text.

Unfortunately there's no good way to automatically compose a quoted reply message with added custom content via AppleScript. (Bad Apple!)

To compose a proper reply message with quoted content and then add custom content it's necessary to first reply to the message and let Mail do it's thing and then add the custom content.

Mail won't let you change the content of an already composed outgoing message, so it's necessary to position the cursor and paste.

It's kludgey and complicated, but it can be done.


@ccstone, it would appear that you are quite correct, which is very disappointing.

@eranbeard, I've now updated the macro and the attached AppleScript with a workaround solution that appears to work. Please feel free to test it and see if it does what you want.