【问题标题】:How to run Terminal commands from cocoa app?如何从可可应用程序运行终端命令?
【发布时间】:2020-04-23 12:32:23
【问题描述】:

我在从 Cocoa 应用程序运行终端命令时遇到问题。 终端的输入非常简单:/Users/.../Csvmidi </Users/.../test.csv> /Users/.../Melody.mid 这是三个输入——三个实际路径——只是写成一行并由一个空格分隔:第一个“Csvmidi”运行一个 Unix 应用程序,它将 test.csv 转换为一个实际的可听 MIDI 文件。通过终端它可以完美运行...

我只是无法通过 Cocoa 应用程序让它工作。

        let process = Process()
        process.executableURL = URL(fileURLWithPath: "/bin/zsh/")
        process.arguments = [lblCsvmidi.stringValue,"<"+lblURL.stringValue+">",lblMidi.stringValue]
    //I saved the URL of the UNIX program, test.csv and Melody.mid in a lable just to be sure.
    //lblCsvmidi --> URL of Csvmidi UNIX program
    //lblURL --> URL of test.csv
    //lblMidi --> URL of Melody.mid


        print(lblCsvmidi.stringValue,"<" + lblURL.stringValue + ">",lblMidi.stringValue) 
    // this print command was only made to check the arguments in the terminal if they would work --> they do


        process.terminationHandler = { (process) in
            print("\ndidFinish: \(!process.isRunning)")
        }
        do {

          try process.run()
        } catch {}

当我运行代码时,它给了我75: unmatched" 的错误,但实际上命令中没有引号 - 或者我收到“权限被拒绝”错误。

我尝试了几个 bin 文件夹,例如(我真的尝试了几乎所有可能的方法): - /bin/zsh - /bin/csh - /bin/ksh - ... 我在做什么错--> 我没有在此处的其他问题中找到信息,Apple 的 Process、NSTask 和 Bundle 信息到目前为止还没有帮助我。

谢谢!!

【问题讨论】:

    标签: swift macos cocoa terminal process


    【解决方案1】:

    以下在我的系统上运行没有错误:

    import Cocoa 
    
    let process = Process()
    process.executableURL = URL(fileURLWithPath: "/bin/zsh/")
    
    var args : [String]!
    args = []
    args.append("-c")
    args.append("open '/Users/xxxx/Desktop/myApp.app/Contents/MacOS/myApp'")
    process.arguments = args
    process.terminationHandler = { (process) in
      print("\ndidFinish: \(!process.isRunning)")
    }
    do {
       try process.run()
     } catch {}
    
    let app = NSApplication.shared
    app.setActivationPolicy(.regular)
    app.activate(ignoringOtherApps:true)
    app.run()
    

    “-c”之后的字符串取决于您要完成的任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多