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.
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
You may try :
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 :
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
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:
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"}
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