Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created June 6, 2014 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christiangenco/3711dc053530eb9179df to your computer and use it in GitHub Desktop.
Save christiangenco/3711dc053530eb9179df to your computer and use it in GitHub Desktop.
Get a csv from an Apple Address Book ".abbu" archive
require 'csv'
require 'pry'
# sqlite3 AddressBook-v22.abcddb
# .headers on
# .mode csv
# .output ZABCDPOSTALADDRESS.csv
# select * from ZABCDPOSTALADDRESS;
# .mode csv
# .output ZABCDRECORD.csv
# select * from ZABCDRECORD;
address_filename = "ZABCDPOSTALADDRESS.csv"
record_filename = "ZABCDRECORD.csv"
# binding.pry
addresses = {}
CSV.read(address_filename, headers: true).entries.map(&:to_hash).map{|h|
addresses[h["ZOWNER"]] = {
city: h["ZCITY"],
state: h["ZSTATE"],
street: h["ZSTREET"],
zip: h["ZZIPCODE"]
}
h
}; nil
people = []
CSV.read(record_filename, headers: true).entries.map(&:to_hash).each{|h|
# binding.pry
address = addresses[h["Z_PK"]]
if address
address = "#{address[:street]}, #{address[:city]} #{address[:state]} #{address[:zip]}"
end
people << {
name: "#{h["ZLASTNAME"]} #{h["ZMIDDLENAME"]} #{h["ZLASTNAME"]}",
address: address
}
}; nil
class Array
def to_csv(csv_filename="hash.csv")
require 'csv'
CSV.open(csv_filename, "wb") do |csv|
csv << first.keys # adds the attributes name on the first line
self.each do |hash|
csv << hash.values
end
end
end
end
people.to_csv('people.csv')
# binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment