Open a Forum Email in Safari

Hey Folks,

I don’t like having to find the visit-topic links in forum emails, so of course I wrote a script to extract the link and open it in Safari.

I have it bound to A in FastScripts, and it saves me time and aggravation every day.

Supported Forums:

Applescript Users List (Generic) - I’ve recently written a separate script to find ASUL messages.
Keyboard Maestro Discourse Forum
Keyboard Maestro Yahoo Forum
Mail Act-On { Indev }
Path Finder Beta
Script Debugger Discourse Forum
Script Debugger Yahoo Forum
Yahoo Groups (Generic)

This script long predates ASObjC and used to use the Satimage.osax for regex support. The 2013 creation date is when I rewrote it to use only stock OSX tools.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2013/04/02 14:00
# dMod: 2016/07/27 15:09
# Appl: Apple Mail & Default browser
# Task: Visit List-Serve Page Related to List-Mail-Message if Available.
# Libs: None
# Osax: None
# Tags: @Applescript, @Mail, @List, @Archive, @Header, @Browser
# Test: OSX 10.11.6
--------------------------------------------------------------------------------

set listArchiveUrl to {}
set urlToOpen to false
set yahooGroupsBaseURL to "http://groups.yahoo.com/neo/groups/"

tell application "Mail"
	set messageList to selection
	if length of messageList = 1 then
		set theMessage to item 1 of messageList
	else
		error "No messages OR too many messages are seleced!"
	end if
	
	tell theMessage
		
		set msgText to content
		
		set listID to content of (headers whose name is "List-Id")
		if listID ≠ {} then set listID to item 1 of listID
		
		set listArchiveUrl to content of (headers whose name is "List-Archive")
		if listArchiveUrl ≠ {} then
			set listArchiveUrl to item 1 of listArchiveUrl
		end if
		
		--» Applescript Users List
		if listID contains "applescript-users.lists.apple.com" then
			set urlToOpen to "http://lists.apple.com/archives/applescript-users"
		end if
		
		--» Keyboard Maestro Discourse Forum & Script Debugger Discourse Forum
		if listID contains "forum.keyboardmaestro.com" or listID contains "forum.latenightsw.com" then
			
			set shCMD to "<<< " & (quoted form of msgText) & ¬
				" sed -En '/^To respond, reply to this email or visit https:[^[:blank:]]+/{
						s!.+(https:[^[:blank:]]+).*!\\1!
						p
					}'"
			set urlToOpen to do shell script shCMD
			
			--» Discourse Forums
			if urlToOpen = "" then
				set msgSource to source
				
				set shCMD to "<<<" & (quoted form of msgSource) & " egrep -i -A1 '\\[Visit Topic\\]' \\
					| sed -E 's!=$!!; N;s/\\n//; s!^.*(https?:[^)]+)\\).*!\\1!'"
				set urlToOpen to do shell script shCMD
				
			end if
			
		end if
		
		--» Mail Act-On { Indev }
		if listID contains "support.indev.ca" then
			set shCMD to "<<< " & (quoted form of msgText) & ¬
				" sed -En '/^\\/\\/ Please reply above this line/,/^URL:.+/p' \\
					| sed -En 's!^URL: (.+)!\\1!p'"
			set urlToOpen to do shell script shCMD
		end if
		
		--» Path Finder Beta
		# URL: http://support.indev.ca/discussions
		if listID contains "Path Finder <support.cocoatech.com>" then
			set shCMD to "<<< " & (quoted form of msgText) & " sed -En '
						/View this discussion at our support site online:/ {n;p;}
					'"
			set urlToOpen to do shell script shCMD
		end if
		
		--» Yahoo Groups	
		if listID contains "yahoogroups.com" then
			set shCMD to "<<< " & (quoted form of listID) & " sed -E '
            s![<>]!!g
            s!\\.yahoogroups\\.com$!!
          '"
			
			set listID to do shell script shCMD
			set urlToOpen to yahooGroupsBaseURL & listID & "/info"
		end if
		
	end tell
end tell

if urlToOpen = false then
	
	if listArchiveUrl is not in {{}, ""} then
		set shCMD to "<<< " & (quoted form of listArchiveUrl) & " sed -E 's!^<|>$!!g'"
		set urlToOpen to do shell script shCMD
	end if
	
end if

if urlToOpen ≠ false then
	
	tell application "Safari"
		make new document with properties {URL:urlToOpen}
		set bounds of front window to {228, 22, 1542, 1196}
		activate
	end tell
	
end if

--------------------------------------------------------------------------------

Hey Folks,

The above script won’t compile, because of a bug in the Discourse forum software that causes certain escapes in code blocks to not render properly. (I reported the bug about a year ago, and they still haven’t fixed it.)

\\1

This should be backslash-backslash-one, but as you can see it’s been mangled.

Here’s a downloadable compiled script bundle.

Open → List-Archive Header in Safari.scptd.zip (12.9 KB)

-Chris