Unread Message Count

I am trying to get the number of unread messages in the Inbox. I have tried this:

tell application "Mail"
	check for new mail
	get unread count of mailbox "In"
end tell

But it does not work.


It’s easier to use the ‘in mailbox’ property of Mail to get the reference to the in mailbox. Confusion arises because there are different accounts. In the following simple script, the first statement gets a list of unread messages and the second statement counts items of the list:

tell application “Mail”
set unread_messages to (every message of in mailbox whose is read is false)
set unread_meassage_count to (count unread_messages)
end tell

Note that this is using Jaguar Mail and things might have changed with Panther Mail.


That does not even pass the debugger…

Try this

tell application "Mail"
	set total to 0
	repeat with theAccount in (every account)
		repeat with theBox in (every mailbox of theAccount)
			set total to total + (count (messages in theBox where read status is false))
		end repeat
	end repeat
	return total
end tell

This works very well. Thanks!