在做个监控剪贴板的程序,如果复制的是需求网址就直接调用外部命令执行。想好了就动手,监控剪贴板这块儿很快搞定,问题出在使用 Process
(原 NSTask
)调用外部命令上。
先上代码(全部代码在 GitHub):
let process = Process() let outpipe = Pipe() process.standardOutput = outpipe let errpipe = Pipe() process.standardError = errpipe process.launchPath = "/usr/local/bin/acmpv" process.arguments = ["-f=mp4", copiedString] process.launch() let outdata = outpipe.fileHandleForReading.availableData let outputString = String(data: outdata, encoding: String.Encoding.utf8) ?? "" NSLog("Ouput: %@", outputString) let errdata = errpipe.fileHandleForReading.availableData let errString = String(data: errdata, encoding: String.Encoding.utf8) ?? "" NSLog("Err: %@", errString) process.waitUntilExit()
运行后并没有执行命令,而是在 console
输出:
warning: the gestalt selector gestaltsystemversion is returning 10.9.0 instead of 10.12.0...
在 Google 上搜索半天没有一条有用的。请问如何正确的在 Swift 中调用外部命令?
付费偷看金额在0.1-10元之间
一周热门 更多>