简体   繁体   English

如何从 cocoa 应用程序运行终端命令?

[英]How to run Terminal commands from cocoa app?

I have problems running a Terminal command from a Cocoa Application.我从 Cocoa 应用程序运行终端命令时遇到问题。 The input for the Terminal is real easy: /Users/.../Csvmidi </Users/.../test.csv> /Users/.../Melody.mid These are three inputs- three actual paths - are just written in a row and seperated by a spac: the first "Csvmidi" runs a Unix Application which converts the test.csv to an actual hearable MIDI file.终端的输入非常简单: /Users/.../Csvmidi </Users/.../test.csv> /Users/.../Melody.mid这三个输入——三个实际路径——只是写成一行并由一个空格分隔:第一个“Csvmidi”运行一个 Unix 应用程序,它将 test.csv 转换为实际的可听 MIDI 文件。 Through the terminal it works perfectly...通过终端它完美地工作......

I just don't get it to work via a Cocoa Application.我只是没有通过 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 {}

When I run the code it gives me either the error of an 75: unmatched" , but actually there isn't a quotation mark in the command - or I get "permission denied" errors.当我运行代码时,它给了我75: unmatched"的错误,但实际上命令中没有引号 - 或者我得到“权限被拒绝”错误。

I tried several bin folders like ( I really tried almost every possible): - /bin/zsh - /bin/csh - /bin/ksh -... What am i doing wrong --> I haven't found information in other Questions here and the Process, NSTask and Bundle informations from Apple haven't helped me so far.我尝试了几个 bin 文件夹,例如(我真的尝试了几乎所有可能的方法): - /bin/zsh - /bin/csh - /bin/ksh -...我做错了什么 --> 我没有在其他文件夹中找到信息到目前为止,这里的问题以及来自 Apple 的 Process、NSTask 和 Bundle 信息对我没有帮助。

Thanks!!谢谢!!

The following runs without error on my system:以下在我的系统上运行没有错误:

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()

The strings after "-c" would depend on what you are trying to accomplish. “-c”之后的字符串取决于您要完成的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM