Mail links format

Are there any plans to change the Hook links for mail to make them behave like Drafts, Bear etc so the link format is : mail:// or even better message://

This would enable links between Apple Mail & Omnifocus to still work on iOS devices.

Thanks

1 Like

You can customize the URL scheme Hook uses with different applications by editing the Get Address script associated with an app in Preferences>Scripts

I modified this Get Address script to use the link format message://

Try it out and let me know if it does what you’re asking for.

tell application "Mail"
	
	set frontWinIsMessageViewer to false
	
	set fid to id of front window
	repeat with v in message viewers
		set wid to id of window of v
		if fid = wid then
			set frontWinIsMessageViewer to true
		end if
	end repeat
	
	if frontWinIsMessageViewer then
		try
			set theSelection to selection
			set theMessage to item 1 of theSelection
			set mid to message id of theMessage
			set mid to my encodeText(mid, true, false)
			return "message://%3C" & mid & "%3E"
		on error eStr number enum
			--display dialog eStr & " Number: " & enum buttons {"OK"} default button 1
			return
		end try
	end if
	
	try
		set theSelection to selection
		set theMessage to item 1 of theSelection
		set mid to message id of theMessage
		set subj to subject of theMessage
		set wname to name of front window
		
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {" ", ": "} -- to strip "re: ", "re:", "Re: ", "fwd:", etc
		repeat with i from 1 to count of text items of subj
			repeat with j from 1 to count of text items of wname
				if text item i of subj is equal to text item j of wname then
					repeat with k from j to (count of text items of subj) - i
						set window_title to text item k of wname
						set subj_name to text item (i + k - j) of subj
						if window_title is not equal to subj_name then
							set AppleScript's text item delimiters to astid
							return
						end if
					end repeat
					set AppleScript's text item delimiters to astid
					set mid to my encodeText(mid, true, false)
					return "message://%3C" & mid & "%3E" -- an old message
				end if
			end repeat
		end repeat
		set AppleScript's text item delimiters to astid
	on error eStr number enum
		--display dialog eStr & " Number: " & eNum buttons {"OK"} default button 1
	end try
	
	try
		set searchText to name of front window
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {" — "} -- to strip " — mailbox"
		set searchText to text item 1 of searchText
		set AppleScript's text item delimiters to astid
		
		set matchText to paragraphs of (do shell script "mdfind \"kMDItemSubject == " & quoted form of searchText & "\"")
		
		if matchText ≠ {} then
			set targetMatch to (item 1 of matchText) as rich text
			if targetMatch ≠ "false" then
				set lns to paragraphs of (read targetMatch)
				# Loop over lines read and copy each to the clipboard.
				repeat with ln in lns
					--display dialog ln
					if ln starts with "Message-ID:" then
						set x to length of "Message-ID:"
						set ln to characters (x + 1) thru -1 of ln as string
						repeat while ln begins with " "
							set ln to characters (2) thru -1 of ln as string
						end repeat
						repeat while ln begins with "<"
							set ln to characters 2 thru -1 of ln as string
						end repeat
						repeat while ln ends with ">"
							set ln to characters 1 thru -2 of ln as string
						end repeat
						set ln to my encodeText(ln, true, false)
						return "message://%3C" & ln & "%3E"
					end if
				end repeat
			end if
		end if
	on error eStr number enum
		--display dialog eStr & " Number: " & enum buttons {"OK"} default button 1
	end try
end tell
on encodeText(theText, encodeCommonSpecialCharacters, encodeExtendedSpecialCharacters)
	set theStandardCharacters to "abcdefghijklmnopqrstuvwxyz0123456789"
	set theCommonSpecialCharacterList to "$+!'/?;&@=#%><{}\"~`^\\|*"
	set theExtendedSpecialCharacterList to ".-_:"
	set theAcceptableCharacters to theStandardCharacters
	if encodeCommonSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theCommonSpecialCharacterList
	if encodeExtendedSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theExtendedSpecialCharacterList
	set theEncodedText to ""
	repeat with theCurrentCharacter in theText
		if theCurrentCharacter is in theAcceptableCharacters then
			set theEncodedText to (theEncodedText & theCurrentCharacter)
		else
			set theEncodedText to (theEncodedText & encodeCharacter(theCurrentCharacter)) as string
		end if
	end repeat
	return theEncodedText
end encodeText

on encodeCharacter(theCharacter)
	set theASCIINumber to (the ASCII number theCharacter)
	set theHexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
	set theFirstItem to item ((theASCIINumber div 16) + 1) of theHexList
	set theSecondItem to item ((theASCIINumber mod 16) + 1) of theHexList
	return ("%" & theFirstItem & theSecondItem) as string
end encodeCharacter
7 Likes

@grahamb

Thanks for doing this. I’d set aside time today to figure out this script on my own but when it comes to AppleScript, it is ALWAYS better when someone else has run the gauntlet for me already.

3 Likes

@MacSparky - Love the interwebs! Heard you mention this on Automators, came here did a simple search, and BAM… Thanks for mentioning this!

1 Like

Welcome the Hook Productivity Forum, @MacSparky and @95omega !

Glad you found it quickly, @95omega. Do you have a link to the Automators episode in question?

Yep!

@MacSparky touches on Hook towards the end of the episode on cloud automation.

Is this still the best way to use message:// links in Hook?

Why is this not the default behavior in Hook, as it is for Hook when linking to an item in DevonThink, Drafts, Things, and other apps?

I haven’t listened to that podcast episode yet.

Hook’s default integration with most supported mail apps is hook://email<ID>. This makes it possible to send URLs that resolve regardless of email client as long as the user has Hook Lite or better (Lite is free: can be used to resolve hook:// URLs, and create hook://email URLs), and a compatible email app (that uses RFC compliant email IDs). This means that you don’t need to make as strong an assumption about the email client of the recipient, and you yourself can change email apps dynamically (there’s a Favorite Apps tab in Hook’s preferences).

Hook integration with apps (including email apps) is configurable, so someone can use a different URL scheme than the default if they want. Often we publish alternative scripts for apps. on the Linkable Mac Apps – Hook page, many apps have links for further information. (We will create more of these pages in 2021 with our new URL patterns that you can see: https://hookproductivity.com/help/integration/using-hook-with-<App-Name>/).

1 Like

Thanks, LucB.

A partial answer to my own question: The script above seems to work very well. If there’s a better way to do this … well, I may not be interested, because the script above seems to work fine. :slight_smile:

1 Like

Thanks. We’ll add that to the web page on email integration (and linked from the particular "Using Hook with <email App Name> " web pages), so users can quickly get to this alternative script. Ultimately, Hook’s Scripts preferences pane should allow users to easily choose (with the UI, like a menu) the integration script they want (and sometimes: parameters)

1 Like

Follow up to this question - am I correct that using a traditional Hook link would make a message that was sent to two people open that message on their respective computers? If I’m correct in that, will switching tot his message: style take that away?

Teddy

As long as

  1. you copied the link with a fully compliant e-mail app
  2. they use such an email app (and Hook is configured to use it),
  3. they have access to the same message

Qualification #2 is because some apps use local message IDs that are not compliant with RFC-5322. Mail, AirMail and MailMate provide RFC compliant email ID automation.

If I’m correct in that, will switching tot his message: style take that away?

sorry, I don’t understand this question.

Thanks for the response! For the second question, I was wondering if editing the script for mail links as described in this thread (shifting from hook:// to message://) will interfere with the ability to open the same message across computers.

Sorry for the delay. That should work across Macs and on iPhone/iPad.
The limitation is that it only works in Mail.app. If you switch to a different email client, or want to send it to someone else, you’ll be out of luck. That’s why we invented the hook:///email URL scheme.

1 Like

I would also like to say thanks for doing this. Why is this not the Default Get Address script for Mail?

Because the hook: version is more robust than the message: version.

1 Like

Thanks! If I had looked closely the first time I would have seen that you have answered this very question before :upside_down_face:

1 Like

For users who prefer message:// style URLs, we published this in 2019: Mail links format - #2 by grahamb

I’ve updated the Favorite Apps Tab – Hook page to link to this.