Getting address from selected record in people picker

Hi!

I am trying to make a dynamic template for Microsoft Word 2008 with Apple Script studio. So far so good, but I would like to be able to get addresses from the Address Book with a people picker. I can get the first and last name by using the ABHandler from the AB-demo: http://www.jonn8.com/html/misc_projects.html

However, I would like to get the address that the user has selected in the people picker (home, work, etc.) as well. There must be a way of getting what address the user has selected and returning those values so I can use them in the AppleScript? Since I don’t know any C and have not found any information on this in any tutorial or references (that I can understand) I would be very grateful for any help with this.

Hi,

I changed Jonathan’s code to retrieve the address instead of the email information

First, in Interface Builder connect File’s Owner to will finish launching handler

in the AppleScript file, add this handler


on will finish launching theObject
    call method "initABView:" of class "ABHandler" with parameter (view "people_picker" of window "main")
end will finish launching

and replace the else part of the on clicked handler with


else if object_name = "info" then
            set the_record to call method "getABDictFromID:picker:" of class "ABHandler" with parameters {the_id, (view "people_picker" of window "main")}
            set the_info to {"first name: ", the_record's first_name, return, "last name: ", the_record's last_name, return, "company: ", the_record's company, return, ¬
                "street: ", the_record's street, return, "ZIP: ", the_record's zip, return, "city: ", the_record's city}
            set contents of text view 1 of scroll view 1 of window "main" to (the_info as Unicode text)
        end if

Now replace the contents of the ABHandler.h and ABHandler.m with this code

.h

[code]#import <Cocoa/Cocoa.h>
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABPeoplePickerView.h>

@interface ABHandler : NSObject

  • (void)initABView:(ABPeoplePickerView *)picker;
  • (NSDictionary *)getABDictFromID:(NSString *)theID picker:(ABPeoplePickerView *)picker;

@end[/code]
.m

[code]#import “ABHandler.h”

NSString *kEmptyString = @“”;

@implementation ABHandler

  • (void)initABView:(ABPeoplePickerView *)picker
    {
    [picker removeProperty:kABEmailProperty];
    [picker addProperty:kABAddressProperty];
    [picker setAllowsMultipleSelection:NO];
    [picker setValueSelectionBehavior: ABSingleValueSelection];
    }

  • (NSDictionary *)getABDictFromID:(NSString *)theID picker:(ABPeoplePickerView *) picker
    {
    NSString *firstName, *lastName, *company, *city = kEmptyString, *street = kEmptyString, *zip = kEmptyString;
    NSDictionary *address;
    ABPerson *person = (ABPerson *)[[ABAddressBook sharedAddressBook] recordForUniqueId:theID];
    if (person) {
    firstName = [person valueForProperty:kABFirstNameProperty];
    if (!firstName) firstName = kEmptyString;
    lastName = [person valueForProperty:kABLastNameProperty];
    if (!lastName) lastName = kEmptyString;
    company = [person valueForProperty:kABOrganizationProperty];
    if (!company) company = kEmptyString;

      NSArray *addresses = [picker selectedValues];
      if ([addresses count]) {
      	address = [addresses objectAtIndex:0];
      	street = [address objectForKey:@"Street"];
      	if (!street) street = kEmptyString;
      	zip = [address objectForKey:@"ZIP"];
      	if (!zip) zip = kEmptyString;
      	city = [address objectForKey:@"City"];
      	if (!city) city = kEmptyString;
      }
    

    }
    return [NSDictionary dictionaryWithObjectsAndKeys:
    firstName, @“first_name”,
    lastName, @“last_name”,
    company, @“company”,
    street, @“street”,
    zip, @“zip”,
    city, @“city”, nil];
    }[/code]

Thank you so much for your very quick reply! It works beautifully! :smiley: I really do appriciate your help with this! My template app is finally completed.

One thing that is missing is the state? How would you get this?

Thanks

In the line to declare the parameter strings (NSString *firstName.) , add

, *state = kEmptystring

before the semicolon.

After the if (!city) city = kEmptyString; line add

state = [address objectForKey:@"State"]; if (!state) state = kEmptyString;
In the returning NSDictionary add

state, @"state"

before the nil delimiter

In the AppleScript part you have to add the parameter in the set the_info line

thanks :slight_smile: worked great

OK, so my address book thingy works great (thanks to all of you). However, it does not show the company name in the ABpeople picker view nor does the search field return results if I try to search contacts by company name. Any ideas how I can adjust the content that is being displayed and the search capabilities?

Thanks

just add

[picker addProperty:kABOrganizationProperty];

in the iinitABView: method