Unable to link email from Apple Mail

I am no doubt missing something very basic, but when I select an email in Apple Mail and invoke Hook, I get a message saying “No linkable item in Mail.” What might I be doing wrong?

Welcome to the forum, @pubay, and thanks for asking.

Hook can link previously received or sent email messages, but not drafts (not saved drafts either). That is because a draft email does not yet have a message ID, so it can’t be referenced. The ID is assigned by a Mail server when the message is sent (per https://tools.ietf.org/html/rfc2822#section-3.6.4 ) .

Does that solve your issue?

This is happening with previously received emails, not drafts. I realize that Hook is supposed to work with Mail, and so I am puzzled why I can’t get it to work, especially since it is working for me with Airmail.

Have you tried a restart? That has fixed similar Hook issues with other apps for me.

Yes, the problem persists after a restart.

Could you please select a message in Mail.app, and then try running the following script (taken from Hook’s Scripts for Mail) and send me the result (as a PIM if you prefer).

	
	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 "email://" & mid
		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 "email://" & mid -- 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 "email://" & ln
					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```

Forgive my obtuseness, but how do I run the script? When I try to run it from Script Debugger, I get an AppleScript Compile Error.

Sorry, I should have specified, using /Applications/Utilities/Script Editor.app . Create a new window in that app, paste the text from above there, and hit the “run” button at the bottom.

also, the last three tic characters, should not be included in the script. I.e., there should be no text after end encodeCharacter .

The Result pane, at the bottom of the Script Editor window, should display a message of the form "email:// … "

I again get a Syntax Error highlighting the word “viewers” in the line “repeat with v in message viewers”.

@LucB looks like your code is missing an opening ‘tell’ line.

Should there be an extra line at the top that says

tell application “Mail”

thanks for noticing that Steve. Something clearly went wrong with my copy paste, which explains the extra ticks too.
In short, the script is a copy of “get address” in Preferences > Scripts> Mail script

I still get a Syntax Error, this time with “ln” in the line “repeat with ln in lns”.

Sorry for the trouble, but please copy/paste the Mail “get address” script directly from Hook’s Scripts tab into Apple’s Script Editor; and then please run it again in Apple’s Script Editor.

If there’s a syntax error in that case, it means somehow your Mail script is corrupt, which would explain the issue.

If it does return an error, then instead of using the forum please email a plain text copy of that script directly to hook@cogsciapps.com . That will ensure that the Discourse’s software (used by this forum) doesn’t mangle your message.

Thank you very much for persisting with this.