Skip to content

Instantly share code, notes, and snippets.

@rickychilcott
Forked from christiangenco/parse_abcddb.rb
Created December 15, 2018 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickychilcott/2906dfed3add69f1cb9c771d6d56438c to your computer and use it in GitHub Desktop.
Save rickychilcott/2906dfed3add69f1cb9c771d6d56438c 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
@rickychilcott
Copy link
Author

This is a start...

Find all files that have AddressBook-v22.abcddb, crawl through them and do a query like:

Look in the following tables zabcdemailaddress and zabcdrecord

pluck z_pk,zlastname, zfirstname from zabcdrecord
and merge it with zowner,zaddress from zabcdemailaddress

Take those and store them...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment