2010-05-09 60 views
2

我有一個應用程序,我正在閱讀地址簿信息,使用ABAddressBook API並選擇一個人。我想從我的應用程序提供一個鏈接來啓動Mac地址簿並預先選擇特定的用戶。如何從我的應用程序啓動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"); 
    } 

} 

如果地址簿中的地址簿以前沒有選擇「所有聯繫人」以外的組,根據他們是否在選定的組中,它可能或不可能找到該人。但是,再次點擊該應用中的鏈接會正確轉到「所有聯繫人」並找到該人。

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

回答

3

您可以改用URL。

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

謝謝,尼古拉斯 - 顯然我喜歡以艱難的方式做事情。 :) 奇蹟般有效。 – Eric 2010-05-10 16:42:28

1

可能與您無關,但值得指出,因爲我只偶然發現它。如果你從ABPeoplePicker那裏得到了你的人,那麼這是一個單線程。隨着在PeoplePicker選擇的人,叫

[myPeoplePicker selectInAddressBook: self]; 

可以使用nil而不是self。 地址簿在該人的頁面上啓動。 您也可以使用editInAddressBook:來啓動地址簿,並且可以編輯該人員。

對我來說,這應該是一個ABPerson的方法,而不是ABPeoplePicker。

相關問題