Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Search Address Book from Terminal UNIX
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.]
    •    
  • Currently 2.31 / 5
  You rated: 5 / 5 (16 votes cast)
 
[7,662 views]  

Search Address Book from Terminal | 5 comments | Create New Account
Click here to return to the 'Search Address Book from Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Search Address Book from Terminal
Authored by: mankoff on Feb 01, '10 08:37:08AM

The "contacts" program (available via fink) would also probably meet your needs.

---
http://kenmankoff.com



[ Reply to This | # ]
contacts URL
Authored by: jecwobble on Feb 01, '10 09:49:25AM
If you don't have/use fink, you can go to http://www.gnufoo.org/contacts/contacts.html

[ Reply to This | # ]
Search Address Book from Terminal
Authored by: kenahoo on Feb 01, '10 07:44:20PM

I can't be the first to wonder: what bizarre person put 'Z's all over the place in the table & column names in this schema? And what's with "ABCD" all over the place?



[ Reply to This | # ]
Search Address Book from Terminal
Authored by: wallybear on Feb 02, '10 02:10:20AM

That's the convention used by Address Book (AB) for its fields.



[ Reply to This | # ]
Search Address Book from Terminal
Authored by: Anonymous on Feb 02, '10 03:55:04PM

I have to wonder if the tab character can be abstracted with something like "\t". That way you don't need a warning about making sure it's a tab character...

Not at a Mac, so I can't test.



[ Reply to This | # ]