How to Get the "First Name" of the Sender from Mail and/or Spark?

Hi,

I use both Mail and Spark.
I'm wondering if there is a convenient way to get the first name of the sender?
What I would like to do is: when I reply someone's email, the reply email will begin with:

Hi [First Name],
[Cursor goes here]

Thanks!

This AppleScript shows how to get the sender's name and email address from the selected message:

tell application "Mail"
	set selectedMessages to selection
	if selectedMessages = {} then return
	
	repeat with aMessage in selectedMessages
		set senderName to extract name from sender of aMessage
		set senderAddress to extract address from sender of aMessage
		display dialog "    name: " & senderName & return & "address: " & senderAddress
	end repeat
end tell

I leave it to you to get the sender's first name!

1 Like

Hi @tiffle,

That's very helpful. Thanks a lot!

Just one question: Your script is able to handle multiple selections as well as single selection, which is great.
For now, in my usage, I mostly need only the sender name of one email. So I tried to simplify the script as follows:

tell application "Mail"
	set selectedMessage to selection
	if selectedMessage = {} then return
	set senderName to extract name from sender of selectedMessage
	--set senderAddress to extract address from sender of selectedMessage
	return senderName
end tell

But I'm getting an error:

2021-09-30 11:58:25 Execute an AppleScript failed with script error: text-script:114:157: execution error: Can’t get sender of {message id 69726 of mailbox "[Gmail]/All Mail" of account id "920C6AFF-2297-4453-BF05-F5D90270A2CD" of application "Mail"}. (-1728). Macro “Mail” cancelled (while executing Execute AppleScript).

What is causing the error in this code?

Try this:

tell application "Mail"
	set selectedMessages to selection
	if selectedMessages = {} then return
	
	set senderName to extract name from sender of item 1 in selectedMessages
	return senderName
end tell

selection is a list so you must treat it as such.

1 Like

Ah! That's why!
Learned something today. Thanks!

Uploaded the finished macro to the macro library.

Thanks again, @tiffle, for your help!

1 Like

Hey Guys,

Keyboard Maestro has a token for this:

image

Hey @martin,

I've been doing this for about 25 years – starting with Eudora and continuing with Mail.

I've found over time that auto-insertion of a salutation on REPLY causes me extra work too often.

My solution is a Hotkey driven macro that I use in addition to Reply. This macro has a rules file for specialized greetings for various email addresses, and it has rules for handling things generically.

This way I use it on demand, and it only costs me one extra keystroke.

I also have another macro called Reply-With-Rules that does various things when Replying in various contexts.

-Chris

3 Likes

Thanks, Chris. This is even better! How come I did not know that? :joy:

This is a good solution. Thanks for sharing! I will consider this approach if I end up editing the auto inserted string too often.

Great catch @ccstone!!

And to reduce this down to the sender's first name using just Keyboard Maestro Actions, the following works for me:

EDIT - I should explain that the first entry in the Search "for" field is a blank space (one hit of the spacebar) and it replaces the space with a comma.

This works because the Keyboard Maestro Token %MailSender% returns a value something like this:

image

2 Likes

Hey Martin,

Please take the time to complain to Readdle about Spark's abysmal AppleScript support.

They didn't even bother to install the default AppleScript suite.

If they're going to take the trouble to make Spark AppleScriptable they can surely do better than this:

image

You can add my name to your complaint/petition if you like.

-Chris

1 Like

I don't even see another product of theirs ==PDF Expert== listed in the AppleScript library. :joy:

I've sent a feature request as feedback to them.

1 Like

@ccstone Hello! Where would I put this token? (sorry for the text, I am not at my computer at the moment)

My macro is set up as such:

Mail triggers KM Macro:

image

The goal is to set email senders name to a local variable I can use later in the macro.
I also tried adding to my mail AppleScript set senderName to local_sender.
Did not work.

This is my AppleScript that triggers the macro (made by you and @Nige_S)

--------------------------------------------------------

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
      tell application "Mail"
         repeat with eachMessage in theMessages
            my doKeyboardMaestroMacro(eachMessage's content)
         end repeat
      end tell
   end perform mail action with messages
end using terms from

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on doKeyboardMaestroMacro(msgContent)
   tell application "Keyboard Maestro Engine"
      do script "5E50DFA0-06E7-4D56-8B86-ACC8D67CFC20" with parameter msgContent
   end tell
end doKeyboardMaestroMacro
--------------------------------------------------------

Hey Mike,

This is not going to work. The %MailSender% token only works with the frontmost message in Apple Mail.

Nor can you set local variables from an externally run AppleScript (in a rule) – you'll have to set a global, use it appropriately, and then do proper housekeeping to prevent old data from creeping into your working macro.

Here's an untested example:

--------------------------------------------------------

using terms from application "Mail"
   on perform mail action with messages messageList for rule theRule
      tell application "Mail"
         repeat with theMessage in messageList
            my doKeyboardMaestroMacro(theMessage's content)
            my setKMVar("Mail_Rule_Sender", theMessage's sender) -- Set global variable to Sender.
         end repeat
      end tell
   end perform mail action with messages
end using terms from

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on doKeyboardMaestroMacro(msgContent)
   tell application "Keyboard Maestro Engine"
      do script "5E50DFA0-06E7-4D56-8B86-ACC8D67CFC20" with parameter msgContent
   end tell
end doKeyboardMaestroMacro
--------------------------------------------------------
on getKMVar(kmVarName) -- Global Variables Only
   tell application "Keyboard Maestro Engine"
      return getvariable kmVarName
   end tell
end getKMVar
--------------------------------------------------------
on setKMVar(kmVarName, kmVarValue) -- Global Variables Only
   tell application "Keyboard Maestro Engine"
      setvariable kmVarName to kmVarValue
   end tell
end setKMVar
--------------------------------------------------------

I think I've got the get and set variable handlers correct, but I'm not going to take time to test them right now.

It's a good idea to use handlers when you're calling functions from another app in an existing app's tell-block.

-Chris

1 Like

Thank you, Sir. Giving it a shot now.

Ok, don't be mad. I searched and searched and there is a ton of answers, confusing me. The house keeping part... How do I get it to clear after each macro? Like a local_var?

No worries.

You can use set-variable actions to set each global variable to nothing at the end of your macro, or you can use an Execute-AppleScript action to do this en mass.

set varNameList to items 1 thru -2 of {¬
   "varOne", ¬
   "varTwo", ¬
   ""}

tell application "Keyboard Maestro Engine"
   repeat with theVariable in varNameList
      setvariable theVariable to ""
   end repeat
end tell

I generally use the AppleScript method myself – IF I'm dealing with more than one variable – and I have a favorite action set up to make this easy.

1 Like

Can this be adapted for use with Outlook for Mac?