How do you extract current events from a subscription calendar in iCal

I’ve search several sites and the only reference I have found to this issue is an excerpt from Adam below.

I am writing a simple (I thought) AppleScript that would determine whether the current day is a federal holiday or not. I have a home automation software (ie. Indigo) that would use this value to determine whether or not certain events gets executed (ie. wake up call for work, put out the trash reminder, etc.). I’ve subscribed to the “US Holidays” iCal calendar at webcal://ical.mac.com/ical/US32Holidays.ics and have created a list with a subset of federal holidays from the US Office of Personnel Management (http://www.opm.gov/fedhol/). I was under the impression that it would be relatively straightforward to run the script at 12:01 in the morning and extract all of the events for the current day in the “US Holidays” calendar and compare them to the federal holiday list and set a boolean value to either true or false. Well, it has not been so easy. The start and end dates of every event in the subscription calendar is the date the first event occurred after the calendar was created (ie. 2002, 2003, thru 2007). Can someone confirm that there is not an easier way of getting current events that repeats. Since iCal is already doing this by displaying the current event on the appropriate day on the calendar, is there a way to grab it or must it be computed like Adam said.

Thanks,
Rolin

Model: Intel Mac Mini, PowerMac G5
AppleScript: 1.10.7
Browser: Safari 522.12.1
Operating System: Mac OS X (10.4)

==============================================
note: I removed this script because it does not work properly! The dates it gets were wrong due to the fact that this type of calendar has recurring events and, from what I’ve found through searching, nobody has found a reliable way to calculate recurring events accurately using applescript and iCal.

See if this will help you. It will give you a list of the upcoming holidays in the “sortedHdayList” variable. I made this script by modifying a script I posted here in post #3. Note that thanks goes to Adam Bell for the initial code that went into this.

regulus6633

This helps tremendously. The trick is to copy the event and tell it to set its year to the current year. Your code will work as is. I simply have to look for entries at the beginning of the sorted list with zero days_to_hday. These will be holidays for the current day.

Thank you so much.
Rolin

Yep!!! :cool:

I’m glad it helps. I have a question for you… do you have code for getting the federal holidays from that website? If so, I’d like to see it. I think I could use it. Please post it if you have it!

Well, it didn’t work as I thought. Setting the year on the event object does not miraculously set all of the other date parameters. It simply changes the year. Thus the date of the event does not wrap to the appropriate date for the current year. It looks like a more complicated method is required to calculate a repeating event for the current year.

This works really well and is a lot faster than iterating through all the events in a calendar as i think its done more natively.

set theEvents to every event of calendar theCalendar whose start date is greater than my getSecondsOfDays(days_back)


set this post as i’ve spent a lot of time on scripting iCal.
http://bbs.applescript.net/viewtopic.php?pid=84954#p84954

Thanks for the advice Kim but your approach doesn’t work on this calendar. It must have something to do with the way the repeating events are calculated and stored in the calendar. rolinster touched on the reasons in his original post.

Here’s what I tried…

property calName : "US Holidays"
tell application "iCal"
	set theEvents to every event of calendar calName whose start date is greater than (current date)
	set theData to {}
	repeat with anEvent in theEvents
		set thishday to summary of anEvent
		set thisHdayDate to start date of anEvent
		set end of theData to {thishday, thisHdayDate}
	end repeat
end tell
return theData

results ==> {{“Easter”, date “Sunday, March 23, 2008 12:00:00 AM”}, {“Easter”, date “Sunday, April 12, 2009 12:00:00 AM”}, {“Daylight Savings Time Ends”, date “Sunday, November 4, 2007 12:00:00 AM”}, {“Easter”, date “Sunday, April 4, 2010 12:00:00 AM”}}

You can see that the results make no sense and certainly aren’t indicative of the calendar.

Sorry. I don’t have logic that retrieves the Federal Holiday from that website. I simply used it as a source to determine which US Holidays were federally observed. Then I created a list that had these names as they are referred in the US Holidays calendar so that I can do an exact match. I used this method because I was under the impression that I could easily retrieve the holiday for the current day in the recurring calendar.

Try this:

property theyear : "2007"
property theWeekdays : {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

set temp to ((path to temporary items as Unicode text) & "holiday.txt") -- define tempfile
set Ptemp to quoted form of POSIX path of temp
do shell script "curl [url=http://www.opm.gov/fedhol/]http://www.opm.gov/fedhol/"[/url] & theyear & ".asp -o " & Ptemp
do shell script "textutil -format html -inputencoding iso-8859-1 -convert txt -encoding UTF-16 " & Ptemp -- convert html to txt
set theText to paragraphs of (read file temp as Unicode text)
do shell script "rm " & Ptemp -- delete tempfile

set {holidayList, x} to {{}, 1}
repeat
	try
		if first word of item x of theText is in theWeekdays then exit repeat
	end try
	set x to x + 1
end repeat
repeat
	try
		set end of holidayList to {date (item x of theText & ", " & theyear), item (x + 1) of theText}
	on error
		exit repeat
	end try
	set x to x + 2
end repeat
holidayList

Note: The script works only with a datum format, which accepts Monday, January 1, 2007

Hi StefanK, that’s works great, thanks. I was going to work on it tonight but you saved me the effort. I love how you use that textutil to make parsing the html code so much easier. I wish I would have learned to use that tool long ago.

I wonder why you don’t just pipe the curl results directly into textutil… but I’m sure if you’re not doing it then it’s probably not possible. Anyway. thanks.