Sunday, September 27, 2020

#1 2020-02-07 03:38:07 pm

maweir
Member
Registered: 2014-03-28
Posts: 30

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...)

Applescript:


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

Select Opened Email Window

Browser: Safari 605.1.15
Operating System: macOS 10.14

Offline

 

#2 2020-02-08 01:33:33 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1305

Re: Select an Already opened email message via AppleScript

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

Applescript:


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):

Applescript:


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

Last edited by KniazidisR (2020-02-08 09:27:00 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-02-08 06:25:19 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Select an Already opened email message via AppleScript

KniazidisR wrote:

No need python.



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:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2020-02-08 09:09:51 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4569

Re: Select an Already opened email message via AppleScript

@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.

Last edited by Yvan Koenig (2020-02-08 09:10:52 am)

Offline

 

#5 2020-02-08 09:28:44 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1305

Re: Select an Already opened email message via AppleScript

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?

Last edited by KniazidisR (2020-02-08 09:29:13 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#6 2020-02-08 07:06:33 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Select an Already opened email message via AppleScript

KniazidisR wrote:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2020-02-08 07:43:52 pm

maweir
Member
Registered: 2014-03-28
Posts: 30

Re: Select an Already opened email message via AppleScript

Thank you all it is appreciated

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)