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:
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
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).
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
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.
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:
@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:
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
--------------------------------------------------------
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.
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?
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.