Tuesday, October 18, 2005

Importing vCard Contacts To Thunderbird

Personally Evolution is not my favourite mail program. The only reason why I have been sticking to it was it’s ability to import vCard contacts. And I have had a LOT of those :)

Considering how good Firefox is I was thinking about switching to Thunderbird. Unfortunatelly lack of ability to import vCards was stopping me. Untill today that is.

I have stumbled upon a small Ruby library for handling vCards. Writing a script that will convert my exported contacts to CSV file was just a matter of minutes.

To use it you will need to download and install Vpim library. Installation is quite simple:


$ ruby install.rb config
$ ruby install.rb setup

Switch to root privileges and invoke:


# ruby install.rb install  

Now for the script itself (file vcard-to-csv.rb):


#!/usr/bin/env ruby

require 'vpim/vcard'

cards = Vpim::Vcard.decode(ARGF.read)

cards.each do |card|
  surname, name = card['N'].split(';')
  full_name = card['FN']

  # Depending on contact Evolution used different types for email field
  # In my case order of importance for emails was as below.
  # Luckily no contact have had more than two emails
  email_other = card['EMAIL', 'OTHER']
  email_work = card['EMAIL', 'WORK']
  email_home = card['EMAIL', 'HOME']
  emails = []
  [email_other, email_work, email_home].each {|email| email && emails << email}

  tel_home = card['TEL', 'HOME']
  tel_work = card['TEL', 'WORK']
  tel_mobile = card['TEL', 'CELL']

  # Full list of fields:
  #
  # first,last,display,nickname,email,add email,work phone,home phone,fax,pager,mobile,address,address2,city,state,zip,country,address work,address work2,city work,state work,zip work,country work,titile,departament,organization,http://web page work,http://web page,,,,custom 1,custom 2,custom 3,custom 4,notes,
  puts "#{name},#{surname},#{full_name},,#{emails[0]},#{emails[1]},#{tel_work},#{tel_home},,,#{tel_mobile},,,,,,,,,,,,,,,,,,,,,,,,,," 

end

To use it export contacts from Evolution to contacts.vcf file and from shell type:


  $ ruby vcard-to-csv.rb contacts.vcf >contacts.csv

Now you can import contacts.csv file into Thunderbird.

1 comments:

Jorge Juan said...

Very nice script.

Here you are an improved version (actually a hack) including more fields:

#!/usr/bin/env ruby

require 'vpim/vcard'

cards = Vpim::Vcard.decode(ARGF.read)

cards.each do |card|
surname, name = card['N'].split(';')
full_name = card['FN']
nickname = card['NICKNAME']

# Depending on contact Evolution used different types for email field
# In my case order of importance for emails was as below.
# Luckily no contact have had more than two emails
email_internet = card['EMAIL', 'INTERNET']
email_work = card['EMAIL', 'WORK']
email_home = card['EMAIL', 'HOME']
email_other = card['EMAIL', 'OTHER']
emails = []
[email_internet, email_work, email_home, email_other].each {|email| email && emails << email}

tel_home = card['TEL', 'HOME']
tel_work = card['TEL', 'WORK']
tel_fax = card['TEL', 'FAX']
tel_mobile = card['TEL', 'CELL']
address = card['ADR', 'HOME']
address = address.gsub(/\\,/,'.') if address.kind_of?(String)
address_work = card['ADR', 'WORK']
address_work = address_work.gsub(/\\,/,'.') if address_work.kind_of?(String)

title = card['TITLE']
organization = card['ORG']
organization = organization.gsub(/\\,/,'.') if organization.kind_of?(String)


url = card['URL']
notes = card['NOTE']

# Full list of fields:
#
# first,last,display,nickname,email,add email,work phone,home phone,fax,pager,mobile,address,address2,city,state,zip,country,address work,address work2,city work,state work,zip work,country work,title,departament,organization,http://web page work,http://web page,,,,custom 1,custom 2,custom 3,custom 4,notes,
puts "#{name},#{surname},#{full_name},#{nickname},#{emails[0]},#{emails[1]},#{tel_work},#{tel_home},#{tel_fax},,#{tel_mobile},#{address},,,,,,#{address_work},,,,,,#{title},,#{organization},,#{url},,,,,,,,#{notes},"

end