Skip navigation

AppleScript showing incorrect # of msgs in Mail mailbox

610 Views 10 Replies Latest reply: Nov 17, 2012 8:46 AM by disguise RSS
TimMYYZ Level 1 Level 1 (0 points)
Currently Being Moderated
Nov 15, 2012 7:20 AM

I create two new mailboxes in "Mail" -- "Sandbox1" and "Sandbox2".  I use AppleScript to show the number of messages in the mailboxes -- 0 and 0 as you would expect.  I copy an existing message from my Inbox into Sandbox 1.  Check the count again; 1 and 0 as you'd expect.


Then I manually move the message from Sandbox1 to Sandbox2.  Check the count again -- 1 and 1.  That's not right.  When I look in Mail, I see no messages in Sandbox1, and my moved message in Sandbox2.


I've tried everything I can think of and I'm stumped.  I'm doing this because I have more complex script to manage messages and I was running into this strange behaviour, when the script moved messages from one mailbox to another (by changing the mailbox of the message).


Thanks in advance for any suggestions.

Automator, OS X Mountain Lion (10.8.2)
  • disguise Level 2 Level 2 (330 points)

    When you manually move the message from Sand1 to Sand2 - You do this using Terminal? How exactly?

  • disguise Level 2 Level 2 (330 points)

    I can reproduce the behavior you're experiencing - no solution

  • disguise Level 2 Level 2 (330 points)

    Tim - I've been playing with the "Show Related Messages" feature. This helps to reveal duplicates. I'm guessing that this has something to do with the message counts we're receiving. Also, to help debug - In Terminal, I navigate to the Mailbox and run 'ls -l | wc -l' to count the number of message files. At first I thought this might be a buffering problem.


    Now, however, I'm thinking "Show Related Messages" is somehow involved.


    The bad news is that I don't see any way to allow for this in AppleScript.


    Still unresolved.

  • Jacques Rioux Level 4 Level 4 (1,925 points)



    A workaround :  just deselect the mailbox and reselect, like this :


    tell application "Mail"
         tell front message viewer to if exists then
              set selBoxes to selected mailboxes
              if selBoxes is not {} then
                   set selected mailboxes to {} -- deselect
                   set selected mailboxes to selBoxes
              end if
         end if
         count of messages of mailbox "Sandbox1"
         count of messages of mailbox "Sandbox2"
    end tell
  • disguise Level 2 Level 2 (330 points)

    1) I created a brand new Mailbox - On My Mac


    2) In Terminal - ls -al ~/Library/Mail/V2/Mailboxes


         The new mailbox folder DOES NOT exist - even though it shows in the Mail sidebar.


         The new mailbox folder DOES exist  in ~/Library/Mail/V2/Mailboxes/.mboxCache.plist


    3) Select another maiilbox from which to drag and drop a message.


         The new mailbox still DOES NOT exist in the Mailboxes folder.


    4) Drag and Drop a message into my brand new Mailbox.


         Voila! The 'ls' command reveals that the brand new mailbox exists in the Mailboxes folder.


         I navigate down the new mailbox to the Messages folder and, sure enough, the message file exists.


    5) In Mail, I select the new mailbox and it shows one message.


         The Mail app says there is 1 message in the mailbox.


         I run the AppleScript to count the messages and it reads 1


         So far so good.


    6) While still in the new mailbox, I now drag -n-drop the only message to another mailbox.


         The Mail app shows the message disappear and the count is 0 - fine


         However, I do an 'ls' in the new mailbox folder and the message still exists.


         AND, when I run the AppleScript, the count still reads 1 - NOT fine


         AND, a the new message exists in the drop target mailbox.


         So, at this point the message exists in the drag source mailbox AND in the drop target mailbox.


    7) I now click on the drop target mailbox


         In the Mail app, the message exists in the drop target mailbox.


         In Terminal, 'ls' reveals the file exists in the drop target mailbox.


         In Terminal, 'ls' reveals the file DOES NOT exist in the drag source mailbox.


    Conclusion, Mail app uses .mboxCache file to store new mailbox creation UNTIL a message is actually stored in the mailbox, then the app physically creates the mailbox folder. Subsequent operations performed while residing in the mailbox may not take place - are cached - until the user switches mailboxes.


    I still see no way to code for this caching behaviour in AppleScript.

  • disguise Level 2 Level 2 (330 points)

    My last post -


    I reran steps 1 & 2 above.


    At this point the new mailbox folder DID NOT physically exist - only in the .mboxCache file.


    Now I ran my AppleScript to count the messages in my new mailbox.


         The count is 0 - correct.


         This also triggered the actual creation of the mailbox folder.


More Like This

  • Retrieving data ...

Bookmarked By (1)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.