r/Python•
aasmodeus

How do I insert entries into OSX AddressBook?

I'd like to use Python to insert a record into my local OSX AddressBook. There's a lot of information already out there about using the appscript module (need to learn AppleScript), and the AddressBook module (need to learn ObjC API). There's a lot of information, that is, on how to extract information. I haven't seen a clean code example for creating a new record and inserting that into AddressBook. The closest was someone's absync script, but I am unable to getattr(ab_person, "First") so I'm not sure if that script as written is compatible with 10.6.8's included modules.

Posted Sep 16, 2011, 9:26 PM UTC

914
AcademGreen•

This might help you get started using the appscript module.

from appscript import *

addbook = app('Address Book')
newperson = addbook.make(new=k.person, with_properties={k.first_name:'foo', k.last_name:'bar'})
newperson.phones.end.make(new=k.phone, with_properties={k.label:'work', k.value:'555.555.5555'})
addbook.save()

Any additional contact properties can be found by looking at the "person" entry in the Address Book's dictionary in the AppleScript editor (under the file menu). The only difference is that you should put underscores in the place of spaces and prefix property keys with k. Edit: Add example to add a phone number to a person.

6
aasmodeusOP•

Thanks, those newperson examples are exactly what I've been searching for! May I ask how you get from all the Class descriptions to newperson.phones.end.make() and how you determine the new= and with_properties=? While attempting to use the AddressBook module, I had searched through the Programming Guide (not knowing ObjC or any Apple programming in the past), and got stuck trying to create an ABPerson object. ABPerson seems to have an addPropertiesAndTypes method but I couldn't find an example for what to use in parameters. I might just jump ship and use appscript with your examples instead.

1
ClarkGoble•

Note that the above uses Appscript and not PyObjc. I'd definitely suggest using Appscript instead. It's very simple and there's a program ASTranslate that will translate Applescript to Python + Appscript. To install just do sudo easy_install appscript at the command line. I do all my scripting with Appscript and have for years.

1
packysauce•

pyobjc?

1
noreallyimthepope•

Hasn't been updated in years and does not seem to work with Lion/Xcode 4.

2
aasmodeusOP•

Right, my very shallow understanding is that the Python-ObjC glue code is now included by Apple (since 10.5). You're automatically using it when you use the AddressBook module. Please correct that if I'm wrong!

2
noreallyimthepope•

Sorry, can't say either way. I would wager that you are wrong, unfortunately, since this article hasn't been updated in years.

1
aasmodeusOP•

Hrm...

The PyObjC EGG is v2.2b3 and the notes reference that it's a beta, which requires OSX 10.5. The modification timestamp on AddressBook/PyObjC.bridgesupport is July 10, 2009... Perhaps I shouldn't be using it :)

Cheers

2
joshbudde•

I think you could work directly with the sqlite file as described here: http://krypted.com/mac-os-x/querying-address-bookapp-from-the-command-line/

1
pudquick•

This only works for reading. The sqlite db is basically the "cached" form of the contacts for quick load.

The real contact information is in the Metadata/ folder located in the same place. If you delete the sqlite db, AddressBook will happily recompile it. The same does not apply to the Metadata folder and its contents.

If you'll notice, the article doesn't provide any data insertion examples.

4
GimmeSomeSugar•

Have you read the Address Book Programming Guide for Mac OS X?
I found it quite lacking when I was trying to do some CardDAV stuff, but the class reference has some promising looking stuff.
This MacTech article has a worked example.

1
aasmodeusOP•

Thanks, yes I had found that article in my searching, but it's a read only example. I found another article (I think also from MacTech) which explained how to convert from ObjC specifications into Python parameter passing, but I still need a table of what specific parameters and keywords to use for AddressBook in particular.

1