退出可可的其他应用程序 [英] Quitting other applications in cocoa
问题描述
我需要在可可中退出其他应用程式。我有一个userInfo字典从通知,告诉我应用程序的名称。我尝试了方法terminate和forceTerminate,但他们没有工作(我认为他们只有在雪豹。)
I need to quit other applications in cocoa. I have a userInfo dictionary from a notification that tells me the name of the application. I tried the methods terminate and forceTerminate, but they did not work (I think they are only available in snow leopard.)
推荐答案
解决方案(占用OS X的最后3-4个版本中可用的所有不同的API)将使用AppleScript。只要生成必要的脚本在Obj-C / Python / Java无论你是真正使用(我假设Obj-C,因为你特别说在可可)。并使用NSAppleScript类(一个假设的例子)执行它:
The best solution (accounting for all the different API's available in the last 3-4 versions of OS X) is going to be using AppleScript. Just generate the necessary script in Obj-C/Python/Java whatever it is you are actually using (I'm assuming Obj-C since you specifically said 'In Cocoa'). And execute it using the NSAppleScript class (a contrived example):
// Grab the appName
NSString *appName = [someDict valueForKey:@"keyForApplicationName"];
// Generate the script
NSString *appleScriptString =
[NSString stringWithFormat:@"tell application \"%@\"\nquit\nend tell",
appName];
// Execute the script
NSDictionary *errorInfo = nil;
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:theScript];
NSAppleEventDescriptor *theDescriptor = [run executeAndReturnError:&errorInfo];
// Get the result if your script happens to return anything (this example
// really doesn't return anything)
NSString *theResult = [theDescriptor stringValue];
NSLog(@"%@",theResult);
这有效地运行一个脚本(如果appName是'Safari'):
This effectively runs a script that (if appName was 'Safari') looks like:
tell application "Safari"
quit
end tell
您可以查看此SO问题
这篇关于退出可可的其他应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!