Transforming iCal Calendars with Java
by Daniel H. Steinberg
05/20/2003
Apple is using human readable text files to store the data from some of
its most popular applications. At the same time, Java 1.4.1 on the Mac includes
facilities for regular expressions. If you are a Perl programmer you might
scoff that Java is for sissies. With exceptions and strong typing, Java
makes you say "please" while Perl makes you say "sorry". Each has its
place, but in this article we'll look at how well regex-centric work can
be done in Java.
The "Open" iCal Format
You can store your iCal calendars on your iPod and check your
appointments while enjoying music. But, as Terrie Miller wrote in Goodbye PDA, Hello iPod?,
"to-do items from iCal don't synch." When you use iSynch to bring your
iPod up to date, your iCal events are transferred but not your
todos. Terrie's article was a reminder of why I haven't used my iPod as a
PDA. Most of the items I need to keep track of each day are todos and not
meetings. So, here's the plan: change all the todos to events, and then
they'll synch.
How hard is it to change a todo into an event? Look for ics files in
your home directory's Library/Calendars subdirectory. Open one of
them in a text editor. If you're timid, make a copy and open the copy in
your favorite text editor. Here's a sample calendar opened in BBEdit.
You can pretty much figure out what's going on in the calendar. There
are two events (my dog's birthday and a phone meeting with Derrick) and
two todos (finish this article and lose some weight). Contrast that with
the same information stored in a Microsoft Exchange file. This time inside
of your home directory, open Documents/Microsoft User Data/Office X
Identities/Main Identity/Database in a text editor. Here's a screen shot
of the dog's birthday event.
Tell me whether you can figure that out. Sometimes it takes a nudge or
two to get going in the right direction. The second nudge was Matt
Deatherage's April column on the back page of MacWorld magazine. The bar
was set high for Matt as he was displacing Andy Ihnatko, one of the only
Mac columnists worth reading. But in that first and in the subsequent
articles Matt has gotten the wrong end of the stick in issues that have
been dealt with long ago by people that actually understand them. The
April article was taking Apple to task for pretending to have open formats
where they really don't. The prime example used in the article was
Keynote, Apple's presentation software.
I almost wrote this article about Keynote but the red herring was that
the file format is XML with a published DTD. What makes Keynote accessible
isn't so much that it is written in XML or that Apple has released its
schema. The Omni group had written functionality into their outlining
software that allowed you to transform an outline into a Keynote
presentation before Apple published the schema. What makes Keynote and
iCal accessible is that the files are stored as human readable plain text
as Andy Hunt and Dave Thomas recommend in The Pragmatic
Programmer. This is a book you should own and reread regularly.
Look at the first calendar image above. If the iCal format later
changes or the product is no longer supported, we can still read the
calendar file and easily construct software that can use that format as
input. If Microsoft changes the Entourage format, it is unlikely that we
can do much with the binary format of the second file. Sure we can pour
over it and extract little pieces of text and try to reconstruct a
schedule, but it won't be easy.
This article presents an example of how software can be written to work
with an unfamiliar but semi-accessible format. Sure, if Apple changes the
structure of iCal files, this application will break. The expectation is,
however, that we will be able to fix it.
Here's the plan. Take an iCal file and read it from our disk into a
String. Next, use the regular expression facilities now
available on your Mac as part of Java 1.4 to change the todo items in our
calendar to events. Third, take the altered calendar and save it under a
different name so as not to overwrite the existing calendar.
This code is presented as an example. Do not use it on data for
which you don't have a copy. It hasn't been widely tested. Consult a
professionally trained computer scientist or a twelve year old child
before attempting anything difficult on your own machine..