[英]How to execute `aerender` command line tool via NSTask/Process?
[英]NSTask get notified when a command can't execute “command not found”
我正在嘗試從我的應用程序運行終端命令/腳本,但是一切正常,但是當命令錯誤並且無法執行時,我得到如下信息:
但是這個“ / bin / bash:line ...”字符串不在我從任務中獲得的輸出字符串中,有什么方法可以在我的應用程序中獲取這些錯誤,也可以通過任何方式通知它們嗎?
我的代碼
// Create a new task
let task: Process = Process()
task.environment = env
task.launchPath = "/usr/bin/env"
task.arguments = ["/bin/bash", "-c", command.scriptCode]
// Assign output pipes
let pipe: Pipe = Pipe()
let outHandle: FileHandle = pipe.fileHandleForReading
task.standardOutput = pipe
outHandle.readabilityHandler = { pipe in
if let line = String(data: pipe.availableData, encoding: String.Encoding.utf8) {
if line.contains("command not found") {
// never triggered 😩
} else {
print("New ouput: \(NSDate() )\(line)")
}
} else {
print("Error decoding data: \(pipe.availableData)")
}
}
您正在尋找的是NSTask
或Process
類的standardError屬性。 就像您對standardOutput
一樣,將管道分配給該屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.