【发布时间】:2020-09-23 23:44:56
【问题描述】:
我希望通过我在 MAC 中的 SWIFT 应用程序在命令行上运行命令。
var resString = "open \(app.getLocation()) --args 25 40"
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", resString]
task.launch()
print (resString)
当我在控制台上打印 resString 时,我得到以下内容
open [path to app on my local drive] --args 25 40
当我在命令行中复制粘贴时正常执行。但另一方面,应用程序已打开,但参数被忽略。
我也试过这个https://stackoverflow.com/a/53725745/12197482 仍然没有用,应用程序已启动但参数没有通过
编辑:这是最令人沮丧的最有趣的事情,我创建了一个包含命令的小脚本,其中包含
#! /bin/bash
open ./SimulatorDebug.app --args arg1 arg2
我再次从终端运行它,参数正确传递,没有问题。我尝试从我的应用程序运行,但同样的问题再次发生,应用程序运行播放但没有传递任何 ARGS,我觉得这很奇怪。
【问题讨论】:
-
参数应该作为数组传递:
task.arguments = ["-c", "open", app.getLocation(), "--args", "25", "40"]。如果应用是沙盒的,你无论如何都不能使用Process。 -
@vadian,好吧,你是对的,当我删除沙箱时不知何故传递了参数,但我确实需要一个沙箱,因为我正在从本地服务器下载文件。我能做些什么解决方法?
-
请阅读objc.io/issues/14-mac/sandbox-scripting。这是关于 AppleScript 但它与 shell 脚本的模式相同。
标签: swift macos shell macos-catalina