我一直在尝试使用python和pyObjC将“work”和“home”电话号码添加到Mac AddressBook。我相信你需要添加一个多值对象,但我不知道如何使用pyobjc这样做。这是我到目前为止汇总的代码示例:
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()
答案 0 :(得分:1)
MultiValue因此而被称为多值 - 一个对象,多个值。您不需要创建两个单独的对象。
见这个例子:
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()
换句话说,您创建一个MultiValue并在不同标签下添加不同的值。这个例子在使用python 2.7进行测试并使用10.8.3。