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);