关于Objective C:如何从我的应用程序启动Mac地址簿并选择特定的人?

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

我有一个应用程序,正在使用ABAddressBook API读取通讯录信息并选择一个人。我想提供一个来自我的应用程序的链接,以启动Mac通讯簿并预先选择特定用户。

我敢肯定,有几种方法可以做到-我想出了一种似乎可行的方法,但感觉有些笨拙。我启动" Address Book.app ",如果成功,它将调用一点AppleScript来选择" currentPerson " uniqueId(从ABRecord对象中选择)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
- (void) notifyABSelect
{
    NSString *src = [NSString stringWithFormat:@"tell application "Address Book"\
\
\\tset selection to first person whose id is "
%@" \
\
end 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");
    }

}

在此落下的地方是,如果通讯簿以前选择了"所有联系人"以外的其他组;可能无法根据某人是否在所选组中来找到该人。但是,再次在应用程序中单击链接确实可以正确转到"所有联系人"并找到此人。

完成此行为的替代方法有哪些?


您可以改用URL。

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


可能与您无关,但值得指出,因为我只是偶然发现了它。如果您是从ABPeoplePicker找到您的人的话,那是单线的。在" PeoplePicker"中选择该人后,呼叫

1
[myPeoplePicker selectInAddressBook: self];

您可以使用nil代替self
通讯录将在该人的页面上启动。
您也可以使用editInAddressBook:与准备好编辑的人一起启动通讯簿。

在我看来,这应该是ABPerson的方法,而不是ABPeoplePicker。