Cleaning up your Address Book on OS X with Ruby

Lately I’ve been working on mailing out wedding invitations. The easiest way to print address labels on OS X is to select contacts or a group in Address Book, hit print and change the Page option to “Avery Standard” then select the label type. Easy. The hard part is getting all those addresses in there in the first place and then organizing them. My problem was that I imported things incorrectly a number of times and filled my address book with 1500 or so broken contacts. That made it really hard to call someone because I had 15 very similar looking entries for each person. Fortunately all the addresses I imported were missing phone numbers, so I knew I could group them together easily (with a smart group) but how could I script the name edits like “Add ‘& Guest’ if the name doesn’t include an &”? Not to mention I didn’t want to end up with contacts like “Joe Smith” next to one named “Joe Smith & Guest”!

First I tried to do this with AppleScript and 25 minutes into it had something working, but working with AppleScript is so strange, I needed ruby! In 10 minutes I had the following script written and now I could also use ruby to push/pull/stretch and tweak all the data I have in Address Book already, yay.

Here is the AppleScript version of the same script:

Here is a great reference for how to work with Address Book from AppleScript that you can combine with the rb-appscript guide on commands to add new contacts, etc. Now I’ll leave it up to you to go write a script to update your contacts picture in Address Book from their facebook, linked in, gravatar or other photo. Or perhaps see if they have a DandyID and add all their associated accounts to their contact entry.

Leave a Reply