Extract domain name from current email and open in Safari

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
  • open that website in Safari

Any help would be appreciated.

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

And then Open URL http://%CurrentClipboard%

Hey Dave,

What application are you using to read your email?

-Chris

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

^(\S+)@(\S+)

And replace with:

$2

Open URL http://%CurrentClipboard%

2 Likes

Hey Dave,

What you’ve done is quite good. Well done.

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:

  1. 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:
--------------------------------------------------------------------------------

-Chris

2 Likes

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.

Thanks Chris too for the power of regex in AppleScript using AppleScriptObjC :+1:
-Alain

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.

Of course @JMichaelTX, you are true.
But at this time I don’t have enough time to (re-)learn JavaScript/JXA…
Cheers,
-Alain

Learning ASObjC is even harder, at least for me. :wink: