Return to Snippet

Revision: 69430
at June 17, 2015 20:36 by nguyenvu


Initial Code
func executeCommand(command: String, args: [String]) -> String {
    
    let task = NSTask()
    
    task.launchPath = command
    task.arguments = args
    
    let pipe = NSPipe()
    task.standardOutput = pipe
    task.launch()
    
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)
    
    return output
    
}

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

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

Initial Title
Execute a command in swift

Initial Tags
command

Initial Language
Other