Locating a particular iCal 2.0.5 .ics file

To synchronise calendars between iCal 1.5.5 on my Jaguar machine and iCal 2.0.5 on my Tiger one, I have a script that overwrites the relevant .ics files on one macine (while iCal’s not running!) with the equivalent files from the other. The iCal on the receiving machine adopts the new files next time it opens, apparently updating its indices in the process.

The problem is in identifying which iCal 2.0.5 .ics file is which. iCal 1.5.5’s .ics files are all in one folder and are named after the calendars themselves. (eg. “Home.ics”.) iCal 2.0.5’s, on the other hand, are all called “corestorage.ics” and are kept in separate folders that have ID-style names which are not the same as the calendars’ UIDs!

I got round this by reading each “corestorage.ics” file in a text editor, identifying the calendar from the events, and setting the comment of the enclosing folder to the name of the equivalent iCal 1.5.5 file on the other machine. The synchronising script identifies what’s what by matching folder comments on one machine with file names on the other and sorts out the appropriate destinations and name changes.

But it’s also possible to set the folder comments by script, since iCal 2.0.5 maintains a .plist file containing both the calendar UIDs and the matching “Source Keys” after which the folders are named. I’ll be using this in future if I add any more calendars!

-- iCal 1.5.5 calendar files: ~/Library/Calendars/(Calendar name).ics
-- iCal 2.0.5 calendar files: ~/Library/Application Support/iCal/Sources/(Source key).calendar/corestorage.ics

-- This script is run by the machine with iCal 2.0.5 to set the comment of each of its
-- (Source key).calendar folders to the name of the corresponding iCal 1.5.5 .ics file.

set iCalAppSupportPath to (path to application support from user domain as Unicode text) & "iCal:"

tell application "iCal" to set c to (count calendars)
repeat with i from 1 to c
	-- Read paired "Key" and "SourceKey" values from the "nodes.plist" file in iCal's Application Support folder.
	tell application "System Events"
		tell property list item i of property list item "List" of property list file (iCalAppSupportPath & "nodes.plist")
			set {keyVal, sourceKeyVal} to {value of property list item "Key", value of property list item "SourceKey"}
		end tell
	end tell
	-- Get the name of the calendar whose uid is the "Key" value.
	tell application "iCal" to set calendarName to (name of first calendar whose uid is keyVal)
	-- Set the comment of the folder whose name begins with the "SourceKey" value to the name of the corresponding iCal 1.5.5 calendar file.
	tell application "Finder" to set comment of folder (iCalAppSupportPath & "Sources:" & sourceKeyVal & ".calendar:") to calendarName & ".ics"
end repeat

It may be that you simply want to access the “corestorage.ics” file pertaining to a particular iCal 2.0.5 calendar:

set iCalAppSupportPath to (path to application support from user domain as Unicode text) & "iCal:"

tell application "iCal"
	activate
	set calendarNames to name of calendars
	set c to (count calendarNames)
	set theChoice to (choose from list calendarNames with prompt "Please select a calendar.")
	if (theChoice is false) then error number -128
	set selectedName to beginning of theChoice
	set calendarUID to uid of calendar selectedName
end tell

repeat with i from 1 to c
	tell application "System Events"
		tell property list item i of property list item "List" of property list file (iCalAppSupportPath & "nodes.plist")
			if (value of property list item "Key" is calendarUID) then
				set sourceKeyVal to value of property list item "SourceKey"
				exit repeat
			end if
		end tell
	end tell
end repeat

set calendarPath to iCalAppSupportPath & "Sources:" & sourceKeyVal & ".calendar:corestorage.ics"

tell application "Finder" to reveal file calendarPath

-- Or:
tell app "TextEdit" to open file calendarPath

-- Or:
set icsText to (read file calendarPath as «class utf8»)