[英]Run Shell Script from Xcode button
I'm trying to get a button in Xcode to run a shell script with clicked. 我试图在Xcode中获得一个按钮,以单击运行Shell脚本。
This works 这有效
@IBAction func test(_ sender: NSButton) {
let path = "/usr/bin/say"
let arguments = ["hello world"]
sender.isEnabled = false
let task = Process.launchedProcess(launchPath: path, arguments: arguments)
task.waitUntilExit()
sender.isEnabled = true
}
But when I try this it does not work to run a script from the Desktop 但是,当我尝试这样做时,无法从桌面运行脚本
@IBAction func test(_ sender: NSButton) {
let path = "/bin/bash"
let arguments = ["~/Desktop/test.sh"]
sender.isEnabled = false
let task = Process.launchedProcess(launchPath: path, arguments: arguments)
task.waitUntilExit()
sender.isEnabled = true
}
I get this error output in Xcode 我在Xcode中得到此错误输出
/bin/bash: ~/Desktop/test.sh: No such file or directory
If anyone can help me with some help or example that would great. 如果有人可以帮助我,请提供一些帮助或示例。 Thank you.
谢谢。
关闭Xcode沙箱模式,它将解决此问题
func shell(_ args: String) -> String {
var outstr = ""
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", args]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
outstr = output as String
}
task.waitUntilExit()
return outstr
}
This Function returns output of Bash Script you're trying to run 此函数返回您要运行的Bash脚本的输出
let cmd = "for i in $(ifconfig -lu); do if ifconfig $i | grep -q \"status: active\" ; then echo $i; fi; done"
Above Code Demonstrate how to use it. 上面的代码演示了如何使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.