gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:07:32 24 4
gpt4 key购买 nike

我从 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 信息到目前为止还没有帮助我。

谢谢!!

最佳答案

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

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”后面的字符串取决于您想要完成的任务。

关于swift - 如何从 cocoa 应用程序运行终端命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61387309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com