Wednesday, November 19, 2014

How to export Mac's Address Book contents to simple kind-of-human-readable dump

Recently I wanted to take a dump of Mac’s Address Book contents. Just the names & addresses of my contacts in a human-readable format.

Some background

First, my Address Book version is 5.0.3. It has the functionality File → Export -> Address Book Archive. This command generates a directory that contains a file with .abcddb postfix that is a SQLite file. (In my case, the file name was AddressBook-v22.abcddb)

After that, I found the blog post AddressBook entries from a Mac using SQLite that has the needed SQL query with the columns and the joins sorted out.

To get the output to a file, a couple of SQLite commands were found from How to export the results of my query to csv file.

The result

The end result to be run with sqlite3 (a SQLite command-line utility). For all the possible columns, check the blog post linked above.

.headers on
.mode csv
.output export.csv

SELECT DISTINCT
ZABCDRECORD.ZFIRSTNAME [FIRST NAME],
ZABCDRECORD.ZLASTNAME [LAST NAME],
ZABCDPOSTALADDRESS.ZSTREET [STREET],
ZABCDPOSTALADDRESS.ZZIPCODE [ZIPCODE],
ZABCDPOSTALADDRESS.ZCITY [CITY]

FROM ZABCDRECORD
LEFT JOIN ZABCDPOSTALADDRESS on 
  ZABCDRECORD.Z_PK = ZABCDPOSTALADDRESS.ZOWNER
LEFT JOIN ZABCDNOTE ON 
  ZABCDRECORD.Z_PK = ZABCDNOTE.ZCONTACT

ORDER BY
ZABCDRECORD.ZLASTNAME,
ZABCDRECORD.ZFIRSTNAME;


.output stdout

Discussion

Note that entries containing newlines might need some cleanup (google for “sqlite replace newline”, for example) but that wasn’t needed in my case.

No comments: