For those of you who use Gmail IMAP in Mail as I do, you are probably just as annoyed with one thing- the new messages counter always reads twice the actual number. This is because the message is sent to both your inbox and the folder titled “Gmail” or “All Mail”. Just put the following into Script Editor, and voila!
tell application “Mail”
set unreadBox to mailbox “[Gmail]/All Mail” of account “Gmail IMAP”
set unreadCount to unread count of unreadBox
if unreadCount is not 0 then
repeat unreadCount times
set unreadMail to first message of unreadBox whose read status is false
set read status of unreadMail to true
end repeat
end if
end tell
Before running this script, you may have to type in the following in script editor in order to get the official name of your account:
tell the application “Mail”
get accounts
end tell
Once you have done this, change “[Gmail]/All Mail” to the name of the folder as it appears in Mail. Then, change “Google IMAP” to the official name of your account. This should do it!
Comment with any questions or if you have any suggestions for a more efficient script, do tell.


















1) This was nicked from MacOSX hints
2) It's a stupid idea there, too. If you mark your 'all mail' read, then it will have the effect of automatically marking all mail as read in other inboxes as well, so the next time it synchronizes it will pick up the changes and mark all mail read from other folders too.
The solution is to mark your inbox as your inbox, and then the new message count shows what's new in the inbox. If you don't do that, it shows all unread messages in all folders.
You can also handle this with Mail rules. I have one that moves all my sent mail to my Sent Mail folder and marks it read.
I'm not blaming either Google or Mail.app for the issue, I'm simply putting out a way to remedy it.
Dude. Mail.app is not the one to blame for the email appear both in the Inbox and the Gmail/All folder. The "All" folder in Gmail contains all your mail ( Sent, Deteled, Forwards, Inbox etc ). Writing this script is an exercise in futility.
Google mentions this here:
http://gmail.google.com/support/bin/static.py?page=troubleshooter.cs&problem=bugflow&selected=bugflow_imap08