Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript showing incorrect # of msgs in Mail mailbox

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-OTHER, OS X Mountain Lion (10.8.2)

Posted on Nov 15, 2012 7:20 AM

Reply
10 replies

Nov 16, 2012 11:28 AM in response to disguise

By "manually moving the message" I mean that I use the mouse (in Mail) to select the message in Sandbox1, and drag it to Sandbox2. Nothing fancy.


I get the same error, though, whether the message gets moved by my dragging it (as in the previous paragraph) or by my script changing the mailbox of the message (as described in my original post).


Since my original post, I've done a bit more debugging and I find that the "error" fixes itself if, after moving the message, I then use my mouse to go to Mail and just select any other mailbox at random. If I then run a script that shows the number of messages in each mailbox, I get correct answers. But not until I've done that action of manually selecting another mailbox. So now I'm wondering whether there's something I need to do to "close" the mailbox from which the message got moved, so that the move is fully executed, instead of it still having some ghost of the original message in the old mailbox.

Nov 17, 2012 7:12 AM in response to TimMYYZ

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.

Nov 17, 2012 7:18 AM in response to disguise

We're thinking along the same lines. As part of my investigation I turned off all the related messages features in case that had something to do with it. No luck. And then I tried reproducing the same behaviour where there wouldn't be any related messages, and the problem persists. I don't think "related messages" are involved.


I've tried workarounds, including using AppleScript to quit and re-launch Mail to see whether that would do it, but I'm running into other problems there that I haven't had the time (or interest) to resolve and in any case I'd prefer to figure out the underlying problem rather than spend time on a workaround.


Still stumped.

Nov 17, 2012 8:14 AM in response to TimMYYZ

Hi,


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

Nov 17, 2012 8:29 AM in response to TimMYYZ

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.

AppleScript showing incorrect # of msgs in Mail mailbox

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.