All,

I don't remember who who asked the question about a script that would take contact data and convert it into a vCard so the (OS X) address book could import them. Thanks to a train delay (dam trains!) I had sometime and wrote a simple script that converts contact data in CSV format into vCards for the address book. I thought I'd share:

########################################################################
# Beginning of script
########################################################################

#! /usr/bin/perl

use strict;

# This function turns a list of contact data into a vCard. It
# expects the fields in the following order: Lastname, Firstname,
# E-Mail, Office Phone, Cell, Home, Pager. This will create a vCard
# compatible with Apple's Address book.
sub TurnToVcf {
    my($out);
    my($org_name) = 'Pepper Hamilton LLP';

    # Place the `Begin` and `Version` statement on the vCard
    $out = "BEGIN:VCARD\nVERSION:3.0\n";

$out .= "N:$_[0];$_[1];;;\n";
$out .= "FN:$_[1] $_[0]\n";
$out .= "ORG:$org_name\n";
$out .= "EMAIL;type=INTERNET;type=WORK;type=pref:$_[2]\n";
$out .= "TEL;type=WORK;type=pref:$_[3]\n";
$out .= "TEL;type=CELL:$_[4]\n" if (!$_[4] eq 'N/A') or (!$_[5] eq '');
$out .= "TEL;type=HOME:$_[5]\n" if (!$_[5] eq 'N/A') or (!$_[5] eq '');
$out .= "TEL;type=PAGER:$_[6]\n" if (!$_[6] eq 'N/A') or (!$_[6] eq '');


    # Place the end statement on the vCard
    $out .= "END:VCARD\n";

    # Return the vCard
    return $out;
}

my($src_fn, $trg_fn, $flag, @contacts);

# If no source file was provided, the program will die.
# The 0/1 flag indicates if output is to Outlook or Address Book
# 0 is for OS X and 1 is for Outlook.
die ("Usage: $0 [source_file] [0|1]\n") if (@ARGV < 1);

# Identify the source and target file names and assign the default flag.
$src_fn = shift(@ARGV);
$flag = (@ARGV[0] == 1) ? 1: 2;
$trg_fn = substr($src_fn, 0, length($src_fn) - 4) . '.vcf';

if (-e $src_fn) {
    # Gather contact data from the source file.
    open (SRC, $src_fn) or
        die ("Cannot open the source file $src_fn.\n");

while (<SRC>) {
# Source file must provide contact information in the following order
# - Lastname, Firstname, E-Mail, Office Phone, Cell, Home, Pager


chomp;

        # Store the contact data in the @contacts table.
        my(@temp) = split(/,/);
        push(@contacts, [EMAIL PROTECTED]);
    }

close(SRC);

    # Export data to vCard
    if ($flag == 1) {
        my ($capsule) = 'Exported_vCards';

        mkdir($capsule) or
            die ("Couldn't make directory for out vCards ($!)\n.");

        chdir($capsule) or
            die ("Can't move the the output directory ($capsule)\n");

# For each contact create a vcf file and export contacts.
for (my($idx); $idx < $#contacts + 1; ++$idx) {
open (TRG, ">${$contacts[$idx]}[0]_${$contacts[$idx]}[1]-$trg_fn") or
die("Could not create the output file ($trg_fn).\n");


            print (TRG TurnToVcf(@[EMAIL PROTECTED]));
        }
    } else {
        # Place all contacts into a single vcf file.
        open (TRG, ">$trg_fn") or
            die("Could not create the output file ($trg_fn).\n");

        print (TRG TurnToVcf(@{$_})) for (@contacts);
    }

    # Close the vcf - one will always be open.
    close (TRG);

    # Let the operator know how many contacts were processed.
    print ("There are " . @contacts . " processed (see $trg_fn).\n");
} else {
    # Can't confirm the source file exists, raise an error.
    die ("The specified source, $src_fn, file does not exist.\n");
}

exit(0);

########################################################################
# End of script
########################################################################
  • Forking Help Thane
    • Turn Contacts in CSV file into vCards for Address Book Adam

Reply via email to