Copy the sender name of an Apple Mail message to the clipboard

Being a tidy Mac user, I accurately file all my incoming and outgoing eMails in Apple Mail into a special folder structure to avoid a cluttered inbox.

As you can see in the demonstrative screenshot below, I first separate messages into incoming and outgoing ones, then observe the date sent/received and finally archive them into a mailbox named after the sender’s name:

My personal Apple Mail filing structure

I really like this method and also apply it at work. But what really annoys me is, that you cannot easily copy and paste the sender’s name from an eMail message in Apple Mail when you want to create a new mailbox for this contact.

That’s because the sender’s name is not just plain text that can be selected, but rather a kind of an «address object» (see the image below) that is highlighted for further actions whenever you touch it with the cursor of your mouse. If you ever tried to copy the sender’s name from an eMail message in Apple Mail, you will instantly understand what I mean.

A sender name «address object» in Apple Mail

So I often ended up entering the name for a new mailbox manually. MANUALLY! That word hurts my feelings. And that is exactly why I once again sacrificed my lunch time to quickly write a small AppleScript (with a dash of Python flavour), that will simply copy the sender name of a selected eMail message in Apple Mail to the clipboard. You can download it right here:

Get Sender Name (for Apple Mail) (ca. 35.3 KB)

Main dialog of the Get Sender Name AppleScript

Just put it in the Script Menu and the tedious task of selecting the sender name is over. The script was successfully tested under Mac OS X 10.5.2 and Apple Mail 3.1. It works on Intel and PowerPC based Macs.

For enhanced convenience, the script can also switch the position of the first and last name of a contact (Martin Michel becomes Michel, Martin) for you and tries to extract the name and company parts in case that only an eMail address is found (harry.smith@examplemail.com becomes Harry Smith Examplemail).

Under the hood the script basically uses two very helpful (but little known) functions from Apple Mail’s scripting library, namely «extract name from» and «extract address from». These functions can get just the email address/name of a fully specified email address.

Important: Opening and saving the below script code in Script Editor won’t result in a usable AppleScript! That is because «Get Sender Name» internally relies on a little Python helper script, which is located inside its Application bundle. Therefor please download the complete script here.


-- created: 02.03.2008

property mytitle : "Get Sender Name"

on run
	my main()
end run

-- I am the main function, controlling the script
on main()
	try
		-- getting the selected eMail message
		set selmail to my getselmail()
		if selmail is missing value then
			return
		end if
		-- getting the sender name of the selected eMail message
		set sendername to my getsendername(selmail)
		if sendername contains "@" then
			set sendername to my procemailaddress(sendername)
		end if
		-- displaying the sender name to the user
		my dspsendername(sendername)
		-- catching unexpected errors
	on error errmsg number errnum
		-- ignoring 'User canceled'-error
		if errnum is not equal to -128 then
			my dsperrmsg(errmsg, errnum)
		end if
	end try
end main

-- I am returning the currently selected message in Apple Mail.
-- If no or more than one message is selceted, I am returning
-- missing value
on getselmail()
	tell application "Mail"
		set sel to (selection as list)
		-- empty selection
		if sel is {} then
			set errmsg to "You did not select a message in Apple Mail."
			my dsperrmsg(errmsg, "--")
			return missing value
			-- more than 1 message selected
		else if length of sel > 1 then
			set countselmails to length of sel
			set errmsg to "You selected " & countselmails & " messages in Apple Mail. Please select only one message before starting this script."
			my dsperrmsg(errmsg, "--")
			return missing value
		else
			set selmail to item 1 of sel
			return selmail
		end if
	end tell
end getselmail

-- I am returning the sender name of the given eMail message
on getsendername(emailmsg)
	tell application "Mail"
		set sendername to extract name from (sender of emailmsg)
		-- an email address only :(
		if sendername contains "@" then
			set sendername to extract address from (sender of emailmsg)
		end if
		return sendername
	end tell
end getsendername

-- I am displaying the extracted sender name to the user
on dspsendername(sendername)
	tell me
		activate
		display dialog "The following name was extracted from the sender address:" default answer sendername buttons {"Last name, First name", "Cancel", "Clipboard"} default button 3 with title mytitle
		set dlgresult to result
	end tell
	if button returned of dlgresult is "Clipboard" then
		set usrinput to text returned of dlgresult
		if usrinput is not "" then
			set the clipboard to usrinput
		end if
	else if button returned of dlgresult is "Last name, First name" then
		set usrinput to text returned of dlgresult
		if usrinput is "" then
			my dspsendername(sendername)
		else
			-- swapping first, last name
			set revsendername to (reverse of (characters 1 through -1 of sendername)) as Unicode text
			set charoffset to offset of space in revsendername
			if charoffset is 0 then
				my dspsendername(sendername)
			else
				set firstname to ((characters 1 through (-charoffset - 1) of sendername) as Unicode text)
				set lastname to ((characters (-charoffset + 1) through -1 of sendername) as Unicode text)
				set sendername to (lastname & ", " & firstname)
				my dspsendername(sendername)
			end if
		end if
	end if
end dspsendername

-- I am returning the name & domain part of an eMail address:
-- harry.smith@examplemail.com -> Harry Smith Examplemail
on procemailaddress(emailaddress)
	set pyscriptpath to POSIX path of (((path to me) as Unicode text) & "Contents:Resources:Scripts:helper.py")
	set command to "python" & space & quoted form of pyscriptpath & space & quoted form of emailaddress
	set command to command as «class utf8»
	set sendername to do shell script command
	return sendername
end procemailaddress

-- I am displaying error messages
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")" buttons {"Never mind"} default button 1 with title mytitle with icon stop
	end tell
end dsperrmsg