运行 Shell 脚本 Swift 在 Cocoa

Running A Shell Script With Swift in Cocoa

我正在开发 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 用于 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")

以下几行是多余的

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

最后不要在Swift

中使用NS...

let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)

使用 native 等效项

let output = String(data: data, encoding: .utf8)