Extract domain name from current email and open in Safari

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