Grab E-Mail Address from The Current macOS Mail Message

Hi,

I’m wondering if there’s a way to achieve the following:

In macOS Mail when I click into a received email, I’d like to be able to quickly grab the senders email address with a shortcut and get it to the clipboard.

I don’t really seem to find a reliable way to do this with a macro.

Any thing I’m missing?

A possible way to get the reply-to address is the sequence:

⌘R, ⇧Tab, ⇧Tab, ⇧Tab, ⌘C, ⌘W

The Reply To address is not always the same as the From address though.

Yep...

Grab Sender Address From Current macOS Mail Message v1.00.kmmacros (6.3 KB)
Keyboard Maestro Export

1 Like

Very nice, it's getting me closer, but I still don't get the core email address, but something like this:

Christopher Stone kmforum@forum.keyboardmaestro.com

What I need is to get only this: kmforum@forum.keyboardmaestro.com

The good thing is that the complete email with the name info is always in the same format. The bit you need is always after a < and before a > so, it is just a matter of extracting that bit of text.

I see that @ccstone has already provided a Regex solution to this exact thing here

And if you want to do this without Regex it can be done like this, by splitting the KM Variable into parts:

Click to Show Image of Macro

Grab Sender email Address - method using KM split variable.kmmacros (19.6 KB)

2 Likes

Oh the joy of getting automations to work! Thank you! I need to see if I can find an online course or something to get into understand how this works better. Let me know if you can recommend any resources.

2 Likes

I would say just keep trying things out (that are real tasks) read the Wiki for things like this and ask questions here.

Best way is to make Macros for things you actually need to do. That’s the quickest way to learn.

And rather than searching the Forum for previous similar questions - I find a general Google Search is better “Keyboard Maestro Your Question” - that will usually find the topic on this Forum quicker than searching in the Forum.

3 Likes

The most comprehensive one I know of is:

1 Like

Well done.  :sunglasses:

You can also get Mail to do all the work for you.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/11/08 21:06
# dMod: 2022/11/08 21:06 
# Appl: Mail
# Task: Extract Email Address from the Sender String of the Selected Message.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Extract, @Email, @Address, @Sender, @String
--------------------------------------------------------

tell application "Mail"
   set selectedMessageList to selection
   if selectedMessageList ≠ {} then
      set selectedMessage to item 1 of selectedMessageList
      tell selectedMessage
         set senderAddress to extract address from (get its sender)
      end tell
   end if
end tell

--------------------------------------------------------
1 Like

Thanks for this too.

If I use this solution, how do I then get the email address to show – so I can use it as a search criteria later on.

Where is it saved?

The AppleScript variable senderAddress is assigned the value of email's sender's address. That value is not saved by the script which is only an example of how to acquire the value.

There are two main ways to make the value available to Keyboard Maestro.

1) You can assign a Keyboard Maestro variable within the AppleScript itself:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/11/08 21:06
# dMod: 2022/11/16 04:28
# Appl: Mail
# Task: Extract Email Address from the Sender String of the Selected Message.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Extract, @Email, @Address, @Sender, @String
--------------------------------------------------------

tell application "Mail"
   set selectedMessageList to selection
   if selectedMessageList ≠ {} then
      set selectedMessage to item 1 of selectedMessageList
      tell selectedMessage
         set senderAddress to extract address from (get its sender)
      end tell
   end if
end tell

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

# NOTE – ALL Keyboard Maestro Variables are STRINGS.

set kmInstance to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
   
   # Example of setting an AppleScript variable from a KM variable (commented-out)
   # set asVarName to getvariable "local_copiedText" instance kmInstance
   
   # Set a KM variable from an AppleScript variable or value.
   setvariable "local_KmVarName" instance kmInstance to dataStr
   
end tell

# NOTE - The KM variable is a quoted string.

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

2) You can capture the output of an AppleScript using the Execute an AppleScript action settings.

Extract Sender Address v1.00.kmmacros (2.7 KB)
image

2 Likes

Amazing, thanks! :slight_smile:

1 Like