Contacts.app command line interface

Is there a CLI front-end to the Contacts.app (formerly Address Book.app) database?

Example:

mycontacts --email '*@google.com' # to get Googlers

Solution 1:

The closest thing to a generic solution is the CLI package contacts by Shane Celis. The source can be found on GitHub, and is also available as a formulae on Homebrew. The homebrew package can be installed using with the following command:

brew install contacts


There are other options that may also work for you. Contacts just stores info in a sqlite database so you can access it with the sqlite command. You could create a custom script to accomplish a particular task if you want.

Scott Stevenson had a tool called abtool up that was popular for a while, so you could try searching through the source on GitHub. In particular, since the app by Shane Celis appears unmaintained (while still functioning well), there are some who are building replacements.

Solution 2:

Contacts still has AppleScript support. You can see documentation for the supported properties and commands by opening its dictionary in AppleScript Editor.

osascript -e 'tell app "Contacts" to properties of people where vcard contains "@gmail.com"'

I didn't find a way to check the actual email fields using a single specifier.

set l to {}
tell application "Contacts"
    repeat with p in people
        repeat with e in (get value of emails of p)
            if contents of e ends with "@gmail.com" then set end of l to name of p
        end repeat
    end repeat
end tell
l