Posted By

nguyenvu on 06/17/15


Tagged

command execute swift


Versions (?)

Execute a command in swift


 / Published in: Other
 

URL: http://square-the-circle.com/2014/08/03/executing-a-system-command-from-a-macos-app-swift/

let commandOutput = executeCommand("/bin/echo", ["Hello, I am here!"]) println("Command output: (commandOutput)")

  1. func executeCommand(command: String, args: [String]) -> String {
  2.  
  3. let task = NSTask()
  4.  
  5. task.launchPath = command
  6. task.arguments = args
  7.  
  8. let pipe = NSPipe()
  9. task.standardOutput = pipe
  10. task.launch()
  11.  
  12. let data = pipe.fileHandleForReading.readDataToEndOfFile()
  13. let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)
  14.  
  15. return output
  16.  
  17. }

Report this snippet  

You need to login to post a comment.