I continue on my quest to get some basic access to the Address Book through AppleScript Studio.
On the positive side, I’ve managed to get the PeoplePicker palette working quite easily. After adding the AddressBook.framework to my project in XCode, I added the PeoplePicker view in Interface builder by simply adding the palette to my IB palettes through the Palette Preferences. The menu item is Tools/Palettes/Palette Preferences.
Once the ABPalette is added, you can simply drag a PeoplePicker to your window, and the PeoplePicker settings are handled through the IB Info window, instead of obj-C! Hooray!
Now comes the hard part - accessing the AB info for a selected record. I’m halfway there so far. I’m able to get the unique ID of the selected record with this script:
set x to view “myPeoplePicker” of window “myMain”
set y to call method “selectedRecords” of x
set z to call method “uniqueId” of (item 1 of y)
log z
Voila! I have the unique ID of the selected record.
Now I want to get all the info of that record, and I can’t seem to get it to work. Here’s what I’m guessing is the relevant obj-C function documentation:
Returns the ABPerson or ABGroup record that matches the given unique ID, or nil if no record has the given ID. If uniqueId is nil, this method raises an exception.
But I can’t make heads or tails of this. I try (unsuccessfully) things like this:
set a to call method “recordForUniqueId:” with parameter theUniqueID
and
set a to call method “recordForUniqueId:” of theUniqueID
Any suggestions welcome. I can’t believe I’m the only one who’d find this useful.
I’ve mocked up a quick project that shows you (at least to a limited degree) how to do what you want. I had to resort to a little Obj-C to get it done but it isn’t too bad and since you were already comfortable calling so many AB methods, I figured you’d be OK with it. You can get the project here:
That won’t work because first you have to call the sharedAddressBook instance of the AddressBook object and you can’t do that in AppleScript via “call method” calls because the AddressBook object doesn’t have an analog in the AppleScript world and so it causes an error. I get around this in the demo I posted above by calling a method that gets the recordForUniqueId of the sharedAddressBook and then parses the data from the ABPerson record and returns an NSArray (list).
OK. The ABDemo app provides a helpful basic template. Unfortunately, it doesn’t do quite what I need, so I’m forced to try to delve a bit deeper into the dark woods of Obj-C.
What I’m trying to accomplish is to basically duplicate the functionality of the AppleScript code:
Tell app “Address Book” to get name of person id “HereGoesTheUniqueID”
That code gets the first and last name of the contact, OR gets the company name of the contact if the contact is a company.
So, to start, I tried modifying part of Jon’s AB_handler.m to:
The problem here (and with Jon’s original code) is that if the first name is missing, the other properties don’t get handed back to the AppleScript. And if there is no first or last name, the company name doesn’t get handed back.
I have no idea how to debug the Obj-C.
Once the above problem is solved, then we have to figure out if the contact is displayed as a person or as a company. The kABShowAsCompany property is somehow a part of the kABPersonFlags property, and I’m way out of my depth in trying to figure this out.
For the record, the values did get handed back but it’s just that when they those values were missing, the null values, when coerced to a string, caused problems. The other strings were still valid.
Well I am trying to use this demo to be able to select an address. However, when I use the demo the result I get does not change depending on which e-mail address I select in the picker. How can tell the handler what address I want to use?
Model: MacBook Pro
Browser: Safari 525.13
Operating System: Mac OS X (10.5)