Search Address Book from Terminal
Feb 01, '10 07:30:00AM
Contributed by: Anonymous
I have a need (like for Mutt's query_command) to search for email addresses from OS X's Address Book.app, but from the command line. After trying various hints, code, etc., and getting frustrated, I found that it's just stored in sqlite3. Everything is already built into OS X, you just have to figure out the SQL structure. I've only tested this on 10.6, but I imagine it will either work as is, or be easily adapted to work, on 10.5.
I created a shell script called abook.sh in /usr/local/bin with these contents:
#!/bin/sh
sqlite3 -separator ' ' ~/Library/Application\ Support/AddressBook/AddressBook-v22.abcddb "select e.ZADDRESSNORMALIZED,p.ZFIRSTNAME,p.ZLASTNAME,p.ZORGANIZATION from ZABCDRECORD as p,ZABCDEMAILADDRESS as e WHERE e.ZOWNER = p.Z_PK;" | grep $1
Note: The -separator ' ' bit is a Tab character (use Control-V, Control-I) to insert one in bash. You can make it a few spaces or whatever you want, but if you are using this as Mutt's query_command, then it must be a Tab character.
The above code dumps more than one email address per contact (if there is more than one), and one email address per line -- so this isn't useful for importing into anything else. To mess about with the SQL, use sqlite3 and try the .dump command to get a feel for what the schema is like. That's it! I hope this helps someone out there in the world.
[robg adds: If you create the above shell script, remember to make it executable (chmod a+x scriptname), and call it with the name you'd like to find: abook.sh Smith. We covered some other sqlite tricks for Address Book in this previous hint.]
Comments (5)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20100130052524548