iCal: Count events in a given month or week

Hello, this script counts all iCal “Inizio” calendar events, how can I count only the events of a given month or a given week?


tell application "iCal"
	activate
	count events of calendar "Inizio"
end tell

thank you very much

Paolo

Model: MacBook Pro
AppleScript: 2.4.3
Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)

Hi, Paolo. Welcome to MacScripter.

The precise answer to your question depends on how the month or week is “given”. :slight_smile: Basically, you’ll need to get a date representing the beginning of the period and another representing the end of it and look for dates which come between the two:


tell application "iCal"
	activate
	set c to (count (events of calendar "Inizio" whose start date ≥ startOfPeriod and start date ≤ endOfPeriod))
end tell

Or, since ‘whose’ filters tend to be rather slow:


tell application "iCal"
	activate
	set startDates to start date of events of calendar "Inizio"
end tell

set c to 0
repeat with thisDate in startDates
	if (thisDate ≥ startOfPeriod) and (thisDate ≤ endOfPeriod) then set c to c + 1
end repeat

c

The above examples only count events which start in the periods concerned. Finding iterations of repeating events is far more complicated.

Defining the beginning and end points for the month or week depends on what you want to do. If you want to count events for, say, the current month, you could get the month’s start and end dates like this:

-- Get the start and end dates for the month containing the current date.

set today to (current date)
set today's time to 0

set startOfPeriod to today - (today's day) * days + days
tell startOfPeriod + 32 * days to set endOfPeriod to it - ((its day) - 1) * days - 1

Or for the current Monday-start week like this:

-- Get the start and end dates for the Monday-start week containing the current date.

property weekStart : Monday

set today to (current date)
set today's time to 0

set startOfPeriod to today - ((today's weekday as integer) + 7 - (weekStart as integer)) mod 7 * days
set endOfPeriod to startOfPeriod + weeks - 1

Thank you! You were really great!

My problem now is to count the events from the current day, every day ends at 9:00 PM, I tried this but it works! :frowning: ((
Sorry for my English … I’m Italian and I speak little English


set today to (current date)
set today's time to 0

set StartMeseCorrente to today
tell StartMeseCorrente + 32 * days to set EndMeseCorrente to it - ((its day) - 1) * days - 1
set PrimoGiorno to day of (current date)
set UltimoGiorno to day of EndMeseCorrente

set n to 0
repeat (UltimoGiorno - PrimoGiorno) + 1 times
	set Start to today + n * days
	set Fine to today + 75599 + n * days
	tell application "iCal"
		close window 1
		set NumEventSettPross to (count (events of calendar "Inizio" whose start date ≥ Start and start date ≤ Fine))
		set n to n + 1
	end tell
end repeat
tell application "iCal" to quit
NumEventSettPross

Model: MacBook Pro
AppleScript: 2.4.3
Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)

Hi, Paolo.

As I understand you, you want to count the events from today to the end of the month, ignoring any which occur at or after 21:00:00, with the result in NumEventSettPross. I think this modification to your script should do it:


set today to (current date)
-- if (today's time ≥ 21 * hours) then set today to today + days
set today's time to 0

set PrimoGiorno to today's day
tell today + (32 - PrimoGiorno) * days to set UltimoGiorno to (it - (its day) * days)'s day -- ;)

set Start to today
set Fine to today + 21 * hours - 1

set NumEventSettPross to 0
repeat with Giorno from PrimoGiorno to UltimoGiorno
	set Start's day to Giorno
	set Fine's day to Giorno
	tell application "iCal"
		-- close window 1
		set NumEventSettPross to NumEventSettPross + (count (events of calendar "Inizio" whose start date ≥ Start and start date ≤ Fine))
	end tell
end repeat
tell application "iCal" to quit
NumEventSettPross

Or, probably faster:

set today to (current date)
-- if (today's time ≥ 21 * hours) then set today to today + days
set today's time to 0

set StartMeseCorrente to today
tell StartMeseCorrente + 32 * days to set EndMeseCorrente to it - ((its day) - 1) * days - 1

tell application "iCal"
	set startDates to start date of events of calendar "Inizio"
	quit
end tell

set NumEventSettPross to 0
repeat with thisDate in startDates
	if (thisDate ≥ StartMeseCorrente) and (thisDate ≤ EndMeseCorrente) and (thisDate's hours < 21) then set NumEventSettPross to NumEventSettPross + 1
end repeat

NumEventSettPross

I’d better explain this:

It returns the day number of the last day of the month in which today occurs ” ie. the number of days in the current month:

Subtract today’s day number from 32 and add the resulting number of days to today to get a new date in the first few days of next month.
Subtract that date’s day number from it in days to get the date of the last day of this month.
Extract the day number from that.

Great!!
I modified the script of the week to make it compatible with the events <21 PM!
You can modify this script to count events for each week of the month. (example: the first week there are 2 events, the second week of 5 events, etc.)


property weekStart : Monday

set today to (current date)
set today's time to 0

set StartMeseCorrente to today - ((today's weekday as integer) + 7 - (weekStart as integer)) mod 7 * days
set EndMeseCorrente to StartMeseCorrente + weeks - 1

tell application "iCal"
	run
	set endDates to end date of events of calendar "Inizio"
	
	set NumEventSettPross to 0
	repeat with thisDate in endDates
		if (thisDate ≥ StartMeseCorrente) and (thisDate ≤ EndMeseCorrente) and (thisDate's hours < 21) then set NumEventSettPross to NumEventSettPross + 1
		
	end repeat
	quit
end tell
StartMeseCorrente & EndMeseCorrente & NumEventSettPross