Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

automate clicking on hyperlink in mail Apple script

Problem Description:

I receive emails with a hyperlink embedded in a specific word. I need an AppleScript that automatically clicks this link and moves the email to a specific mailbox.

Requirements:

  • The script should run in the macOS Mail app.
  • It should search for the specific word in the email content and extract the link behind it.
  • Upon finding the link, it should click on it and move the email to a specified mailbox.

Current Approach:

  • I've been able to find the word in specific mails but I can't extract the hyperlink behind it. there will be several hyperlinks in the mail so it needs to be on the specific word.


I need the script to run automatically for every new email I receive.


MacBook Pro 13″, macOS 12.6

Posted on Apr 25, 2024 5:50 AM

Reply
2 replies

Apr 26, 2024 6:23 AM in response to GirafCow

Rather than let this go unanswered, here is some insight.


The Apple Mail scripting dictionary does not give you access to the URL behind the hyperlinked text in the mail body. You can get the entire raw source of the mail body which you would have to then parse to get the URL associated with the anchored text. There is no way to get Mail to click that URL in the email body either.


The scripting dictionary does support a move reserved word for transferring a message to another mail folder.


Suppose that I have this mail body view as a screen shot:



The actual HTML behind this view is:


<html><head><meta http-equiv="content-type" content="text/html; charset=us-ascii"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Some text may have this&nbsp;<a href="https://www.apple.com">hyperlink-1</a>&nbsp;or other text may have&nbsp;<a href="https://discussions.apple.com/thread/255591449?sortBy=oldest_first">hyperlink-2</a>&nbsp;on it.</body></html>


The following AppleScript will work on the selected email message, extract the above HTML from the message raw source, extract the URL associated with "hyperlink-1" from the HTML, and then open the URL as a new tab in Safari.


use framework "Foundation"
use scripting additions

property ca : current application

tell application "Mail"
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
		tell theMsg to set theHTML to my justHTML(its source) as rich text
		# tell theMsg to set theHTML to its source
	else
		display alert "No Mail message selected" giving up after 10
		return
	end if
end tell

tell application "Safari"
	activate
	open location my extractURL(theHTML, "hyperlink-1")
end tell
return

on justHTML(asrc)
	-- get just the HTML content from the raw message source without using perl, etc.
	set regex to ca's NSString's stringWithString:"(<html.*html>)"
	set srcStr to ca's NSString's stringWithString:asrc
	set match to srcStr's rangeOfString:regex options:(ca's NSRegularExpressionSearch)
	return (srcStr's substringWithRange:match) as text
end justHTML

on extractURL(ahtml, akey)
    -- extract the URL from <a href="https://www.apple.com">hyperlink-1</a>
    -- using Positive Lookbehind and Lookahead regular expression pairing for URL capture
    set regex to ca's NSString's stringWithString:("(?<=\\<a href=\")(.*)(?=\">" & akey & "<\\/a>)")
	set shtml to ca's NSString's stringWithString:ahtml
	set match to shtml's rangeOfString:regex options:(ca's NSRegularExpressionSearch)
	return (shtml's substringWithRange:match) as text
end extractURL


Whether you can adapt this to a Mail rule or not is to be determined.


Tested: M2 Mac mini Pro w/Sonoma 14.4.1


Apr 27, 2024 7:02 AM in response to VikingOSX

Or further simplified:


use framework "Foundation"
use scripting additions

property ca : current application

set anchor_word to "hyperlink-1"
set extractHTML to "(<html.*html>)"
-- AppleScript requires escaping backslash and embedded double quotes
set extractURL to "(?<=\\<a href=\")(.*)(?=\">" & anchor_word & "<\\/a>)"

tell application "Mail"
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
		tell theMsg to set theHTML to my extract_text(its source, extractHTML) as rich text
	else
		display alert "No Mail message selected" giving up after 10
		return
	end if
end tell

tell application "Safari"
	activate
	open location my extract_text(theHTML, extractURL)
end tell
return

on extract_text(atxt, aregex)
	-- return the result of applying the aregex on the passed atxt content.
	set regex to ca's NSString's stringWithString:aregex
	set srcStr to ca's NSString's stringWithString:atxt
	set match to srcStr's rangeOfString:regex options:(ca's NSRegularExpressionSearch)
	return (srcStr's substringWithRange:match) as text
end extract_text


automate clicking on hyperlink in mail Apple script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.