Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Fix broken Address Book email addresses after import Apps
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.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[7,187 views]  

Fix broken Address Book email addresses after import | 5 comments | Create New Account
Click here to return to the 'Fix broken Address Book email addresses after import' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Fix broken Address Book email addresses after import
Authored by: tedlandis on Apr 05, '07 06:43:44PM
I didn't test this out, but here is the same logic in python:

import sys
new = open('vcards_fixed.vcf', 'w')
for line in open(sys.argv[1], 'r'):
    if 'item1.TEL;type=pref:' in line and '@' in line:
        new.write(line.replace('EMAIL;type=INTERNET;type=WORK;type=pref:', 'item1.TEL;type=pref:'))
    else:
        new.write(line)


[ Reply to This | # ]
Fix broken Address Book email addresses after import
Authored by: dsymonds on Apr 05, '07 08:46:31PM
Whilst I love Ruby, it's a bit overkill for the task at hand. A single simple sed command will do the trick:

cat bad_file.vcf | sed "s/item1.TEL;type=pref:(.*@.*)/EMAIL;type=INTERNET;type=WORK;type=pref:1/" > fixed_file.vcf

[ Reply to This | # ]

Fix broken Address Book email addresses after import
Authored by: dsymonds on Apr 05, '07 08:51:27PM
... or a more compact Ruby version would be:

File.open("vcards_fixed.vcf", "w") { |f|
    File.readlines(ARGV[0]).each { |line|
        f.puts line.sub(/item1.TEL;type=pref:(.*@.*)/, 'EMAIL;type=INTERNET;type=WORK;type=pref:\1')
    }
}


[ Reply to This | # ]
Fix broken Address Book email addresses after import
Authored by: tedlandis on Apr 06, '07 08:19:25AM

Is the point compactness or readablility, especially for non-programmer types?



[ Reply to This | # ]
Fix broken Address Book email addresses after import
Authored by: maggard on Apr 06, '07 08:36:21PM

Does compactness or readablility matter, especially for non-programmer types?

I’d say how-easy-is-it-to-do, followed by how-easy-is-it-to-repurpose would be most folk’s criteria.



[ Reply to This | # ]