• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

John August

  • Arlo Finch
  • Scriptnotes
  • Library
  • Store
  • About

Removing duplicate iCal entries

March 19, 2005 Geek Alert

geek alert
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](http://www.apple.com/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](http://www.palmone.com/us/) 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](http://www.python.org/) 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](http://www.macosxautomation.com/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](http://www.barebones.com/products/bbedit/index.shtml), and it’s easier just to generate the text file in that.

Related Posts

  1. Celtx screenwriting application shows promise
  2. When Final Draft won’t open under OS X
  3. Rewriting, but where’s the script?

Primary Sidebar

Newsletter

Inneresting Logo A Quote-Unquote Newsletter about Writing
Read Now

Explore

Projects

  • Aladdin (1)
  • Arlo Finch (27)
  • Big Fish (88)
  • Birdigo (2)
  • Charlie (39)
  • Charlie's Angels (16)
  • Chosen (2)
  • Corpse Bride (9)
  • Dead Projects (18)
  • Frankenweenie (10)
  • Go (30)
  • Karateka (4)
  • Monsterpocalypse (3)
  • One Hit Kill (6)
  • Ops (6)
  • Preacher (2)
  • Prince of Persia (13)
  • Shazam (6)
  • Snake People (6)
  • Tarzan (5)
  • The Nines (118)
  • The Remnants (12)
  • The Variant (22)

Apps

  • Bronson (14)
  • FDX Reader (11)
  • Fountain (32)
  • Highland (73)
  • Less IMDb (4)
  • Weekend Read (64)

Recommended Reading

  • First Person (88)
  • Geek Alert (151)
  • WGA (162)
  • Workspace (19)

Screenwriting Q&A

  • Adaptation (66)
  • Directors (90)
  • Education (49)
  • Film Industry (492)
  • Formatting (130)
  • Genres (90)
  • Glossary (6)
  • Pitches (29)
  • Producers (59)
  • Psych 101 (119)
  • Rights and Copyright (96)
  • So-Called Experts (47)
  • Story and Plot (170)
  • Television (165)
  • Treatments (21)
  • Words on the page (238)
  • Writing Process (178)

More screenwriting Q&A at screenwriting.io

© 2025 John August — All Rights Reserved.