我正在尝试使用Swift 5从Mac OS / X应用程序运行脚本,但是在运行该进程时,它找不到该脚本(而且我肯定我遗漏了一些明显的东西,但是看不到什么)
我尝试只传递脚本名称,脚本的完整URL以及task.currentDirectoryURL完成和未完成;但仍然找不到脚本
public func run() -> Bool {
let task = Process()
// scriptURL URL "file:///Users/phil/Documents/My%20Scripts/"
// name String "myscript"
task.executableURL = scriptURL.appendingPathComponent(name).appendingPathExtension("sh")
task.currentDirectoryURL = scriptURL
task.arguments = []
let outputPipe = Pipe()
let errorPipe = Pipe()
task.standardOutput = outputPipe
task.standardError = errorPipe
do {
try task.run()
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
print(String(decoding: outputData, as: UTF8.self))
return true
} catch let error as NSError {
print(error.localizedDescription)
let handle = errorPipe.fileHandleForReading
let errorData = handle.readDataToEndOfFile()
print(String(decoding: errorData, as: UTF8.self))
return false
}
}
调用task.run()时,它被Catch捕获,并且NSError返回“文件“ myscript.sh”不存在。”,但它确实存在于ScriptsURL文件夹中。