Exporting Calendars with Applescript?

I've got a Python application that I use to do time and project tracking from my iCal calendars; under Panther, I could just straightforwardly configure it to look in ~Library/Calendars for the relevant projects, and all was well. Under Tiger, however, the location of the calendar files has been moved. For various reasons, I'd prefer not to modify the Python application to deal with the new file layout under ~Library/Application Support/iCal/Sources.

It strikes me that the obvious way to handle this is to simply export the calendar files to disk, and in fact this works quite well with File -> Export and putting them in a directory. However, it's somewhat tedious to have to export each calendar individually, which led me to the idea of using Applescript to export them en masse.

The problem is that I can't seem to find any way to actually export them. I've tried doing a 'save thisCalendar in exportFile' (see below), but I get an 'NSReceiversCantHandleScriptError'. Any thoughts on how to make this work properly?

Cheers,

tell application "iCal"
activate

set allCalendars to every calendar

repeat with thisCalendar in allCalendars
set exportFileName to choose file name with prompt "Choose File to Export iCal To:"
save thisCalendar in exportFileName
end repeat
end tell

Posted on Jun 4, 2005 10:54 PM

Reply
14 replies

Jun 5, 2005 10:15 AM in response to jesaurais

Hi,

As no one who knows has turned up, I'll give it a try. I don't have Tiger, but from what I understand this should copy the ics files to your desktop. I am unable to test this script. Possible problems will include that I have not made the correct file paths to the ics files.

Best wishes

John M

-- Copy all ics files in Tiger
-- John Maisey 5/6/5
-- Should copy all iCal ics files from ~/Library/Application Support/iCal to your Desktop
-- * UNTESTED * --
set AppSuppFolder to (path to application support folder from user domain) as text
set myDesktop to path to desktop folder as text
set calArr to {}
tell application "iCal"
-- make an array of {Calendar name, calendar UID}
repeat with myCal in calendars
set end of calArr to {title of myCal, uid of myCal}
end repeat
end tell
tell application "System Events"
repeat with myCal in calArr
-- Build the path to the calendar source
set mySource to (AppSuppFolder & "iCal:" & (item 2 of myCal) & ".calendar:contents:corestorage.ics") as text
-- copy to Desktop and name as Calendar name
duplicate alias mySource to alias myDesktop with properties {name:((item 1 of myCal) & ".ics") as text}
end repeat
end tell
--

Sep 13, 2005 4:14 PM in response to Kristin Jensen1

Hi Kirsten,

This script below works for me.

You'll have to change the first line to something like set theDestination to folder "path:to:folder:" if you want a fixed path. If this is to be a repeated action you'll also need to delete the previous copies.

set theDestination to choose folder with prompt "Where do you want the .ics 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

Sep 13, 2005 8:06 PM in response to John Maisey

Thanks, John!

It works for me also.

Now, it works quickly enough that it's okay to copy all calendars...but I really only need to copy one. Have you figured out whether there's any way to map the calendars in iCal to the appropriate directory in Sources? I suppose I could just loop until I found the correct folder in Sources...it just feels klugey.

I appreciate all your work on iCal scripting; one can only hope that Apple will give us more to work with in the future.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Exporting Calendars with Applescript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.