This is hugely off-topic, so feel free to skip to the next article, which will likely have something to do with screenwriting and/or filmmaking.
My assistant Chad and I use Apple’s iCal to keep track of appointments. It’s nowhere near as sophisticated as Exchange or a real professional calendar system, but for the most part, it works. He maintains “John’s Work” calendar, and I maintain “John’s Personal” calendar. We both use the built-in publish-and-subscribe feature, so we see the same things on each computer.
After doing this for several years, however, some problems have arisen — mostly stemming from syncing with various Palm devices. Calendar events get duplicated, often six or seven times. Multiply that by several years, and the files get huge, and slow: my Work.ics file ballooned to 1.7 megabytes.
After searching the internet for a program that would fix this, I finally had to write my own. In the interest of paying-it-forward to the next guy with the same problem, here’s what I wrote. Continue on only if you’re truly geeky, or desperate.
UPDATE: Changes with OS 10.4 (Tiger), and specifically iCal 2.0, means that the script as written won’t work anymore. Sorry. But the underlying concept still holds. With an hour and a little ambition, it should be possible to eliminate duplicates in the same way. Just be sure to always work on a backup of the calendar file.
If you open up an iCal file in a text editor, you’ll see that it’s actually very simple. Each entry starts with “BEGIN:VEVENT” and ends with “END:VEVENT”. In-between, you see the start and end times (written as 20041210T130000, which means 2004/12/10 at 1:00 p.m.), a summary of the event, and a UID, which is supposed to keep each entry unique. Unfortunately, events sometimes get imported without the UID’s, which means you can end up with seventeen appointments for picking up the dry cleaning last March.
The best (i.e. simplest) solution I could come up with was to simply sort the file based on start times, and eliminate duplicates. This is a little unforgiving; if you happened to have “Coffee with Jen” and “Conference call w/ Paul Brown” listed at exactly the same date and time, one would get overwritten. But that was a longshot I could live with.
Here’s the Python script I wrote. I like Python because it fits my brain, but you could easily translate it to Perl, Ruby or almost anything. (I’d be tempted to do it in AppleScript, but I find it slow at handling big text files.)
!/usr/bin/python
This script removes duplicate entries from an iCal file,
by building a dictionary keyed from the DTSTART value.
Note: if two different entries have the identical start
time and date, one will be overwritten.
Obviously, work on a backup copy of your iCal file.
import sys
replace next line with path to calendar
the_file = open('/Users/john/Library/Calendars/Work.ics')
the_text = the_file.readlines()
the_dict = {} temp = "" id = ""
for line in the_text:
if 'BEGIN:VEVENT' in line:
temp = ""
temp = line
elif 'END:VEVENT' in line:
temp = temp + line.rstrip()
the_dict[id] = temp ### note: overwrites if multiple
elif 'DTSTART' in line:
temp = temp + line
id = line ### use 'DTSTART' as key id value
else:
temp = temp + line
vcalendar headers; replace if yours are different
print 'BEGIN:VCALENDAR' print 'VERSION:2.0' print 'X-WR-CALNAME:Revised Work' print 'PRODID:-//Apple Computer\, Inc//iCal 1.5//EN' print 'X-WR-RELCALID:DC928866-9705-11D9-9E58-000393D00CCE-CALP' print 'X-WR-TIMEZONE:US/Pacific' print 'CALSCALE:GREGORIAN'
use dictionary values; discard keys
for line in the_dict.values(): print line
stardard vcalendar footer
print 'END:VCALENDAR'
You notice that the script reads from a file, but doesn’t write back out. That’s because I’m using BBEdit, and it’s easier just to generate the text file in that.