Frage

I have been trying to add both a "work" and a "home" phone number to the Mac AddressBook using python and pyObjC. I believe you would need add a multivalue object but I'm not sure how to do this using pyobjc. Here's the code sample I have put together so far:

import AddressBook
from AddressBook import *

ab = AddressBook.ABAddressBook.sharedAddressBook()
p = ABPerson.alloc().init()
p.setValue_forProperty_('JOE', kABFirstNameProperty)
p.setValue_forProperty_('TEST', kABLastNameProperty)

homephoneNumberMultiValue = ABMultiValueCreateMutable()
homephoneNumberMultiValue = ABMultiValueAddValueAndLabel(homephoneNumberMultiValue, '555-555-1212', kABPersonPhoneMobileLabel);
p.setValue_(kABPersonPhoneProperty, homephoneNumberMultiValue);

workphoneNumberMultiValue = ABMultiValueCreateMutable()
workphoneNumberMultiValue = ABMultiValueAddValueAndLabel(workphoneNumberMultiValue, '555-555-1213', kABPersonWorkMobileLabel);  
p.setValue_(kABPersonPhoneProperty, workphoneNumberMultiValue);

ab.addRecord_(p)
ret = ab.save()
War es hilfreich?

Lösung

MultiValue is called multi-value exactly for that reason - one object, multiple values. You don't need to create two separate objects.

See this example:

import AddressBook
from AddressBook import *

ab = AddressBook.ABAddressBook.sharedAddressBook()
p = ABPerson.alloc().init()
p.setValue_forProperty_('JOE', kABFirstNameProperty)
p.setValue_forProperty_('TEST', kABLastNameProperty)

phoneNumberMultiValue = ABMultiValueCreateMutable()
ABMultiValueAdd(phoneNumberMultiValue, '555-555-1212', kABPhoneMobileLabel, None);
ABMultiValueAdd(phoneNumberMultiValue, '555-555-1213', kABPhoneWorkLabel, None);
p.setValue_forProperty_(phoneNumberMultiValue, kABPhoneProperty);

ab.addRecord_(p)
ret = ab.save()

In other words, you create one MultiValue and add different values there under different labels. This example is tested and working on 10.8.3 with python 2.7.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top