Script to count Emails

I have found this script to get the email count of my inbox.

get count of messages of inbox

What I need is the count of emails containing “xyz” in the subject line and for a specific time period (eg 11.00pm yesterday - 11.00pm today)

Can anyone please help me with this?

Hi, this is pretty simple. Your original question is practically the code for the first example.

This will search within the past 24 hours (time-irrespective):


tell application "Mail" to count ¬
(inbox's messages whose subject contains "XYZ" and date received comes after ((current date) - 1 * days))

This will search within specific ranges:


set {startRange, endRange} to {date "August 18, 2012 11:00:0 PM", date "August 19, 2012 11:00:0 PM"}
set searchfor to "XYZ"

tell application "Mail" to count ¬
	(inbox's messages whose subject contains searchfor ¬
		and date received comes after startRange ¬
		and date received comes before endRange)

Hi Marc.

Thank you so much for taking the time to help with this.

Unfortunately I can’t get it to work. Initially ScriptEditor wasn’t happy with the date format but I managed to figure that one out on my own.

It runs, and says ‘running’ but returns a count of ‘0’, even though I can see 10 the emails in my inbox.

To simplify I set it up as below… but still a count of zero!! Any ideas?

[code]set {startRange, endRange} to {date “Sunday, 19 August 2012 02:26:41”, date “Monday, 20 August 2012 20:26:41”}
set searchfor to “a”

tell application “Mail” to count ¬
(inbox’s messages whose subject contains searchfor ¬
and date received comes after startRange ¬
and date received comes before endRange)
return count[/code]

Hi,

count is a reserved word, it cannot be used as a variable.
Set the result to a variable


tell application "Mail" to set mailCounter to count.
.
return mailCounter

or just use


.
return result

result contains always the result of the preceding line

Perfect.

Thank you both!!

Return serves only to move end values to the event log; it doesn’t alter what is already returned from an expression, so I prefer to not use it in my code. I recommend keeping Script Editor’s result window open to see completed events (Window>Result History).