Skip to content

Instantly share code, notes, and snippets.

@purp
Created December 15, 2018 21:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purp/05c8f126a5288c43ae65d9a7e5b9c557 to your computer and use it in GitHub Desktop.
Save purp/05c8f126a5288c43ae65d9a7e5b9c557 to your computer and use it in GitHub Desktop.
Convert exported Apple contacts .vcf to CSV

I have a bunch of folks I want to export from Apple Contacts on MacOS and turn into a spreadsheet for a holiday card mail merge.

This is my clumsy way of doing it.

require 'vpim'
require 'csv'
module Vpim
class Vcard
def full_name
name.fullname.to_s
end
class Address
def street_address
("%s, %s %s %s" % [street, locality, region, postalcode]).strip
end
end
def street_address
address ? address.street_address.to_s : nil
end
def email_address
email.to_s
end
def phone_number
telephone.to_s
end
def mail_merge_fields
[full_name, street_address, email_address, phone_number]
end
end
end
output_filename = ARGV[0].nil? ? 'mail_merge_output.csv' : "#{File.basename(ARGV[0])}.csv"
vcf = ARGV[0].nil? ? STDIN : open(ARGV[0])
cards = Vpim::Vcard.decode(vcf)
csv_rows = [["Name", "Address", "Email", "Phone"]] + cards.map(&:mail_merge_fields)
open(output_filename, "w") { |csv|
csv.write(csv_rows.map(&:to_csv).join)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment