我假设你想运行一个applescript。如果您有大量的 AppleScript 代码要运行,那么脚本桥就很好。但是,如果您只有少量,那么更简单的方法是使用 NSApplescript。
例如,如果你想运行这个 applescript...
tell application "System Events"
set theProcesses to processes
repeat with aProcess in theProcesses
tell aProcess to get properties
end repeat
end tell
然后你可以这样写...
NSString* cmd = @"tell application \"System Events\"\nset theProcesses to processes\nrepeat with aProcess in theProcesses\ntell aProcess to get properties\nend repeat\nend tell";
NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:cmd];
NSDictionary* errorDict = nil;
NSAppleEventDescriptor* result = [theScript executeAndReturnError:&errorDict];
[theScript release];
if (errorDict) {
NSLog(@"Error:%@ %@", [errorDict valueForKey:@"NSAppleScriptErrorNumber"], [errorDict valueForKey:@"NSAppleScriptErrorMessage"]);
return;
}
// do something with result
NSLog(@"result: %@", result);