【问题标题】:How do I launch the Mac Address Book from my app and select a specific person?如何从我的应用程序启动 Mac 通讯簿并选择特定的人?
【发布时间】:2010-05-09 18:39:02
【问题描述】:

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

我确信有几种方法可以做到这一点 - 我想出了一种似乎可行但感觉有点笨拙的方法。我启动“Address Book.app”,如果成功,它会调用一些 AppleScript 来选择“currentPerson”uniqueId(来自 ABRecord 对象)。

- (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");
    }

}

如果通讯簿之前选择了“所有联系人”以外的组,则会出现这种情况;它可能会也可能不会根据他们是否在所选组中找到该人。但是,在应用程序中再次单击该链接确实会正确转到“所有联系人”并找到此人。

完成此行为的替代方法是什么?

【问题讨论】:

    标签: objective-c cocoa macos hyperlink addressbook


    【解决方案1】:

    您可以改用 URL。

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

    【讨论】:

    • 谢谢,Nicholas - 显然我喜欢以艰难的方式做事。 :) 像魅力一样工作。
    【解决方案2】:

    可能与您无关,但值得指出,因为我只是偶然发现它。如果您从 ABPeoplePicker 得到您的人,那么它是单行的。在 PeoplePicker 中选择人员后,调用

    [myPeoplePicker selectInAddressBook: self];
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      相关资源
      最近更新 更多