Skip to content

Instantly share code, notes, and snippets.

@purp
Created December 15, 2018 21:46
Show Gist options
  • Select an option

  • Save purp/05c8f126a5288c43ae65d9a7e5b9c557 to your computer and use it in GitHub Desktop.

Select an option

Save purp/05c8f126a5288c43ae65d9a7e5b9c557 to your computer and use it in GitHub Desktop.

Revisions

  1. purp created this gist Dec 15, 2018.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    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.
    43 changes: 43 additions & 0 deletions apple_contacts_to_csv.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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)
    }