Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
create org-contacs entries from vCard input. Only telephone and name so far.
#!/usr/bin/env ruby
# Converts VCard to org-mode/org-contacts files
# 2012 by Simon Thum
# Requires the vpim gem, which in turn needs patches to run (github xing/vpim)
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
require 'rubygems'
gem 'vpim'
require 'erb'
require 'vpim/vcard'
# simple ISO 8601
def isoDate(t)
"%04d-%02d-%02d" % [ t.year, t.month, ]
# put errors to stderr
def putError(err, vc)
warn err
warn err.backtrace
warn "------ (vcard) --------"
warn vc
warn "-----------------------"
# TODO include some richer vCards, normalize number prefix (+49)
OrgContactTemplate = <<-'EOT', nil, "%<>"
<%#-*- coding: UTF-8 -*-%>
** <%= result[:fullName] %>
:TEL: <%= vc.telephones.first %>
:BIRTHDAY: <%= if (vc.birthday.nil?) then "missing" else isoDate(vc.birthday) end %>
<%= vc.to_s %>
# this can be used to fix up stuff before the template processing starts
def evaluateContact(vc)
:fullName =>,
# :bday =>
:tel => vc.telephone.to_s
def orgContactSection(vc)
result = evaluateContact(vc)
rescue StandardError => e
putError(e, ev)
# filter contacts, e.g. see if VCard already in some file
def includeContact?(vc)
vcf = (ARGV[0].nil?) ? STDIN : open(ARGV[0])
cards = Vpim::Vcard.decode(vcf)
puts "* Imported using vcard2org.rb"
cards.each do |vcard|
puts orgContactSection(vcard) if includeContact?(vcard)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment