我有一個應用程序,我正在閱讀地址簿信息,使用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");
}
}
如果地址簿中的地址簿以前沒有選擇「所有聯繫人」以外的組,根據他們是否在選定的組中,它可能或不可能找到該人。但是,再次點擊該應用中的鏈接會正確轉到「所有聯繫人」並找到該人。
完成此行爲的替代方法有哪些?
謝謝,尼古拉斯 - 顯然我喜歡以艱難的方式做事情。 :) 奇蹟般有效。 – Eric 2010-05-10 16:42:28