I’ve recently been on a quest to find a way to gain access to the database that’s used by Apple’s Address Book. I prefer not to have Address Book running all of the time and it is limited in what it offers. If scripters can have full read/write access to the underlying database, without depending on Address Book, it can become a very valuable resource that is available on virtually all OS X Macs. By asking for a command line tool, this opens it up to two basic groups - command line users and scripters/developers. I feel that this is a fair compromise that might be more likely to be considered than asking only for AppleScript access.
At the suggestion of one of Apple’s employees, I’ve submitted a request for a command line tool which would provide full read/write access to Address Book’s database. He said:
If this is something that interests you, please submit a similar request via Apple’s Bug Reporter. An ADC membership (available for free) is required to submit reports. Details can be found in the side bar of the Bug Reporter page. Numerous requests/bug reports move things up the list.
FYI, I’m familiar with contacts. I’ve already started scripting it for quick searches of the AB database but it’s read only. I feel that scripters need full read/write access.
From what I see the problem lies in the fact that the Addressbook DB can only be accessed through Address Book. I could be wrong, but I have found no way to get around this without using Address Book. In fact, my applescript below accesses the addressbook db through Address Book in the command line:
#!/bin/sh
findCode="tell application "Address Book"
set thePerson to person "$2"
set theProps to the properties of thePerson
set the firstName to the first name of theProps
set the lastName to last name of theProps
set the phnList to the value of every phone of thePerson
set testList to firstName & " " & lastName & " " & phnList
return testList
end tell"
addCode="tell application "Address Book"
set thePerson to make new person at end with properties {first name:"$2", last name:"$3"}
make new phone at end of phones of thePerson with properties {label:"$4", value:"$5"}
end tell"
delCode="tell application "Address Book"
delete person "$2"
end tell"
case $1 in
h | help | -h | -help)
echo "Useage:"
echo "To find a Phone Number: phone -f "FirstName LastName""
echo "To add a Name and Phone Number to the phonebook do: phone
add "FirstName" "LastName" "label" "123-456-789""
echo "To remove a user from the phone book do: phone delete "FirstName
LastName""
echo "NOTE: Be sure to use quotes around entries"
;;
f | find | -f | -find)
osascript -e "$findCode"
#if ["$?" -ne "0"]; then
# echo "This name was not found in the Address Book database"
# echo "To add a Name and Phone Number to the phonebook do: phone
add "FirstName" "LastName" "label" "123-456-789""
#fi
;;
a | add | -a | -add)
osascript -e "$addCode"
;;
d | delete | -d | -delete)
osascript -e "$delCode"
;;
esac
Someone else also wrote a binary called contacts which is much more complex than my simpleton script: http://gnufoo.org/contacts/