How many eMails are stored in Apple Mail?

Some time ago my colleagues at work asked me to write an AppleScript which simply displays the total number of eMails stored in Apple Mail. Maybe you can also make good use of it?

Try this one if you are running a version of Mac OS X that already features Spotlight:


property mytitle : "homama"

on run
	try
		tell application "System Events"
			set fullusrname to full name of current user
		end tell
		set mailfolderpath to POSIX path of (((path to library folder from user domain) as Unicode text) & "Mail:")
		set getmailcount to "mdfind -count -onlyin " & quoted form of mailfolderpath & " \"kMDItemKind == 'Mail Message'\""
		set mailcount to do shell script getmailcount
		set resultmessage to "Apple Mail of user " & fullusrname & " currently stores " & mailcount & " email messages." & return & return & "Scanned mail folder:" & return & mailfolderpath
		tell me
			activate
			display dialog resultmessage buttons {"OK"} default button 1 with title mytitle
		end tell
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")" buttons {"Never mind"} default button 1 with icon stop with title mytitle
		end tell
	end try
end run

If you are running a version of Mac OS X that doesn’t already feature Spotlight, you can just download this alternative AppleScript (initially written for my beloved wife), which offers even more funtionality, but internally uses a Python script to count the eMail messages.

Now how many eMail message have you got? :smiley: