我正在开发 Mac 桌面应用程序并尝试运行 shell 脚本。我在为任务设置启动路径时挂断了电话。这是代码:
let path = fileURL.deletingLastPathComponent().relativePath
//set new filename
let strNewFileName = fileURL.deletingPathExtension().lastPathComponent.appending(".html")
let newPath = path + "/" + strNewFileName
//create script
var strScript = "pandoc -s -o "
strScript.append(newPath)
strScript.append(" ")
strScript.append(fileURL.absoluteString)
//set storage path
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
var documentPath:URL = URL(string: documentsPath)!
documentPath.appendPathComponent("pandoc.sh")
var myURLComponents = URLComponents()
myURLComponents.scheme = "file"
myURLComponents.path = documentPath.absoluteString
do {
//write file
try strScript.write(to: myURLComponents.url!, atomically: true, encoding: String.Encoding.utf8)
//run script
let myTask = Process()
myTask.launchPath = "/usr/bin/env"
myTask.arguments = [strScript]
let myPipe = Pipe()
myTask.standardOutput = myPipe
myTask.launch()
let data = myPipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
print(output!)
} catch {
print(error.localizedDescription)
}
这会抛出一个没有这样的文件的错误:
env: pandoc -s -o /Users/stevensuranie/Desktop/MarkdownFiles/targeting-params-ios.html file:///Users/stevensuranie/Desktop/MarkdownFiles/targeting-params-ios.md: No such file or directory
如有任何帮助,我们将不胜感激。
最佳答案
这条线不行
var documentPath:URL = URL(string: documentsPath)!
URL(string
适用于包含方案( file://
或 https://
)的 URL 字符串,对于文件系统路径,您必须使用 URL(fileURLWithPath
.
但是如果你使用FileManager
的URL相关API你可以避免 init
完全没有方法。
类似的问题是 absoluteString
, 永远不要在文件系统 URL 上调用它,总是使用 path
.
第二个致命问题是每个 shell 参数必须是 arguments
中的一个项目。数组和可执行文件必须指定完整路径
//create script
let scriptArguments = ["/path/to/pandoc", "-s", "-o", newPath, fileURL.path]
//set storage path
let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let scriptURL = documentsURL.appendingPathComponent("pandoc.sh")
以下几行是多余的 <罢工> 罢工>
<罢工>var myURLComponents = URLComponents()
myURLComponents.scheme = "file"
myURLComponents.path = documentPath.absoluteString
罢工><罢工>罢工>
将字符串写入scriptURL
do {
//write file
try strScript.write(to: scriptURL, atomically: true, encoding: .utf8)
...
myTask.arguments = scriptArguments
最后不要使用 NS...
Swift 中的类
<罢工>罢工>
<罢工>let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
罢工><罢工>罢工>
使用 native
等效
let output = String(data: data, encoding: .utf8)
关于swift - 在 Cocoa 中使用 Swift 运行 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423147/