在 Swift 中从 Applescript 获取 return 值
Get return values from Applescript in Swift
是否可以 运行 Swift 中的 Applescript 并接收 return 值?
我有一个像下面这样的脚本。我如何在 swift 中 运行 并接收 return 值?
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)
你能告诉我我的结果应该在哪里或者我做错了什么吗?如果我打印出 errorInfo 或跟踪结果是一些奇怪的数字。
NSAppleScript
是一堆毫无用处的垃圾。如果您想将自己的 AppleScript 代码集成到您的 ObjC/Swift 项目中,请使用 AppleScript-ObjC 桥,它使用标准的 ObjC 消息传递并提供大多数 AS 类型与其 Foundation 等价物之间的自动桥接:
http://appscript.sourceforge.net/asoc.html
http://macscripter.net/viewtopic.php?pid=175562#p175562
如果要执行用户提供的脚本,一般要使用NSUserAppleScriptTask
。从功能上讲,它甚至比 NSAppleScript
还差(不支持持久性);但是,它是沙箱友好的,而 NSAppleScript
(在进程中运行)不是。
如果找到我问题的解决方案。
let text = startAtLoginScript.stringValue
这就是我解析结果的方式。
实际上,let text = startAtLoginScript.stringValue
不会工作,因为 NSAppleScript 没有名为 "stringValue" 的成员。但是,您可以这样做:
let text = startAtLoginScript.executeAndReturnError(errorinfo)!.stringValue!
这将 return 脚本的输出,或者,在出现错误的情况下,errorinfo
.
中的任何内容
是否可以 运行 Swift 中的 Applescript 并接收 return 值? 我有一个像下面这样的脚本。我如何在 swift 中 运行 并接收 return 值?
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)
你能告诉我我的结果应该在哪里或者我做错了什么吗?如果我打印出 errorInfo 或跟踪结果是一些奇怪的数字。
NSAppleScript
是一堆毫无用处的垃圾。如果您想将自己的 AppleScript 代码集成到您的 ObjC/Swift 项目中,请使用 AppleScript-ObjC 桥,它使用标准的 ObjC 消息传递并提供大多数 AS 类型与其 Foundation 等价物之间的自动桥接:
http://appscript.sourceforge.net/asoc.html
http://macscripter.net/viewtopic.php?pid=175562#p175562
如果要执行用户提供的脚本,一般要使用NSUserAppleScriptTask
。从功能上讲,它甚至比 NSAppleScript
还差(不支持持久性);但是,它是沙箱友好的,而 NSAppleScript
(在进程中运行)不是。
如果找到我问题的解决方案。
let text = startAtLoginScript.stringValue
这就是我解析结果的方式。
实际上,let text = startAtLoginScript.stringValue
不会工作,因为 NSAppleScript 没有名为 "stringValue" 的成员。但是,您可以这样做:
let text = startAtLoginScript.executeAndReturnError(errorinfo)!.stringValue!
这将 return 脚本的输出,或者,在出现错误的情况下,errorinfo
.