In this AppleScript on Mavericks:

tell application "Mail"
    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 do not get this behaviour. The only time I get this is if I have Organize by Conversation on and selected a single message in a thread.
    – markhunte
    Dec 25, 2013 at 0:18
  • @markhunte Organize by Conversation doesn't change the count - it's the same either way.
    – jstm88
    Dec 25, 2013 at 3:50
  • Your code is extremely silly. It is sufficient to tell Mail to count selection plain and simple.
    – matt
    Dec 30, 2013 at 2:45
  • @matt Except that this is the starting point for a script to find duplicates, so it needs to step through the messages. Not so silly, right? ;)
    – jstm88
    Jan 1, 2014 at 5:45

1 Answer 1


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.

  • I'll look at rebuilding when I get the time. I've also tried two other mail clients, and what's interesting is that they both show the duplicates. So did Mail cause the duplicates? Did the IMAP server cause them and is Mail actually doing some hidden deduping? Maybe I'll figure it out eventually...
    – jstm88
    Jan 1, 2014 at 5:50
  • Mail does indeed do "hidden deducing", and it may be that these are genuine duplicates; see mjtsai.com/blog/2013/08/26/… for a way to reveal duplicates as duplicates. However, I find that hard to believe, because they are so inconsistent; selecting just one message, I see sometimes 1, sometimes 2, and sometimes even 3 messages in the selection.
    – matt
    Jan 1, 2014 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .