Same time and date search in iCal

I am wanting to create a script that searches through two different calendars, reports back any future events that have the same start/end time and date, and then create a reminder at 9pm the day before those dates that meet that criteria. The purpose of this is because I have a friend from work that I commute with when we have the same schedules. I subscribe to his work calendar and I would love to be able to get a reminder to ask him for a ride when we have the same schedule. Make sense? I am somewhat new to scripting and I’ve pieced together the last part of the script (shown below) where it adds the reminder but everything before that where it searches for events that have the same start and end time is where I am completely stumped.


tell application "Reminders"
	set reminder_date to (current date) - (1 * days)
	set time of reminder_date to 75645
	set mylist to list "Work"
	tell mylist
		make new reminder with properties {name:"Ask Scott for ride", body:"New Reminder", due date:reminder_date}
	end tell
end tell

The (current date) would obviously be replaced with the date that the script finds the matching events on. Any help would be appreciated.

Are all the events in each calendar potential candidates, or do you need to match particular summaries?

In the former case, this ought to do it:

tell application "Calendar"
	set myDates to start date of events of calendar "Me"
	set hisDates to start date of events of calendar "Scott"
end tell

set now to (current date)

repeat with i from 1 to (count myDates)
	set thisDate to item i of myDates
	if ((thisDate comes after now) and (thisDate is in hisDates)) then
		set reminder_date to thisDate - days
		set reminder_date's time to 21 * hours
		
		tell application "Reminders"
			make new reminder at end of reminders of list "Work" with properties {name:"Ask Scott for ride", body:"New Reminder", due date:reminder_date}
		end tell
	end if
end repeat

You may want to add code to ensure that you haven’t already set a reminder for the date in question.

Hello.

I just want to mention, that there is a suite of commandline tools for iCal, called iCal-buddy, that may or may not help, I don’t know, as I have my stuff in Omnifocus.

This script works perfect Nigel! I’m ideally wanting to set the script run once a day or once a week by setting up a cron job to do this for me (that is unless there is some way to make it run automatically in the script). As you mentioned at the end of your post, I noticed that if you run the script twice in a row, it creates the same reminder a second time. I’m researching how to code this now but if you could help with this as well, it could save me some time. Thanks again!

Something like this, I suppose:


tell application "Calendar"
	set myDates to start date of events of calendar "Me"
	set hisDates to start date of events of calendar "Scott"
end tell

tell application "Reminders" to set existingDates to due date of (reminders whose name is "Ask Scott for ride")

set now to (current date)

repeat with i from 1 to (count myDates)
	set thisDate to item i of myDates
	if ((thisDate comes after now) and (thisDate is in hisDates)) then
		set reminder_date to thisDate - days
		set reminder_date's time to 21 * hours
		
		if (reminder_date is not in existingDates) then
			tell application "Reminders"
				make new reminder at end of reminders of list "Work" with properties {name:"Ask Scott for ride", body:"New Reminder", due date:reminder_date}
			end tell
		end if
	end if
end repeat

Wow! This works perfect… Thanks again Nigel!!!

I made one very minor correction and just wanted to post it just in case some one else needed to reference it. I noticed that the reminder that it was making was just setting a due date rather than a remind me date. Found the correct element I needed in the applescript dictionary and made the change.


tell application "Calendar"
	set myDates to start date of events of calendar "Work"
	set hisDates to start date of events of calendar "Scott's Work"
end tell

tell application "Reminders" to set existingDates to remind me date of (reminders whose name is "Ask Scott for ride")

set now to (current date)

repeat with i from 1 to (count myDates)
	set thisDate to item i of myDates
	if ((thisDate comes after now) and (thisDate is in hisDates)) then
		set reminder_date to thisDate - days
		set reminder_date's time to 21 * hours
		
		if (reminder_date is not in existingDates) then
			tell application "Reminders"
				make new reminder at end of reminders of list "Work" with properties {name:"Ask Scott for ride", body:"New Reminder", remind me date:reminder_date}
			end tell
		end if
	end if
end repeat