Count unread messages in Mail.app

Hello!

In the forum, i found a good AppleScript to count the unread messages in Mail.app:

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

display dialog "You have " & a as text & "unread messages."

But! It’s counting messages in the Junk and NOT counting in the Smart Folders…

How can i include the Smart Mailboxes and exclude the Junk?

System: 10.4.8
Mail.app: 2.1.1

Thank You!

I believe Smart Mailboxes are not currently scriptable.

On my machine On My Mac isn’t an account. Try this variation

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

display dialog ("You have " & a as text) & "unread messages."

Messages in smart mailboxes are in some other mailbox, so they will get counted.

Hmmmm, Thank You!

tell application "Mail"
	set a to unread count of mailbox "Archive"
end tell

Result: NSReceiverEvaluationScriptError: 4

It’s not possible to count unread messages by mailbox name?

It is possible, your syntax is correct. The mailbox must be a local mailbox on the top level of “On my Mac”

If my sytax is correct, what’s the problem? I tryed this:

tell application "Mail"
	set a to unread count of mailbox "Inbox"
end tell

I always get the same eroor message. I don’t understand.

the mailbox inbox is a special case


tell application "Mail"
	set a to unread count of inbox
end tell

Just found out that the subfolders in an inbox can be traced by using a counter.

Inbox (1)
->Subfolder01(2)
->Subfolder02(5)
–>Subfolder of the subfolder02(3)
–>Subfolder of the subfolder02(4)
->Subfolder03(8)
–>Subfolder of the subfolder02(6)
–>Subfolder of the subfolder02(7)


tell application "Mail"
		
		set Unread_Count to 0
		set counter to 0
		set output_string to ""
		tell application "Mail"
			repeat with theAccount in (every account)
				repeat with theBox in (every mailbox of theAccount)
					set counter to counter + 1
					--display dialog counter as rich text
--inbox
					if (counter = 1) then 
						set Unread_Count to Unread_Count + (count (messages in theBox where read status is false))
					end if
--Subfolder01
					if (counter = 2) then
						set Unread_Count to Unread_Count + (count (messages in theBox where read status is false))
					end if
--Subfolder02
					if (counter = 5) then
						set Unread_Count to Unread_Count + (count (messages in theBox where read status is false))
					end if
--Subfolder03
					if (counter = 8) then
						set Unread_Count to Unread_Count + (count (messages in theBox where read status is false))
					end if
				end repeat
			end repeat
			
			--repeat with theBox in (every mailbox)
			--	set Unread_Count to Unread_Count + (count (messages in theBox where read status is false))
			--end repeat
		end tell
		
		
		if Unread_Count is 0 then #This is true when ALL messages have been read
			set output_string to "0"
			
		else if Unread_Count is 1 then #this is true when only 1 mesasge is unread
			set output_string to "1" # I did this because i like good grammer.
			
		else #default case of more than one unread message
			set output_string to get Unread_Count as string
		end if
		
	end tell


Just wanted to put it here, if someone might need it :slight_smile: