Find, Copy and Paste Text From Apple Mail App Internal Headers

I just tested that string with the below RegEx, which is part of my Maco, and it found the email. Test it for yourself. Open the below link and paste this string at the bottom.

Pauses have nothing to do with Regex.

The current Regex:
auth-sender:\h+(.+)\h*\R*

will match "auth-sender:" and then ONE or more spaces or tabs.

Change it to this to allow ZERO or more:
auth-sender:\h*(.+)\h*\R*

In the future when you post source text, please use the Forum Code Block with a language of "text".

1 Like

@JMichaelTX, I just wanted to thank you again. I use this macro at lease 20 to 30 times during the workday. Thank you, it significantly improved and sped up my workflow!

1 Like

Is there a way to move the mouse to a specific location on a secondary screen? I'm trying to add to this macro.

See Move or Click Mouse action and use the SCREEN function to get the position on the target screen.

I'm sorry I must be missing something, I don't see an option for an external monitor in the move or click action. Please show me again?

Screen Shot 2020-08-03 at 2.04.16 PM

Choose "absolute position", and enter something like this:
SCREEN(2,MidX) and SCREEN(2,MidY)
where "2" is the target screen#.

If you only have one external monitor, you can use "External" instead of "2".

See SCREEN function for other choices.

1 Like

Thank you so much!

1 Like

I'm having trouble with the screen again because I accidentally edited the working macro.

My external screen is to the left of the main screen. Is there a way I can enter an x and y coordinate once on the external screen?

Hi @gglick

Looking to modify this to get the sender's address from an email open in Apple Mail on the clipboard that will be used by Hazel to create a new rule.

This works up to the last step as when it had a display text action as the last step it displayed the sender's address.

I also tried
image

and set system clipboard to %Variable%Local__EMail% which doesn't work either. I get "COULD NOT FIND" which I see is the original variable being set.

I also see in the Insert Token list in the Mail section one called Mail Sender. Might I use that to more simply extract the sender's address?

I'm guessing the Regex action is setting the text search result of the %MailRawSource% variable to a variable called Local_EMail. Is that accurate?

I looked in the actions in the wiki and don't have the distinctions to understand and use what's there.

Thanks!

Sender to Clipboard.kmmacros (12 KB)

Hey @BernSh,

Yes, fortunately, you don't need regex to get the sender's address in Mail. As you discovered, Keyboard Maestro does already have a token for that, along with numerous other properties besides the mail's raw source:

So yes, if you just want to get the sender's address onto the clipboard, this macro could be drastically simplified like so:

Thanks for quick reply. Exciting idea and this gives a blank clipboard. See something off or maybe restart KM or the Mac? Running 10.15.7 and KM 9.1

Keyboard Maestro Actions.kmactions (8.9 KB)

No problem. Here's one troubleshooting idea: open KM's Value Inspector window (6 while in the editor), put the Mail Sender token there, and see what it says while selecting and opening different messages in Mail:

If nothing shows up there, make sure that KM has permission to automate Mail in System Preferences > Security & Privacy > Privacy > Automation:

Wow, that inspector is cool! Thanks!

I think I found the issue. It works when I either select an email from the list of emails IN Mail or select an email opened from WITHIN the Mail app. It fails with emails that I've dragged out of Mail into a folder and then open from within that folder even though Mail is opening the email.

Ideas?

Sorry, I don't use Mail much to begin with, and I've never used that feature of it, so I'm afraid I can't be much help there.

Thanks for looking at this! @gglick

Hey @peternlewis

The %MailSender% Token works from within the Mail app but not with Mail dragged from Mail app into the Finder and then opened within the Finder. Is this expected?

What a shock!

Apple is so smart that they didn't give AppleScript any means to detect when the front window in Mail.app is in fact a mail message and not a mail-viewer window.

Eudora could do that simple task 25 years ago...

The only way AppleScript can see a message is IF-AND-ONLY-IF it is selected in a mail-viewer-window.

So you're completely S.O.L. when trying to apply AppleScript to a mail message file that has been opened from the Finder.

You'll have to read the message file and scrape its headers yourself.

BTW – when doing something esoteric like this it is ABSOLUTELY ESSENTIAL that you mention it when asking for help on the forum. You'll save yourself and everyone else a great deal of time.

The macro below will do the job, although I haven't spent the time to make it completely bombproof.

Select a .eml file in the Finder and run the macro – the sender's email address will be placed in the Clipboard.

-Chris


Scrape Sender from Selected .eml File in theFinder v1.00.kmmacros (8.8 KB)

1 Like

Chris, I’m delighted that you put this together. Totally generous. I thank you!

I also enjoyed your take on Apple’s thoughtfulness or lack there of :smirk: tho I confess I had to smile at the credit you grant that I could distinguish a simple request from an esoteric one. While I’d guess organizing mail with the Finder rather than within Mail is unusual I wouldn’t think esoteric. Ah, to be responsible for being a strange :bird:.

I’ll test this tomorrow and follow up. Thanks again :pray:

1 Like

Ends with a Display Text window of correctly scraped text and an empty clipboard.

Some emails throw error codes. Would it be useful to send those to you or is this too much?

Thanks!

Whups, I forgot to change that bit...  :sunglasses:

Yes, it would be useful – please send them to me at this address: listmeister@thestoneforge.com

I'll make the script a bit more robust.

-Chris