I had a similar problem in Mail on Mountain Lion, where attachments did not show up for a message. When looking at the raw source (⌘⎇U) of the message, I also found that headers of the message were missing.
I managed to fix the issue by selecting the mailbox containing the broken email, and then using the "Rebuild" command from the "Mailbox" menu. After a short time, my emails reappeared, complete with attachments.
Since hacks are welcome:
The idea is to use AppleScript to rename folders. For example:
@REPLY
will be renamed to something like
@REPLY (34)
where 34 is @REPLY's total message count.
A working implementation of such a script that renames mailboxes prefixed with @
is (tested on OS X 10.8.2 with Gmail and AIM accounts):
-- Check whether Mail is running
tell application "System Events"
if not (exists (processes where name is "Mail")) then return
end tell
tell application "Mail"
-- Loop over mail accounts
repeat with myAccount in accounts
-- Loop over mailboxes
repeat with myMailbox in mailboxes of myAccount
if name of myMailbox starts with "@" then
set nameOfMyAccount to name of myAccount
-- Help Mail.app's AppleScript suite cope with Gmail
if nameOfMyAccount is "Gmail" then
set nameOfMyMailbox to "[Gmail]/" & name of myMailbox
else
set nameOfMyMailbox to name of myMailbox
end if
set currentMessageCount to the (count of messages of mailbox nameOfMyMailbox of account nameOfMyAccount) as Unicode text
set messageCountShownInMailboxName to (do shell script "echo '" & name of myMailbox & "' | /usr/bin/sed -E 's/.* \\(([0-9]+)\\)/\\1/'") as Unicode text
if currentMessageCount is not equal to messageCountShownInMailboxName then
-- Strip message count: "@TODO (3)" -> "@TODO"
set strippedNameOfMyMailbox to do shell script "echo '" & name of myMailbox & "' | /usr/bin/sed -E 's/(.*) \\([0-9]+\\)/\\1/'"
-- Append current count: ""@TODO" -> "@TODO (4)"
set newMailboxName to strippedNameOfMyMailbox & " (" & currentMessageCount & ")"
-- Rename mailbox
set the name of mailbox nameOfMyMailbox of account nameOfMyAccount to newMailboxName
end if
end if
end repeat
end repeat
end tell
The effect of the script is shown in the following screenshots:
Another implementation that only renames mailboxes specified in a list would be (again, tested on OS X 10.8.2 with Gmail and AIM accounts)::
-- Append message count to these mailboxes
global mailboxesWithMessageCount
set mailboxesWithMessageCount to {"My To Do", "To Reply", "Very Important"}
-- Check whether Mail is running
tell application "System Events"
if not (exists (processes where name is "Mail")) then return
end tell
tell application "Mail"
-- Loop over mail accounts
repeat with myAccount in accounts
-- Mail.app's AppleScript suite gets confused with Gmail's [Gmail] folder
if name of myAccount is "Gmail" then
set mailboxPrefix to "[Gmail]/"
else
set mailboxPrefix to ""
end if
-- Loop over mailboxes
repeat with myMailbox in mailboxes of myAccount
-- Loop over mailboxes which should get the message count appended to their names
repeat with mailboxWithMessageCount in mailboxesWithMessageCount
if name of myMailbox starts with mailboxWithMessageCount then
set currentMessageCount to the (count of messages of mailbox (mailboxPrefix & name of myMailbox) of myAccount) as Unicode text
set messageCountShownInMailboxName to (do shell script "echo '" & name of myMailbox & "' | /usr/bin/sed -E 's/.* \\(([0-9]+)\\)/\\1/'") as Unicode text
if currentMessageCount is not equal to messageCountShownInMailboxName then
-- Strip message count: "@TODO (3)" -> "@TODO"
set strippedNameOfMyMailbox to do shell script "echo '" & name of myMailbox & "' | /usr/bin/sed -E 's/(.*) \\([0-9]+\\)/\\1/'"
-- Append current count: ""@TODO" -> "@TODO (4)"
set newMailboxName to strippedNameOfMyMailbox & " (" & currentMessageCount & ")"
-- Rename mailbox
set the name of mailbox (mailboxPrefix & name of myMailbox) of myAccount to newMailboxName
end if
end if
end repeat
end repeat
end repeat
end tell
The effect of the script is shown in the following screenshots:
Both scripts set the message count:
and, only if necessary, that is, when the message count has changed, update it:
@REPLY (34)
-> @REPLY (29)
My original idea was to run one of the scripts above (or some variation thereof) every time a mail is received with a rule in Mail>Preferences>Rules:
but it can also be added to cron
to run every 10 minutes like this (see crontab (5) man page for crontab syntax explanation):
Save the script to /path/to/updateMailMessageCount.scpt
.
Open Terminal and type:
crontab -e
to open your crontab file.
Move with the arrow keys to the end of the file, in case there are any other entries, and press o
(lowercase letter o), which adds an empty line and goes into insert mode.
Add this line (the 10
means "run every 10 minutes", change as necessary to match your needs):
*/10 * * * * osascript /path/to/updateMailMessageCount.scpt >/dev/null 2>&1
Press esc.
Save and close your crontab with :x!
return (in case anything went wrong, type :q!
, press return and start over).
(If you ever need to stop the cronjob, open your crontab file with crontab -e
, position the cursor over the cronjob and type dd
to delete it. Then save the file as explained above.)
You can use Launch Services, too (see this Apple developer document):
Create a script named ~/Library/LaunchAgents/updateMailMessageCount.plist
with contents (600 is the interval in seconds, that is, 10 minutes):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>updateMailMessageCount</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/path/to/updateMailMessageCount.scpt</string>
</array>
<key>StartInterval</key>
<integer>600</integer>
</dict>
</plist>
Load the job with:
launchctl load ~/Library/LaunchAgents/updateMailMessageCount.plist
(If you ever need to stop the job, do: launchctl unload ~/Library/LaunchAgents/updateMailMessageCount.plist
.)
(My favorite as long time Linux sysadmin is cron
, but note that, on OS X, cron
has been deprecated in favor of launchd. Both methods worked on my Mac running OS X 10.8.2.)
If you wish to refresh the message count with a keyboard shortcut you can create a service (see http://www.macosxautomation.com/services/learn/index.html for more information):
Close Mail.
Open Automator in folder Applications and choose Service:
Select Utilities under Library and drag Run AppleScript to the empty pane on the right:
Configure the service to receive no input in Mail:
Replace the line (* Your script goes here *)
with one of the scripts above.
Save with a descriptive name like Refresh Message Count
. The service will be saved to ~/Library/Services/
.
Now set up the keyboard shortcut:
Open System Preferences, select the Keyboard preference pane and then the Keyboard Shortcuts tab.
Select Services, scroll down to the service you previously added and set the keyboard shortcut to whatever you want with the following restriction: your shortcut shouldn't conflict with an existing shortcut in Mail. I've chosen controloption⌘R:
Open Mail. Note that there is a new service, accessible with controloption⌘R:
(For the curious: if you are wondering where service Reply and Move Sent Message to Current Folder
above comes from, see How to save sent messages in same folder as message being replied to? -- warning: shameless plug.)
Now press controloption⌘R, wait a few seconds and watch how the message count of the folders specified in the script get updated.
Caveats:
- Some folders can't be renamed, such as
[Gmail]/All Mails
or the Inbox
folders.
- Folders are not renamed in real time. That means that the number you see is an approximate value. If you implement the service, you will be able to refresh it with controloption⌘R, though.
- It can be slow if you have many mailboxes, or if the mailbox has many mails
- If you specify a set of mailboxes to rename, be careful that they do not match other folders, for example, "To Reply" will match both "To Reply" and "To Reply Later".
Advantages:
- You will see the message count in any mail client, provided you use Mail.app on your Mac and one of these scripts.
Best Answer
I don't believe that it is possible to change the way unread message counts are aggregated. However, you can change your folders so that they are not displayed under your Inbox. That way your Inbox will only show the count for unread message in your Inbox.
On many mail servers, mailboxes are created and stored underneath the Inbox which is what you are seeing.
By changing your account settings in Mail.app, you can specify the IMAP Path Prefix so that Mail will ignore the first part (INBOX) display these folders independently rather than subfolders of the Inbox.
To do this go to Mail -> Preferences -> Account and select the account in the left column. Then select Advanced on the right and you will see a text box to change the IMAP Path Prefix. Typically, you'll want to change this to INBOX, but this will depend on your mail provider (e.g. Google uses [Gmail]) so you may need to check with your provider.