Fix broken Address Book email addresses after import

Apr 05, '07 07:30:00AM

Contributed by: whenders0n

My father lost his addresses on his laptop, and proceeded to import all his contacts from his Treo. Trouble was, none of the email addresses were showing up in Mail.app. Upon closer inspection, I discovered that Address Book had foolishly decieded that all these email addresses were phone numbers, and hence, they were not appearing as such.

Now how to fix this for over 500 contacts? ruby to the rescue! First, I created a smart group of cards whose phone number contained "@". These were the culprits. Next, I backed up Address Book's data, just in case. Then I exported all the contacts in this smart group to a vcard, and then deleted them. Then I wrote this little ruby script to fix everything:

new = File.open("vcards_fixed.vcf", "w")
File.open(ARGV[0], "r").each { |line|
  if ((line.include? "item1.TEL;type=pref:") && (line.include? "@"))
    new.puts "EMAIL;type=INTERNET;type=WORK;type=pref:" << (line.delete "item1.TEL;type=pref:")
   else
     new.puts line
    end
  }
new.close

(You can also download it via this link.) Stick the script in the same directory as the vcard and run it using:
ruby fix_addresses.rb vCards.vcf
This assumes you exported the file as vCards.vcf, of course. The script goes through all the lines and changes the broken email addresses, and then writes a new vcf called vcards_fixed.vcf. Once you've run the script, import the new vcf to a (non-smart) group, and make sure everything is OK. If all went well, your email addresses will now work.

Note that it is possible that your contacts are broken in a slightly different way (maybe a different phone or something), and so you might want to open up the original vCards.vcf file as a text file and look to see if the email address lines begin with item1.TEL;type=pref: -- because this is what the script looks for. You can trivially modify the script if this is the case.

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070403104653786