是否可以在 Cocoa 应用程序中运行 AppleScript 代码?
我试过 NSAppleScript 类,但没有成功。
此外,Apple 允许这样做吗?
最佳答案
解决了!
Xcode 没有将我的脚本文件保存到应用程序的资源路径中。要从 Cocoa 应用程序运行 AppleScript 代码,请使用:
NSString *path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt"];
NSURL *url = [NSURL fileURLWithPath:path];NSDictionary *errors = [NSDictionary dictionary];
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
swift 5.6.1:
import AppleScriptObjC
import Cocoa
let path = Bundle.main.path(forResource: "ScriptName", ofType: "scpt")
let url = URL(fileURLWithPath: path ?? "")
var errors: [AnyHashable : Any] = [:]
let appleScript = NSAppleScript(contentsOf: url, error: &errors)
appleScript?.executeAndReturnError(nil)
关于objective-c - 从 Cocoa 应用程序运行 AppleScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4909702/