Select an Already opened email message via AppleScript

How do I go about selecting an already opened email via AppleScript to create a link instead of using the selected email listed in the message viewer as outlined in the below AppleScript?

(See image for clarification, I’m not sure why I cannot get the image to display in the post but if you right click and choose open you can view it…)


(*
  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

Browser: Safari 605.1.15
Operating System: macOS 10.14

No need python. Try this (when no message clicked manually):


tell application "Mail"
	set visibleViewers to message viewers whose visible is true -- opened viewers
	set theViewer to first item of visibleViewers
	set theMessage to first item of (get selected messages of theViewer) -- opened message
	set messageID to message id of theMessage
	-- Make URL (must use URL-encoded values for "<" and ">")
	set urlText to "message://" & "%3С" & messageID & "%3Е"
end tell

Or, try this (when some message is clicked manually):


tell application "Mail"
	set theMessage to item 1 of (get selection)
	set messageID to message id of theMessage
	-- Make URL (must use URL-encoded values for "<" and ">")
	set urlText to "message://" & "%3С" & messageID & "%3Е"
end tell

No, but you can’t just drop it – you need to percent-encode the id somehow. You can do it all in AppleScript by changing your first script like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Mail"
	set visibleViewers to message viewers whose visible is true -- opened viewers
	set theViewer to first item of visibleViewers
	set theMessage to first item of (get selected messages of theViewer) -- opened message
	set messageID to message id of theMessage
	return my makeMessageURI(messageID)
end tell

on makeMessageURI(theID)
	set theID to "<" & theID & ">"
	set theID to current application's NSString's stringWithString:theID
	set theID to theID's stringByAddingPercentEncodingWithAllowedCharacters:(current application's NSCharacterSet's URLHostAllowedCharacterSet())
	return "message://" & (theID as text)
end makeMessageURI

That’s quite a bit quicker than shelling out to python.

@KniazidisR

In your result I see %3c
in Shane’s one I see %3C

In your result I see a character @
in Shane’s one there is no such character but %40

In your result I see %3e
in Shane’s one I see %3E

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 8 février 2020 16:07:22

message posted after your late edition.

Hi, Shane.

Here is the result returned by my script:
message://%3С0.1.F5.1A.1D5C46F68465366.0@omp.emails.wix.com%3Е

and, this is the result returned by your script:
message://%3C0.1.F5.1A.1D5C46F68465366.0%40omp.emails.wix.com%3E

I updated 3c to 3C, and 3e to 3E. Although I’m not sure that the register plays any role here. So the difference is only between @ and %40. But what does that mean?

I’m presuming it was percent-encoded in the original for good reason, so I provided an AS equivalent. But I confess I can’t make a lot of sense of the rules for message URIs.

Thank you all it is appreciated