Importing Contacts into an iPhone in Bulk

So I had a problem; when I left my previous employer, I had been using my iPhone to interface with work email.  That worked great.  But one problem that I face after I left was that all my contacts were in the Active Directory/Exchange address book system.  I asked for, and received, a CSV of my contact info when I left.

Then this sat for a while, for months, while I didn’t do anything about it.  I grabbed the occasional contact out of it I needed, but it was a pain in the ass.  So what I did to really fix this problem was a bit of hacking.  Turns out that the iPhone will import a bulk vCard address book.  And it turns out that vCard is a documented standard with easy perl modules for using it.  So I did what I often do:  I wrote a tool to solve my problem.  This tool is pretty simple — it takes a pipe separated “CSV” file, reads it in, and creates a vcard address book, then outputs that to STDOUT.

An example entry from the file would be:

Gabriel|Cain|||(206) 123-4567||

And what follows is the script I wrote to deal with this problem.


use strict;
use Text::vCard;
use Text::vCard::Addressbook;

my @contacts;
open(F,"<$ARGV[0]") or die("Can't open $ARGV[0]: $!\n");
foreach my $row (<F>) {
 my $index = 0;
 my %c = (); my @p = split /\|/, $row;

 # Skip rows that don't have enough columns
 next if (scalar @p != 7);
 foreach (qw/first last company home mobile email1 email2/) {
 $c{$_} = $p[$index]; $index ++;
 push @contacts, \%c;

# Create a new address book
my $address_book = Text::vCard::Addressbook->new();

foreach my $r (@contacts) {
 my %c = %{$r};
 my $vcard   = $address_book->add_vcard();

 $vcard->fullname( "$c{first} $c{last}" );
 $vcard->email( $c{email1} );

 my $name = $vcard->add_node({'node_type'=>'N'});
 $name->family( $c{last} );      $name->given( $c{first} );

 my $mobil = $vcard->add_node({'node_type'=>'TEL'});
 $mobil->add_types('cell');      $mobil->value($c{mobile});
 $mobil = $vcard->add_node({'node_type'=>'TEL'});
 $mobil->add_types('home');      $mobil->value($c{home});

print $address_book->export();

So that’s the solution I took to importing my contacts into my iPhone using vCards.  I’m pretty happy with it.  Just write the output of this script to a file, and email it to your phone.  Attached will be a vcard address book that you can import all the contacts from.  Pretty spiffy.

Be Sociable, Share!