Re: [Pyobjc-dev] Getting unexpected NSTaggedDate when pulling birthday properties from AddressBook?
Brought to you by:
ronaldoussoren
From: Ken T. <ke...@co...> - 2016-08-14 01:31:37
|
On Aug 13, 2016, at 7:56 PM, Ramon Felciano <fel...@ya...> wrote: > > I am testing out pyobjc to perform some scripting on the Mac address book. I'm trying to extend the provided exportBook.py script (https://pythonhosted.org/pyobjc/examples/AddressBook/Scripts/exportBook/index.html) to include several date fields such as birthdate. It looked to me like the introspection model should allow me to simply add in additional field names: > > FIELD_NAMES=( > ('Last Name', AddressBook.kABLastNameProperty), > ('First Name', AddressBook.kABFirstNameProperty), > ('E-mail', AddressBook.kABEmailProperty), > ('Birthday', AddressBook.kABBirthdayProperty), > ) > > However the encodeField function returns an attribute error: > ----------------- > return value.encode('utf-8') > AttributeError: '__NSTaggedDate' object has no attribute 'encode' > ----------------- > > I'm not an Apple developer but some poking around on Google, StackExchange, etc suggests that NSTaggedDate isn't typically a returned data type, and the kABBirthdayProperty is supposed to be an NSDate property. This is making me wonder if something when wrong with my pyobjc installation -- can anyone suggest how best to debug this further? First, NSTaggedDate is a private subclass of NSDate. (In fact, it's a way of encoding a date into the pointer itself and the pointer doesn't point to a real, heap-allocated object.) But the problem is not specific to NSTaggedDate. It would happen with any NSDate: $ python Python 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from Foundation import * >>> d = NSDate.date() >>> d.encode('utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: '__NSDate' object has no attribute 'encode' The problem is that the sample script assumes all of the properties are string objects. It's not correct for properties which are date objects or other kinds of objects. You could check if the object is an NSDate and use an NSDateFormatter to build a string representation of the date object and then encode that into UTF-8. Or, for a quick-and-dirty approach, you could just ask the object (whatever type it is) for its description() and then encode that (i.e. return value.description().encode('utf-8')). Regards, Ken |