I) Intro

I'd like to read out the mailbox a message (of Apple Mail) is currently in.

To read out a message's id one can use:

tell application "Mail"

    set m1 to message 1 of inbox
    return id of m1

end tell

This returns a number corresponding to the message's id.

II) First Question

Now, I would like to do the same thing with the message's mailbox. When I "return m1" above I get something like message id 22929 of mailbox "INBOX" of account "Gmail" of application "Mail". It is my goal to read out "INBOX" similar to "id" before. How do I do that?

III) What I've tried and Specs

Of course, I tried:

return mailbox of m1

This returns mailbox "INBOX" of account "Gmail" of application "Mail", but I cannot make use of it, because the class of this output is still message, not string or similar. In addition AppleScript refuses to convert this output - e.g. via return mailbox of m1 as string or similar and then refining the string by cutting characters...

BTW my app/system versions: OS X 10.11.6, AppleScript 2.8.1, Apple Mail 9.3. I use IMAP accounts.

IV) Context and second Question

My ultimate goal is it to select the first read message in the Inbox, but because

set m1 to message 1 of inbox whose read status = true
set selected messages of first message viewer to m1

also takes messages into account which are read and in the trash, and therefore selects nothing in the message viewer if the most recent read message is already discarded, but has not been not deleted yet.

As you see I actually want to read out the "trash status" via the "mailbox" parameter (?) above... Is this possible at all? How?

V) Progress so far / Partial solution OR How Mail moves messages

The solution to my first question can be found on https://macscripter.net/viewtopic.php?id=30381 and in the script below - i.e. the subroutine at the bottom. It outputs the mailbox as well as the corresponding account as a list with two items after you've input a message.

Up until now I cannot solve my second question, since Mail handles deleted messages in an unexpected way. I've written the script below, which tries to workaround this.

It cycles through the first 200 (change this by setting a variable containing the number of messages in your inbox) unread messages in the inbox. As soon as it finds one, it cycles through "Deleted Messages" and then through the "Trash" in search for a message which matches the time stamp, only considering messages received on or after the timestamp of the read message in the inbox. When two match, it disregards the message in the inbox as it must be in one of the two mentioned folders. But why would it then be checked in the first place - the script should only cycle through read messages in the inbox...

Well, this is exactly the problem. Messages don't seem to ever leave the inbox. Explanation:

Upon deleting, i.e. moving a message to "Deleted Messages" or even the "Trash", it is no longer visible in your inbox. But against my expectations, it has indeed not left the inbox. After some testing, it seems that it stays there, but is just made invisible, not moved, but instead a copy of it with a new, available - that is higher - message id is moved to "Deleted Messages" resp. to the "Trash" for us to see over there.

When it is moved back or to another place within Mail, it seems to be copied again, "of course" with a new, available - that is even higher! - message id. The first copy disappears visually in "Deleted Messages" etc., also it's id is gone from that mailbox. This can be confirmed by playing with

tell application "Mail"
    set g1 to the id of messages 1 thru 5 of mailbox "Deleted Messages" of account "Gmail" -- or similar
    set g2 to the id of messages 1 thru 5 of inbox
    return g1
end tell

But now this second, "recopied" message appears wherever it is copied to under its new message id - visually and as id. Should this place be the earlier inbox, then there seem to be now two (or if you went back and forth a couple times, multiple) identical messages, all with different message ids and only the one with the highest one (i.e. the newest back copied one) is visible. All of this is quite weird and I cannot find a sense in it, but maybe there is one from Mail's perspective...?

Nevertheless, I believe my script below can handle all of this. What it cannot handle are completely deleted messages. So messages that were deleted, that is moved to "Deleted Messages" (the trash can in the side bar), and then deleted again there by emptying the trash or right-clicking and deleting it, that probably is moved to the "Trash". Although this makes little sense as also the "Trash" is cycled through... If this is done, the message is not visible anywhere and it's id cannot be found in "Deleted Messages" or "Trash" (I think thats confirmed, since my script searches those places). But its message id can be found is in the inbox. So even upon complete deletion, a message doesn't seem to leave the inbox!

So my script finds this message as a read message in the inbox and cycles through "Deleted Messages" and "Trash" to match it. As there are no copies of it (anymore), it assumes it to be a read message that is NOT deleted, even though it is!, and returns it as the first message of inbox that is read. Would you advice a script to select that message in that inbox - as it is my goal -, it would not (visibly?) select anything...

>>> This seems unsolvable, since there seems to be no way to find out whether a message is deleted or not...??? But how is Mail able to tell visible and invisible messages apart? That should be usable by Applescript...?!

Maybe this problem is IMAP related?

Also, my script only checks the inbox of the first account, since set mess to (message i of inbox whose read status is true) seems to only cycle through the first mailbox in the sidebar, not the "mother inbox" which combines the inboxes of all accounts. The mother inbox or more specific the combined view is probably only a interface representation of all accounts - the messages of the different accounts are never mixed (but the message ids are shared!). But I would like to search that mother inbox, if anyone has a solution...

VI) My script with the helpful subroutine at the bottom

repeat with i from 1 to 200
    tell application "Mail"
        set mess to (message i of inbox whose read status is true)
    end tell

    set messmb to getmailbox(mess)

    if messmb's item 1 is "INBOX" then

        -- go into deleted messages of inbox and check whether there is a message with equal or higher id that matches sender and time; if yes disregard, if no that's the one > save id
        tell application "Mail"
            set messid to mess's id
            set messtag to mess's date received & mess's message id as list
            set dmlist to {}
            set dmlist to (messages of mailbox "Deleted Messages" of account (item 2 of messmb) whose date received = item 1 of messtag) as list

            set dmlc to count of dmlist
            set dmok to 1

            if dmlc > 0 then
                repeat with r from dmlc to 1
                    set dmtag to dmlist's item r's date received & dmlist's item r's message id as list

                    if messtag = dmtag then
                        set dmok to 0
                        exit repeat
                    end if
                end repeat
            end if

            if dmok is 1 then
                set tlist to {}
                set tlist to (messages of mailbox "Trash" of account (item 2 of messmb) whose date received = item 1 of messtag) as list

                set tlc to count of dmlist
                set tok to 1

                if tlc > 0 then
                    repeat with s from tlc to 1
                        set ttag to tlist's item s's date received & tlist's item s's message id as list

                        if messtag = ttag then
                            set tok to 0
                            exit repeat
                        end if
                    end repeat
                end if

                if tok is 1 then
                    return mess's date received & mess's sender
                end if

                exit repeat
            end if
        end tell
    end if
end repeat

on getmailbox(msg)
        tell application "Mail"
            set msgmb to msg's mailbox as rich text
        end tell
    on error e
        set {TID, text item delimiters} to {text item delimiters, quote}
        set msgoutput1 to text item 2 of e
        set msgoutput2 to text item 4 of e
        set text item delimiters to TID
    end try
    set msgoutput to {}
    copy msgoutput1 to end of msgoutput
    copy msgoutput2 to end of msgoutput
    return msgoutput
end getmailbox

You must log in to answer this question.

Browse other questions tagged .