AppleScript counts multiple Mail messages In this AppleScript on Mavericks:

tell application "Mail"
    activate
    set viewer to the first message viewer
    set msgList to selected messages of viewer
end tell

set msgCount to 0
repeat with m from 1 to the count of msgList
    set msgCount to msgCount + 1
end repeat


I get a much higher message count than the number of selected messages (it can be a difference of almost 30% for some mailboxes). I have confirmed that selecting ONE message with no replies can result in the message being counted more than once. This is with a standard IMAP account.

Is there any reason other than IMAP errors for this discrepancy?
I've seen this too, and I presume it's because Mail in Mavericks plays badly with IMAP in various ways, and AppleScript aggravates these issues. For example, some people are finding that if you move a message from one IMAP mailbox to another using AppleScript, it can take 30 seconds or even a minute - but if you do the same thing just by dragging the message, it's very fast.

So, to sum up, my answer is: yes, it's real; yes, it's a bug, and no, you're not going to be able to do anything about it. Mail is doing some kind of caching here. Quitting and relaunching Mail might (or might not) clear up the problem temporarily. But it might not.

I tried getting the Mail selection with AppleScript just now and I got four messages when only two were selected. But close examination showed that this was only two distinct messages; Mail has wrongly assigned to two different unique message ids to each message. That's a massive bug, and there's nothing you can do about it. (I tried rebuilding the mailbox but it didn't help.)

EDIT: I played around with this some more: I turned off View -> Organize By Conversation and I then rebuilt the mailbox, and that seems to have helped.