Re: Obtain data from an Address Book card

Ronald Oussoren <[email protected]>
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
The script below creates a CSV file with all records in de AddressBook using the AddresBook framework. (This is the exportBooks.py script from the examples directory in the pyobjc-framework-AddressBook archive).

The Python API to AddressBook is the same as the Objective-C one, in general Objective-C based examples should help to get you going with accessing the same APIs using PyObjC.

Ronald


# ===

#!/usr/bin/env python2.3
"""
This script gives a short example on how to use the addressbook framework,
it creates an CSV file containing information about all entries in the
addressbook.

Usage:
    python exportBook.py output.csv
"""
import csv
import sys

import AddressBook

# The names of fields in the export, and the corresponding property.
FIELD_NAMES=(
    ('Last Name',  AddressBook.kABLastNameProperty),
    ('First Name', AddressBook.kABFirstNameProperty),
    ('E-mail',     AddressBook.kABEmailProperty),
)

def encodeField(value):
    """
    Encode a value into an UTF-8 string
    """
    if value is None:
        return ''

    if isinstance(value, AddressBook.ABMultiValue):
        # A multi-valued property, merge them into a single string
        result = []
        for i in range(len(value)):
            result.append(value.valueAtIndex_(i).encode('utf-8'))
        return ', '.join(result)

    return value.encode('utf-8')

def personToFields(person, fieldnames):
    """ Extract the specified fields from a person object """
    return [ encodeField(person.valueForProperty_(nm)) for nm in fieldnames ]     

def bookFields(book, fieldnames):
    """ Generate the records for all people in the book """
    for person in book.people():
        yield personToFields(person, fieldnames)

def main(argv = None):
    """ main entry point """

    if argv is None:
        argv = sys.argv[1:]

    if len(argv) != 1:
        print "Usage: python exportBook.py output.csv"
        sys.exit(1)

    book = AddressBook.ABAddressBook.sharedAddressBook()

    fp = open(argv[0], 'wb')
    csvStream = csv.writer(fp)
    csvStream.writerow([ f[0] for f in FIELD_NAMES])
    for row in bookFields(book, [ f[1] for f in FIELD_NAMES]):
        csvStream.writerow(row)
    fp.close()

if __name__ == "__main__":
    main()

#====

On 14 Oct, 2010,at 11:28 PM, Ned Deily <[email protected]> wrote:

In article <C8DCC28D.2B7B7%[email protected]>,
Jeremy Reichman <[email protected]> wrote:
> Does anyone have any examples of how to obtain data from cards via the
> Address Book API with PyObjC? I'm not finding much (at least at my skill
> level) via searching.
> 
> My goal is to output a few of the fields from "my card." I'm interested in
> this from the scripting perspective, not doing so in a full-fledged app. I'd
> like to do it with Python, partly as a learning exercise. The little bit
> I've done with PyObjC has been trying to update a script I wrote that used
> the old Core Graphics bindings.
> 
> If I were doing this via a shell script, I could get information with an
> osascript line like this:
> 
> $ osascript -e 'tell application "Address Book" to get the {first name, last
> name, organization} of my card'
> Leonardo, Flathead, GUE Tech

You might consider using py-appscript instead:

>>> from appscript import *
>>> my_card = app("Address Book").my_card.properties()
>>> [my_card.get(f) for f in (k.first_name, k.last_name, k.organization)]
[u'Ned', u'Deily', k.missing_value]

http://appscript.sourceforge.net/

-- 
Ned Deily,
[email protected]


------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly 
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev
_______________________________________________
Pyobjc-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyobjc-dev

------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly 
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev

_______________________________________________
Pyobjc-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.