Hi. I am often reading emails where I would like to visit the website of the sender’s email address. I’ve tried writing a macro myself, and looking up similar examples, but haven’t had much luck. Here is what I want to do.
While reading the current email:
extract the domain name from the “From:” email address
Given the email address, extracting the domain name and opening the URL is fairly easy. But I don’t know how to get the email address from the From line in any kind of automated way. You could do this sequence, though it is quite a hack:
Command-R, 3 x Shift-Tab, Command-C, Command-W
And then given the email address in the clipboard
Search and Replace clipboard with regex .@([a-z.]+). with $1
Thanks Peter and Chris for your help. I forgot about this for awhile but have now figured out how to do it. I’m not a programmer, so there may be a more elegant way, but for now this works.
Execute AppleScript
tell application "Mail"
set theMessages to selection
set theEmail to extract address from sender of item 1 of theMessages
set the clipboard to theEmail
end tell
Search and Replace Clipboard Using Regular Expression
Personally I tend avoid the clipboard, unless I’m going to paste from it.
Here’s how to do the whole job with pure vanilla AppleScript:
--------------------------------------------------------------------------------
tell application "Mail"
set theMessages to selection
set theEmail to extract address from sender of item 1 of theMessages
end tell
set AppleScript's text item delimiters to "@"
set theDomain to text item 2 of theEmail
open location "http://" & theDomain
--------------------------------------------------------------------------------
Here’s how to drop a value directly into a Keyboard Maestro variable:
--------------------------------------------------------------------------------
tell application "Mail"
set theMessages to selection
set theEmail to extract address from sender of item 1 of theMessages
end tell
tell application "Keyboard Maestro Engine"
setvariable "theDomain" to theEmail
end tell
--------------------------------------------------------------------------------
From there you’d recreate the action in your macro, but you’d use the variable instead of the clipboard.
I don’t like to use the clipboard (unless necessary) for two reasons:
I don’t want to disturb the contents of the clipboard.
Using the clipboard is a trifle slower than using a variable.
Finally – here’s a solution that uses AppleScriptObjC to add regular expression support to the AppleScript:
--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/05/08 13:10
# dMod: 2016/12/04 13:23
# Appl: Apple Mail & Default Web Browser
# Task: Get the domain of sender of selected message in Mail and open it in your browser.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Open, @Domain, @Selected, @Message, @Mail, @Browser
--------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
--------------------------------------------------------------------------------
tell application "Mail"
set theMessages to selection
set theEmail to extract address from sender of item 1 of theMessages
end tell
set theDomain to "http://" & (its cngStr:"^.+@(.+)" intoStr:"$1" inStr:theEmail)
open location theDomain
--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on cngStr:findStr intoStr:replaceStr inStr:dataStr
set anNSString to current application's NSString's stringWithString:dataStr
set dataStr to (anNSString's ¬
stringByReplacingOccurrencesOfString:findStr withString:replaceStr ¬
options:(current application's NSRegularExpressionSearch) range:{0, length of dataStr}) as text
end cngStr:intoStr:inStr:
--------------------------------------------------------------------------------
Thanks Chris. That’s much more elegant and simple.
I’m a writer and I receive numerous pitches from PR companies and products. This allows me to quickly look up some background info to see if I’m interested in replying. It’s already saved me a ton of time in the first hour.
Alain, as you know I like and use AppleScript often.
But if RegEx is needed in a script, I much prefer JXA.
The core JavaScript engine comes with a powerful, easy-to-use, RegEx facility.
To be honest, I find most of the ASObjC code to be too verbose for my liking.
The JavaScript RegEx is very concise.