How do I launch Mac Address Book from my application and select a specific person?

I have an application and I am reading information in an address book using the ABAddressBook API and picking a person. I would like to provide a link from my application to launch the Mac address book and preselect a specific user.

I'm sure there are several ways to do this - I came up with one that seems to work, but it feels a little awkward. I run "Address Book.app", if successful it calls some AppleScript to pick a unique identifier "currentPerson" (from the ABRecord object).

- (void) notifyABSelect
{
    NSString *src = [NSString stringWithFormat:@"tell application \"Address Book\"\r\n\tset selection to first person whose id is \"%@\" \r\nend tell", [currentPerson uniqueId]];

    NSMutableDictionary *err = [NSMutableDictionary dictionary];
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:src];
    [script executeAndReturnError:&err];

    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];

    [src release];
    [err release];
    [script release];
}

- (IBAction) launchAddressBook:(id)sender
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] 
     addObserver:self 
     selector:@selector(notifyABSelect) 
     name:NSWorkspaceDidLaunchApplicationNotification 
     object:nil];

    if ([[NSWorkspace sharedWorkspace] launchApplication:@"Address Book.app"] == YES)
    {
        [self notifyABSelect];
    }
    else 
    {
        NSLog(@"Unable to launch address book");
    }

}

      

If it crashes, if a group other than "All contacts" was previously selected in the address book; he may or may not find the person depending on whether they are in the selected group. However, after clicking the link a second time in the app, correctly navigate to All Contacts and find the person.

What are the alternatives for performing this behavior?

+2


a source to share


2 answers


You can use URL instead.



[[NSWorkspace sharedWorkspace] openURL:
 [NSURL URLWithString:
  [NSString stringWithFormat:
   @"addressbook://%@", [currentPerson uniqueId]]]];

      

+3


a source


Maybe this doesn't apply to you, but it's worth pointing out because I came across it by accident. If you have your person from ABPeoplePicker this is a one-liner. With the person selected in the PeoplePicker, call

[myPeoplePicker selectInAddressBook: self];

      



You can use nil

instead self

. The address book starts on this page. You can also use editInAddressBook:

to launch an address book with a person ready to edit.

It seems to me that it should be the ABPerson method, not ABPeoplePicker.

+1


a source







All Articles