Drew Hamlin

    February 25, 2007

Removing Photos from vCards

Drew Hamlin: 9:00 PM
Removing Photos from vCards

I was faced with a challenge the other day:
How do you quickly remove photos from a multiple-entry vCard file?

For those familar with regular expressions, here's my crafted answer:
PHOTO;BASE64:(\s+[=+/\w]+)+\s

Here's the context of the question:
Facebook just added a new feature called Find Your Friends that lets you easily find people on Facebook who you already know in real life. It's especially useful for new users just signing up, but relevant for anyone.

(By the way, if you haven't heard of Facebook, sign up. It's quite simply the best way to keep in touch with friends. Think MySpace, except good.)

Actually, Find Your Friends isn't entirely new, but they just added the ability to import your contacts from a vCard file, which makes it useful for anyone who uses Mac OS X's Address Book to store their contacts.

The problem is that Facebook only lets you upload a file that's 500KB — and when your Address Book has lots of photos, it's going to be much, much bigger (10.5MB, in my case).

Here's the full solution:
  1. Export your vCard file.
    Select all your Address Book entries and drag them to the desktop.
  2. Open the vCard in your favorite modern text editor.
    May I recommend TextMate or SubEthaEdit.
  3. Look at the content. Is there an empty line after every line?
    If not, skip this step. (If you do it, you'll just get gibberish.)
    If so, it means that one or more of your entires contained a non-basic character, like an accent character, so the file is UTF-16.
    TextMate: FileRe-Open With EncodingUTF-16 (Big Endian)
    SubEthaEdit: FormatFile EncodingsUnicode (UTF-16); choose Reinterpret
  4. Open the Find window and paste in the magic incantation.
    This goes in the Find field. Leave the Replace field empty:
    PHOTO;BASE64:(\s+[=+/\w]+)+\s
  5. Check the box for Regular Expression / RegEx.
  6. Replace All and Save.
Mission accomplished: you've got a much smaller vCard file that contains no images, perfect for uploading to Facebook's Find Your Friends.

Of course using regular expressions sometimes feels like just one step up from compiling your own kernel... so I've filed an Apple bug report (rdar://5022380) suggesting that this really should be an Address Book feature in the form of a preference checkbox.

Update 2/27: Tweaked the expression to reach a corner case.
Update 12/13: Apple listened. In Leopard, they implemented this feature exactly the way I proposed it. You can find it in the vCard tab of Address Book preferences.
2 Comments:
Blogger Nic: March 05, 2007 6:40 PM 
I just had to solve this exact problem and remembered your blog post, so I coped in the regular expression and... no match found?

I was getting really confused until I realized that SubEthaEdit was interpreting the file as UTF-8. So of course, I couldn't match anything longer than one character. Whoops.

Anyways, the regex worked really well, but I added \n? to the end just for aesthetic value. No point leaving blank lines behind.
Blogger Mark McLaughlin: April 16, 2007 11:23 AM 
Great stuff - works an absolute treat.

The very idea of having to figure out a RegEx myself was starting to make my brain hurt. Thanks!
Post a Comment