【发布时间】:2022-01-18 21:48:27
【问题描述】:
我正在尝试快速运行 applescript 代码以从其他应用程序获取信息。 这是一个示例代码:
var applescript = "tell application "Xcode" \n set fileName to name of window 1 \n end tell"
var error: NSDictionary?
let scriptObject = NSAppleScript(source: applescript)
let output: NSAppleEventDescriptor = scriptObject!.executeAndReturnError(&error)
if (error != nil) {
print("error: \(String(describing: error))")
}
if output.stringValue == nil{
let empty = "the result is empty"
return empty
}
else {
return (output.stringValue?.description)!
}
但它总是返回这样的错误消息:
error: Optional({
NSAppleScriptErrorBriefMessage = "Expected end of line but found unknown token.";
NSAppleScriptErrorMessage = "Expected end of line but found unknown token.";
NSAppleScriptErrorNumber = "-2741";
NSAppleScriptErrorRange = "NSRange: {25, 1}";
})
我认为它提到的未知标记是“\n”。因为当我在代码中运行其他没有“\n”的applescript时,没有错误消息。
有人可以帮我解决这个问题吗?非常感谢!
【问题讨论】:
-
如果您的 AppleScripts 不平凡,您可能会发现call AS handlers directly via the AppleScript-ObjC bridge 更容易。它还允许您在外部编辑器(脚本编辑器/脚本调试器)中编写 AppleScript 代码。
标签: ios swift macos applescript