Automators 2: Email Automation

I implemented David’s applescript to get the name in Mail in Keyboard Maestro since I don’t have TextExpander, but I modified it slightly. Email addresses in my company are all of the form “Lastname, Firstname (Location) <firstname.lastname@company.com>” so I added a check for the name formatting. The script will return the first name for regular email addresses and company email addresses.

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 then
					if (offset of "," in theToRecipient) is greater than 0 then
						set theName to word 2 of theToRecipient
					else
						set theName to word 1 of theToRecipient
					end if
					set theGreeting to theName & ","
					return theGreeting
				end if
			end if
		end tell
	end tell
end tell
2 Likes