Duplicating & Renaming iCal Calendars For Web Server

I’m trying to publish my iCal calendars to my web server, on the same machine. They’ll be interpreted by PHP iCalendar. I tried symlinking the files, but that didn’t work. I tried regular aliases, and that didn’t work. Then I tried setting up WebDAV, but I think that because I have my IP address routed to a specific folder, that didn’t work either. So as a last resort, I’m trying to set a Folder action to copy the files over any time the folder is modified. Unfortunately, my skills leave something to be desired.

tell application "Finder"
	set file_path to ":Users:walter:Library:Application Support:iCal:Sources:ACC6C983-0C02-4B22-99DC-203B597F452E.calendar:corestorage.ics"
	set receiving_folder to ":Library:WebServer:Documents:home:schedule:calendars:"
	set dupe_ to duplicate file file_path to receiving_folder with replacing
	set name of dupe_ to "School.ics"
end tell

Can someone help me out? This returns…

Finder got an error: Can’t set “:Library:WebServer:Documents:home:schedule:calendars:” to file “:Users:walter:Library:Application Support:iCal:Sources:ACC6C983-0C02-4B22-99DC-203B597F452E.calendar:corestorage.ics”.

You’re getting fouled up by path formats. Anytime you’re scripting the finder, use “:” delim’d paths. These start with a volume name…

“Macintosh HD:Users:walter:Library:Application Support:iCal:Sources:ACC6C983-0C02-4B22-99DC-203B597F452E.calendar:corestorage.ics”

…for example.

If you’re going to use paths in a shell script command, like cp, use “/” delim’d paths (posix format), which don’t have drive names like…

“Users/walter/Library/Application Support/iCal/Sources/ACC6C983-0C02-4B22-99DC-203B597F452E.calendar/corestorage.ics”

I’m not familiar with it, but I’m surprised phpical didn’t follow your symlink. Maybe there’s a pref you can set or some way to edit the php so it will use the .ics file in your users dir?

That worked, thanks!

I was surprised too. I thought about trying to configure the script to read calendars in the folder iCal keeps them in, but iCal separates each calendar into a folder with some weird serial number as the name, then names the calendar “corestorage.ics”. PHP iCalendar names the calendars on the webpage according to their actual file names, so it would have to not only read all calendars in subdirectories but also figure out a way to rename them…

Hi Walter,

You might be interested in this script which moves all your Tiger iCal .ics files to the folder of your choice and renames them from the calendar name in their ‘info.plist’ file.

set theDestination to choose folder with prompt "Where do you want the files?"
set myPath to alias ((path to application support from user domain) & "iCal:Sources:" as string)
tell application "Finder"
	set myFolders to (folders of folder myPath)
	repeat with myFolder in myFolders
		set thePath to ((myFolder as alias) & "info.plist") as text
		set theXML to read file thePath
		repeat with myPara from 1 to count of paragraphs of theXML
			if paragraph myPara of theXML contains "Title" then set theName to (characters 10 thru -10 of (paragraph (myPara + 1) of theXML) & ".ics") as string
		end repeat
		set myDestination to duplicate file "corestorage.ics" of myFolder to theDestination
		set name of myDestination to theName
	end repeat
end tell

Best wishes

John M

John… that’s amazing. I wouldn’t have even thought a script like that was possible.

Now if only I could get it to replace existing files if they’re there (I might be able to swing that on my own) and to run every time I quit iCal (no idea - Folder Actions doesn’t support on file change or on file update, does it?) then I’d be set.

The part where the duplicated ics file is being renamed is probably failing.

You could check for exists on the new file name to see if there’s an old version of the file sitting in the dir and then delete it. Then the rename command will have clear sailing.

Don’t know much about folder actions, but you could schedule a cron job to run periodically to check for changes.

p.s. Thanks for that cool script, John M.

This script deletes the destination file if it exists. If you want to update on a regular basis try Cron, or you could set up a hidden calendar in iCal with events to run the script.

set theDestination to choose folder with prompt "Where do you want the files?"
set myPath to alias ((path to application support from user domain) & "iCal:Sources:" as string)
tell application "Finder"
	activate
	set myFolders to (folders of folder myPath)
	repeat with myFolder in myFolders
		set thePath to ((myFolder as alias) & "info.plist") as text
		set theXML to read file thePath
		repeat with myPara from 1 to count of paragraphs of theXML
			if paragraph myPara of theXML contains "Title" then set theName to (characters 10 thru -10 of (paragraph (myPara + 1) of theXML) & ".ics") as string
		end repeat
		if (name of files of theDestination) contains theName then delete file ((theDestination & theName) as string)
		set myDestination to duplicate file "corestorage.ics" of myFolder to theDestination
		set name of myDestination to theName
	end repeat
end tell

Best wishes

John M

For some reason, I started getting an error with this. Before I changed to the revised script. On the line… “set myDestination to duplicate file “corestorage.ics” of myFolder to theDestination”, it returns the error: “Finder got an error: The operation could not be completed”. Is this a privileges problem? Why would it work a few times and then stop working after that? Removing the files from the destination folder seems to make no difference.

Hi Walter,

I’m just guessing here. The Finder can be slow in carrying out commands sometimes (if it’s busy doing something else) and doesn’t do things in the order expected. It may be that one file has not been renamed from “corestorage.ics” before the next “corestorage.ics” file is made. Try adding a delay 1 command after set name of myDestination to theName.

John M