How to modify AppleScript to generate clickable email subject line link instead of a long cryptic link

Hello,

MacSparky published a script to link to currently selected email in apple mail, even if mail is in the background (script below)

The script works but the result is rather cryptic, giving no clue as to the subject of the email.

example:

message:%3C4Q80.A9LF.943CHBAW78.20200125123837534@emails.pocketmags.com%3E

is there any way to modify the script to yield the subject line as a clickable link.

example: [Grab a bestseller for just 99p] (the subject) which if clicked on would go to the email link message:%3C4Q80.A9LF.943CHBAW78.20200125123837534@emails.pocketmags.com%3E

thanks in advance for your time and help !


(*

Returns a link to the first selected Apple Mail message

*)

tell application "Mail"

set _msgs to selected messages of message viewer 0

if (_msgs is not equal to missing value) then

set _msg to first item of _msgs

set _msgID to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & (message id of _msg)

return "message://%3C" & (_msgID) & "%3E"

end if

end tell

I don’t know if any escaping is necessary for the subject. I’ve modified what is returned from the script to be in markdown format for a link.

Grab a bestseller

tell application "Mail"
	
	set _msgs to selected messages of message viewer 0
	
	if (_msgs is not equal to missing value) then
		
		set _msg to first item of _msgs
		
		set _msgID to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & (message id of _msg)

		set _msgSubject to subject of _msg
		
		return "[" & _msgSubject & "](message://%3C" & (_msgID) & "%3E)"
		
	end if
	
end tell

So I guess it depends where you’d be using this link whether the result would be clickable.

1 Like

thank you very much for your script
It works in Script Editor but not in Evernote (or Bear) where I would be using it.
thanks again very much for your time and effort.
I searched the Internet and am surprised that there is not more interest or demand for such a script which seems rather essential to me.

What do you mean by it doesn’t work in Evernote or Bear?

1 Like

Does not work means that nothing happens in evernote and bear.

In the meantime, I found the perfect script in the evernote forum:

thanks again for your work and time

Can I ask how you were triggering the script and what you were doing with the result?

The script above was just returning the Markdown formatted link. not putting it on the clipboard or outputting it for use from what I can see.

The Evernote scripts you linked to were using HTML based link formatting (rather than the Markdown links above). Bear should support both HTML and Markdown I think for links.

My current suspicion is that you didn’t have anything to output the link. Just to build a Markdown link,

1 Like

you have an excellent point and I am at fault.
I assumed that the output would be in the clipboard.
I had no idea that a script could create a link … and that’s it.
How should I copy it to the clipboard <’
thanks very much for your comment.

In AppleScript, you can set the clipboard like this.

set the clipboard to myLink

Where myLink is whatever you want the clipboard set to. In your case, you want to set the clipboard to the return.

This set the clipboard through AppleScript technique is actually the technique used in at least one of the Evernote scripts you linked to above.

2 Likes

I understand now and it works. Thanks very much and my apologies to @dustinknopoff

2 Likes