Scripting iCal

When recurrent events are inserted in a Calendar, neither ‘summary’ nor ‘date’ of the ‘event’ show up in a request for every event in a particular calendar. Do you have to calculate those recurrences yourself, or did I miss something in iCal’s dictionary that would tell me?

Hi Adam,

You have to calculate them. What are you trying to do?

Best wishes

John M

I have two calendars that list specific categories of events, and either on demand with FastScripts hot keys or automatically in the morning, two scripts produce the next four events on one, and all upcoming events on the other. In the latter of these are some repeated events (every six weeks) and they don’t appear after the first one in the series has (except one that was modified. When I get the properties of that event, however, I don’t see anything that would indicate that it was repeated - do I just have to know that, or should I be including the next repeat in my script and not using repeat in iCal?

Hi Adam,

Applescripting of recurrence in iCal is not easy. If you know that you will only be dealing with specific types of repeat you could handle this by looking at events that may repeat in the relevant time scale and find when they occur.

IIRC Modified events in a recurrence are marked as excluded dates in the recurrence and a new seperate event is made with the changes.

Best wishes

John M

Thanks, John - I haven’t gone feeble minded after all, and presuming that “not easy” equates to “don’t bother trying”, I’ll simply set up my script to predict those events on it’s own given a key symbol in the first instance so I can find it among all events. We shall, as they say, overcome.

Hi Adam,

I remember having a discussion at Apple iCal, but it went off the discussion list already.

You can check for recurrence with something like this:


tell application "iCal"
	set this_cal to first calendar whose title is "Work"
	tell this_cal
		set recur_events to every event whose recurrence is not ""
	end tell
	-- repeat start
	set this_recur to item 1 of recur_events
	set recur_info to recurrence of this_recur
	-- repeat end
end tell

Your recurrence event is simple and looks like this:

“:FREQ=WEEKLY;INTERVAL=6;BYDAY=SA;WKST=SU”

Then, you could get the start date and find multiples of 6 weeks:


tell application "iCal"
	set this_cal to first calendar whose title is "Work"
	tell this_cal
		set recur_events to every event whose recurrence is not ""
	end tell
	-- repeat start
	set this_recur to item 1 of recur_events
	set recur_info to recurrence of this_recur
	-- repeat end
	set start_date to start date of this_recur
end tell
-- repeat start
set i to 1
set next_date to start_date + i * 6 * weeks
-- repeat end

I’m still not sure how you have your thing setup, but simple recurrence events shouldn’t be to hard. It’s when you try to script all recurrence events when difficulty arises.

gl,

Thanks, Kel;

I didn’t figure out that recurrence was a property of an event - when I had tried it, it seemed always to be “”. Must have screwed up. Now I’ll rework my rather complicated test with your very simple one.

I had already figured a work around, though grabbing it by name and then doing this to get the next one after now - I don’t want them all, just the upcoming one:

to NextTest(Dstart, now)
set aDate to Dstart
repeat until (aDate - now) > 0
set aDate to aDate + 6 * weeks
end repeat
return aDate
end NextTest

Hi Adam,

I don’t think you need the repeat loop just to find the next occurence from the current date. There should be a formula and I think this works as an example:


set t to "1/1/05"
set start_date to date t
set cur_date to (current date)
set i to 6 * weeks
cur_date + (i - ((cur_date - start_date) mod i))

I did a quick one here, so it needs checking. It should give the date of the next occurence.

gl,

Works very nicely, Kel. I have an additional constraint that it be a Thursday (another recurrence occurs on another day) but that’s easy to build in.

Where did you get this? (I’ve crunched it)

tell (repWeeks * weeks) to set nextAppt to now + (it - ((now - startDate) mod it))

That is really slick! :lol:

Hi Adam,

Thanks.

I just pictured it out. If you take the date difference, you get the number of seconds. Mod that with the period (6 weeks) and you get the remainder. Subtract the remainder from the period and you get the number of seconds till the next recurrence. So, if you add that to the current date, then you get the date of the next occurrence.

Nice crunch. You may be able to crunch the formula also. In fact you can see how you can easily remove the first parenthesis and put the current date ahead 1 period. Then subtract the remainder. It probably won’t matter very much anyway though. Maybe Nigel or Kai has something on the efficiency.

gl,

Hi Kel & Adam,

That’s really interesting. I had not come across doing this with modular numbers before.

Thanks

John M

What I’ve done for repeated events is to follow their title with something like (6 wks), then search for the recurrences not “”, find the offset in the Summary of the event (the header) of the character “(” and then pick up the 6 and the wks (dys, mos, etc.) and use Kel’s trick to find the next one for my Growl display.

Thanks to all here.