//运行可执行文件
func runExecutableFile(_ executableURL: String, _ args: [String]) -> String? {
let task = Process()
//要运行的外部程序的路径
let executableURL = URL(fileURLWithPath: executableURL)
task.executableURL = executableURL
//使用通信通道获取执行程序的输出
let pipe = Pipe()
task.standardOutput = pipe
//设置可执行文件的操作目录
// task.currentDirectoryURL = URL(fileURLWithPath: "/users/dan/OneDrive/Documents/")
//可执行文件的所有参数
task.arguments = args
// //流程完成后调用
// task.terminationHandler = {
// _ in
// print("process run complete.")
// }
try! task.run()
task.waitUntilExit()
//捕获输出
let fileData = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String (data: fileData, encoding: String.Encoding.utf8)
// print("terminal output: (output!)")
// print("execution complete...")
return output
}
网友评论