February 18, 2010 at 4:02 PM by Dr. Drang
I coach my younger son’s YMCA basketball team and use email to send updates and reminders to the other parents. Today my wife needed that list of addresses to coordinate an after-game dinner with the parents of a (friendly) rival team. My first thought was to export the list as a vCard file from my Address Book and email it to her, but importing that into her Address Book would have led to several duplicates and more work for her to weed them out. Also, she had no interest in the phone numbers and other contact information I have for some of these parents; she just wanted the email addresses.
So I went ahead and did the vCard export, and typed up this simple Python filter:
1: #!/usr/bin/python 2: 3: contacts = open('/Users/drang/Desktop/contacts.vcf') 4: 5: for line in contacts: 6: if line[:3] == 'FN:': 7: print line[3:], 8: if line[:6] == 'EMAIL;': 9: colon = line.find(':') 10: print line[colon+1:]
The name and path to the vCard file is in Line 3. I had the vCard file open in TextMate as I wrote the script. The lines with the important data looked like this:
FN:Ms. Laura Ipsum EMAIL;type=INTERNET;type=HOME;type=pref:email@example.com
Lines 5-10 were written with this format in mind. The output was a list of names and addresses
Ms. Laura Ipsum firstname.lastname@example.org Ms. Dolores Amet email@example.com Ms. Elizabeth Consectetur firstname.lastname@example.org
which I copied into a email to my wife. Five minutes of effort, maybe, with some interruptions.
I’m sure the script won’t handle every situation, but that’s OK. It was easy to write and it got me what I wanted quickly. I didn’t even save the script. I ran it within TextMate, using the Run Script command (⌘R) in the Python bundle. When I was done, I kept the script in an open window until I wrote this blog post around it.