Find iCal events within time frame

Hey.

I want to make a script that I will run each time my mac returns from screen saver.
The goal of the script is to look into iCal for events that occur within today - tomorrow and contains [Alarm] in the notes, then change the wakeup schedule in energy saver to 61min before the event occurs.

All this to work around that iCal alarms won’t wake up my mac, and putting a fixed wake up time every day won’t work since it will enter sleep after 30min.

The wake up part will probably be with something like pmset schedule wake <The date/time the event occurs (ie 01/25/10 05:59:00 if the event occurs at 07:00)>. Any pointers? Mainly with how to deal with finding the events.

Hi Arkitekten.

I am not experienced enough to actually write the script, but here http://macscripter.net/viewtopic.php?pid=123564#p123564 you’ll find a script that wades through all entries of a calendar.

Your final solution might look something like this:

--  Definition of the time frame in which to search for an event
set current_date to current date
set time_frame_start to current_date - 2 * hours --  Look two hours back in time, like when the machine just woke up from sleep...
set time_frame_end to current_date + 1 * days --  Look ahead 1 day into the future...


tell application "iCal"
	--  Wade through all events in each calendar that occur in the defined time frame
	set all_events_of_calendar to (every event of calendar "WakeUps" whose (start date ≥ time_frame_start and start date ≤ time_frame_end))
	repeat with current_event in all_events_of_calendar
		
		--  Check if any of them have the word "blah" in the description
		tell current_event
			if exists description then set current_event_description to description
			if current_event_description contains "blah" then do shell script "pmset sched ...."
		end tell
		
	end repeat
end tell

I’m getting an error that says it can’t fetch “every event whose start date ≥ date ‘sunday january 2010 08:22:45’ and start date ≤ date ‘tuesday 26 january 2010 10:22:45’”.

This is what I have so far

set current_date to current date
set time_frame_start to current_date - 2 * hours -- Look two hours back in time, like when the machine just woke up from sleep...
set time_frame_end to current_date + 2 * days -- Look ahead 1 day into the future...


tell application "iCal"
	-- Wade through all events in each calendar that occur in the defined time frame
	set all_events_of_calendar to (every event whose (start date ≥ time_frame_start and start date ≤ time_frame_end))
	repeat with current_event in all_events_of_calendar
		set alarm_time to alarm + 3 * minutes -- Should get the time the alarm on the event is set to go off
		
		-- Check if any of them have the word "[Alarm]" in the description
		tell current_event
			if exists description then set current_event_description to description
			if current_event_description contains "[Alarm]" then do shell script "pmset schedule wake alarm_time"
		end tell
		
	end repeat
end tell

Not sure if the line for getting the alarm time of the event is correct though and also, how do I insert that time into the pmset shell script? And another thing, what will happen if it finds two or more events with alarms set to them, will the wake up time be pushed forward to the latest event alarm?

Hi.

I don’t understand the point of waking the computer 61 minutes before the event time if it’s going to go to sleep again after 30 minutes, so you’ll have to adjust the criteria below yourself. The script, adapted from maelcum’s, ignores any trigger intervals on the alarms and goes solely on the events’ start dates.

--  Definition of the time frame in which to search for an event
set current_date to current date
set time_frame_start to current_date + 30 * minutes --  Look forward to when the macine's likely to go to sleep...
set time_frame_end to time_frame_start + days --  Look ahead for 24 hours after that...

tell application "iCal"
	--  Get the start dates of all events which match the criteria.
	set start_dates to start date of (every event of calendars whose (start date ≥ time_frame_start and start date ≤ time_frame_end and description contains "[Alarm]"))
	-- The result's a list containing lists for every calendar in the application.
	-- Each "calendar" list contains the start dates of any matching events in that calendar.
end tell

-- Get the earliest start date returned.
set earliest_start_date to time_frame_end + 1 -- Initial dummy start date beyond the time frame
repeat with this_calendar in start_dates
	repeat with this_start_date in this_calendar
		if (this_start_date comes before earliest_start_date) then set earliest_start_date to this_start_date's contents
	end repeat
end repeat

if (earliest_start_date comes after time_frame_end) then return -- No matching events found. Don't go on to change the computer's wake-up time.

-- Get the date/time 61 minutes before the earliest start date found, in "dd/mm/yy hh:mm:ss" format.
set wake_up_date to earliest_start_date - 61 * minutes
tell wake_up_date
	tell ((its year) * 10000 + (its month) * 100 + (its day)) as text
		set dateStr to text 5 thru 6 & "/" & text 7 thru 8 & "/" & text 3 thru 4 -- "mm/dd/yy"
	end tell
	set t to its time
	tell (1000000 + t div hours * 10000 + t mod hours div minutes * 100 + t mod minutes) as text
		set timeStr to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7 -- hh:mm:ss (24-hour format)
	end tell
end tell

-- Set the computer to wake up at that time. Use your own administrator name and password:
do shell script ("pmset schedule wake \"" & dateStr & " " & timeStr & "\"") user name "yourusername" password "yourpassword" with administrator privileges

Not at home just now, but it looks good. Will try it tomorrow. The 61min is for the alarm that I have is 60mins before the event.

BTW, what’s the best way of making iCal convert events with [Alarm] in the notes to set another reminder? I mostly add events through google calendar and it would be nice if I didn’t have to adjust them in ical to run the alarmscript I have.

Well, since running scripts is a very specific iCal function it’s no surprise that things have to be converted after having been entered in google calendar. I assume you are running your conversion script in regular intervals via launchd, right?
Thats about as good as it gets, I guess… :stuck_out_tongue:

Alarms can be created in gcal and sync to iCal via “BusySync”, but I do not know about scripts. gcal doesn’t give you an option to enter that information in the first place.

Your original question revolved around the task of waking up the machine at a certain time. To do what for? Maybe there are other ways to achieve what you are trying to.

What I’m looking for is a wakeup alarm that is in sync with the work schedule I have entered in google calendar (and synced with iCal). That’s about the most specific I can explain it :stuck_out_tongue:

Had a nice surprise today btw. Got home from work. iTunes was blasting my wakeup-script lol. My neighbours must love me.