在Cocoa中使用Swift运行Shell脚本
我正在开发Mac桌面应用程序,并尝试运行shell脚本。我正在为任务设置启动路径。代码如下:在Cocoa中使用Swift运行Shell脚本,swift,shell,cocoa,Swift,Shell,Cocoa,我正在开发Mac桌面应用程序,并尝试运行shell脚本。我正在为任务设置启动路径。代码如下: let path = fileURL.deletingLastPathComponent().relativePath //set new filename let strNewFileName = fileURL.deletingPathExtension().lastPathComponent.appending(".html") let newPath = pa
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(字符串:documentsPath)代码>
URL(string
用于包含方案的URL字符串(file://
或https://
),对于文件系统路径,必须使用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")
以下行是多余的
将字符串写入scriptURL
do {
//write file
try strScript.write(to: scriptURL, atomically: true, encoding: .utf8)
...
myTask.arguments = scriptArguments
最后,不要在Swift中使用NS…
类
使用本机
等效工具
let output = String(data: data, encoding: .utf8)
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
let output = String(data: data, encoding: .utf8)