public
Last active

create org-contacs entries from vCard input. Only telephone and name so far.

  • Download Gist
vCard2org.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#!/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
# http://creativecommons.org/publicdomain/zero/1.0/
 
require 'rubygems'
gem 'vpim'
require 'erb'
require 'vpim/vcard'
 
# simple ISO 8601
def isoDate(t)
"%04d-%02d-%02d" % [ t.year, t.month, t.day ]
end
 
 
# put errors to stderr
def putError(err, vc)
warn err
warn err.backtrace
warn "------ (vcard) --------"
warn vc
warn "-----------------------"
end
 
# TODO include some richer vCards, normalize number prefix (+49)
OrgContactTemplate = ERB.new <<-'EOT', nil, "%<>"
<%#-*- coding: UTF-8 -*-%>
** <%= result[:fullName] %>
:PROPERTIES:
:TEL: <%= vc.telephones.first %>
:BIRTHDAY: <%= if (vc.birthday.nil?) then "missing" else isoDate(vc.birthday) end %>
:END:
:VCARD:
<%= vc.to_s %>
:END:
EOT
 
# this can be used to fix up stuff before the template processing starts
def evaluateContact(vc)
 
{
:fullName => vc.name.fullname,
# :bday =>
:tel => vc.telephone.to_s
}
end
 
def orgContactSection(vc)
result = evaluateContact(vc)
OrgContactTemplate.result(binding)
rescue StandardError => e
putError(e, ev)
end
 
# filter contacts, e.g. see if VCard already in some file
def includeContact?(vc)
true
end
 
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)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.