在Cocoa中使用Swift运行Shell脚本

时间:2019-03-29 17:59:10

标签: swift shell 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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

此行无效

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)