是否可以用swift运行applescript并接收返回值?
我有一个像下面这样的脚本。我怎样才能用swift运行并接收返回值?

global frontApp
set windowTitle to "
tell application "System Events"
    set frontApp to first application process whose frontmost is true
end tell
return {frontApp}

我的代码如下
var set: String = "set windowTile to \"\"\n"
var tell: String = "tell application \"System Events\"\n"
var setFrontApp: String = "set frontApp to first application
                           process whose frontmost is true\n"
var setFrontAppName: String = "set frontAppName to name of frontApp\n"
var tellProcces: String = "tell process frontAppName\n"
var tellFirst: String = "tell (1st window whose value of attribute
                              \"AXMain\" is true)\n"
var setWindowTitle: String = "set windowTitle to value of
                               attribute \"AXTitle\"\n"
var endTellFirst: String = "end tell\n"
var endTellProcess: String = "end tell\n"
var endTell: String = "end tell"
var startAtLoginScript: NSAppleScript = NSAppleScript(source: tell
                                        + deleteItem + endTell)!
startAtLoginScript.executeAndReturnError(errorInfo)

你能告诉我我的结果应该在哪里,或者我做错了什么吗?如果我打印出错误信息或跟踪结果是一些奇怪的数字。

最佳答案

NSAppleScript是一堆无用的垃圾。如果您想将自己的Apple Script代码集成到Objc/Swift项目中,请使用Apple Script Objc桥,它使用标准Objc消息传递,并在大多数AS类型及其基础等价物之间提供自动桥接:
http://appscript.sourceforge.net/asoc.html
http://macscripter.net/viewtopic.php?pid=175562#p175562
如果要执行用户提供的脚本,通常需要使用NSUserAppleScriptTask。从功能上讲,它甚至比NSAppleScript更糟糕(没有持久性支持);但是,它是沙盒友好型的,而NSAppleScript(在进程中运行)则不是。
https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSUserAppleScriptTask_Class/

关于macos - 从Swift中的Applescript获取返回值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28955239/

10-12 00:15
查看更多