Thursday, October 17, 2019

#1 2019-09-21 10:11:49 pm

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

Get all properties of selected in "Mail.app" message.

Hi, all.
Here I wrote script to get all properties of selected in "Mail.app" message. There are total 20 properties. 19 properties I get directly from message easy. The problem is getting the property mailbox. I got it using 3 repeat loops, which I don't like at all. The question about help is this:

Is it possible to get this property somehow easier?

NOTE: In my "Mail.app" mailboxes have localized names.

Applescript:


tell application "Mail"
   set aMessage to item 1 of (get selection)
   tell aMessage
       set aContent to content
       set |source| to source
       set |subject| to subject
       set |sender| to sender
       set |messageId| to message id
       set messageSize to message size
       set wasForwarded to was forwarded
       set wasRedirected to was redirected
       set wasRepliedTo to was replied to
       set replyTo to reply to
       set readStatus to read status
       set junkMailStatus to junk mail status
       set |id| to id
       set flaggedStatus to flagged status
       set flagIndex to flag index
       set deletedStatus to deleted status
       set backgroundColor to background color
       set dateReceived to date received
       set dateSent to date sent
       set |mailBox| to my getMailBox(aMessage)
   end tell
   return {|Name|:name of |mailBox|, unreadCount:unread count of |mailBox|, containerName:name of container of |mailBox|, accountName:name of account of |mailBox|}
end tell
-->{|Name|:"Όλα τα μηνύματα", unreadCount:3, containerName:"[Gmail]", accountName:"Google"}

on getMailBox(aMessage)
   tell application "Mail"
       set |accounts| to accounts
       repeat with i from 1 to (count |accounts|)
           set |account| to item i of |accounts|
           set |mailboxes| to mailboxes of |account|
           repeat with j from 1 to (count |mailboxes|)
               set |mailBox| to item j of |mailboxes|
               set |messages| to messages of |mailBox|
               if |messages| ≠ {} then
                   repeat with k from 1 to (count |messages|)
                       set |message| to item k of |messages|
                       if aMessage = |message| then return |mailBox|
                   end repeat
               end if
           end repeat
       end repeat
   end tell
end getMailBox

Last edited by KniazidisR (2019-09-21 10:43:07 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#2 2019-09-22 03:39:08 am

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

Re: Get all properties of selected in "Mail.app" message.

You may try :

Applescript:

tell application "Mail"
   tell item 1 of (get selection)
       set {theAccount, theName, theContainer, theUnreadCount} to {name of account, name, name of container, unread count} of (get properties of its mailbox)
       {theAccount, theName, theContainer, theUnreadCount}
   end tell
end tell

Sometimes, account is missing value and/or theContainer may be unreachable so it would be better to use :

Applescript:

tell application "Mail"
   tell item 1 of (get selection)
       set mailBoxProperties to (get properties of its mailbox)
       set {theAccount, theName, theContainer, theUnreadCount} to {account, name, container, unread count} of mailBoxProperties
       if theAccount is not missing value then set theAccount to name of theAccount
       try
           set theContainer to name of theContainer
       on error errmsg
           log errmsg (*Erreur dans Mail : Il est impossible d’obtenir account id "30ACBF46-881C-40AC-B33B-500D53832270".*)
           set theContainer to missing value
       end try
       {theAccount, theName, theContainer, theUnreadCount}
       
   end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 22 septembre 2019  11:37:43

Last edited by Yvan Koenig (2019-09-22 04:17:13 am)

Offline

 

#3 2019-09-22 04:53:43 am

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

Re: Get all properties of selected in "Mail.app" message.

Thank you very much, Yvan. This helped.

As I see, you use the keyword its with mailbox. This keyword returns now correct results. With your help, I can now remove unneeded handler with repeat loops:

Applescript:


tell application "Mail"
   set aMessage to item 1 of (get selection)
   tell aMessage
       set aContent to content
       set |source| to source
       set |subject| to subject
       set |sender| to sender
       set |messageId| to message id
       set messageSize to message size
       set wasForwarded to was forwarded
       set wasRedirected to was redirected
       set wasRepliedTo to was replied to
       set replyTo to reply to
       set readStatus to read status
       set junkMailStatus to junk mail status
       set |id| to id
       set flaggedStatus to flagged status
       set flagIndex to flag index
       set deletedStatus to deleted status
       set backgroundColor to background color
       set dateReceived to date received
       set dateSent to date sent
       set |mailBox| to its mailbox
   end tell
   return {|Name|:name of |mailBox|, unreadCount:unread count of |mailBox|, containerName:name of container of |mailBox|, accountName:name of account of |mailBox|}
end tell
-->{|Name|:"Όλα τα μηνύματα", unreadCount:3, containerName:"[Gmail]", accountName:"Google"}

Last edited by KniazidisR (2019-09-22 05:04:44 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#4 2019-09-22 06:34:24 am

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

Re: Get all properties of selected in "Mail.app" message.

Thank you.

This tip is an old ape's one.
I use it for year every time I want to grab properties of an object.
Often it's not required but from time to time it's required.
To spare my rare hair I decided to use it always.

CAUTION: if the message is not stored in an InBox the containers may be unreachable.
As I added in my post, some extraneous instructions may be welcome.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 22 septembre 2019  14:34:16

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)