Create a gist now

Instantly share code, notes, and snippets.

Embed
NSTask Sample for Swift
import Cocoa
import Foundation
var str = "Hello, playground"
var task:NSTask = NSTask()
var pipe:NSPipe = NSPipe()
task.launchPath = "/bin/ls"
task.arguments = ["-la"]
task.standardOutput = pipe
task.launch()
var handle = pipe.fileHandleForReading
var data = handle.readDataToEndOfFile()
var result_s = NSString(data: data, encoding: NSUTF8StringEncoding)
print(result_s)
@Seasons7

This comment has been minimized.

Show comment
Hide comment
@Seasons7

Seasons7 Jul 22, 2015

save this file.
chmod +x nstask.swift
xcrun swift a.swift -f /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks

Owner

Seasons7 commented Jul 22, 2015

save this file.
chmod +x nstask.swift
xcrun swift a.swift -f /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks

@dautermann

This comment has been minimized.

Show comment
Hide comment
@dautermann

dautermann Aug 4, 2016

for what it's worth, pipe and task are immutable and should be let

that is to say:

                let task = NSTask()
                let pipe = NSPipe()

                task.launchPath = "/bin/ls"
                task.arguments = ["-la"]
                task.standardOutput = pipe
                task.launch()

the other variables might also need to be declared let.

for what it's worth, pipe and task are immutable and should be let

that is to say:

                let task = NSTask()
                let pipe = NSPipe()

                task.launchPath = "/bin/ls"
                task.arguments = ["-la"]
                task.standardOutput = pipe
                task.launch()

the other variables might also need to be declared let.

@ingconti

This comment has been minimized.

Show comment
Hide comment
@ingconti

ingconti Oct 1, 2016

for swift 3.0:

    // Create a Task instance (was NSTask on swift pre 3.0)
    let task = Process()

    // Set the task parameters
    task.launchPath = "/bin/ls"
    task.arguments = ["-laF@" , filePath]

    // Create a Pipe and make the task
    // put all the output there
    let pipe = Pipe()
    task.standardOutput = pipe

    // Launch the task
    task.launch()

    // Get the data
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)

    print(output!)

ingconti commented Oct 1, 2016

for swift 3.0:

    // Create a Task instance (was NSTask on swift pre 3.0)
    let task = Process()

    // Set the task parameters
    task.launchPath = "/bin/ls"
    task.arguments = ["-laF@" , filePath]

    // Create a Pipe and make the task
    // put all the output there
    let pipe = Pipe()
    task.standardOutput = pipe

    // Launch the task
    task.launch()

    // Get the data
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)

    print(output!)
@Gerst20051

This comment has been minimized.

Show comment
Hide comment

thanks! @ingconti

@pakLebah

This comment has been minimized.

Show comment
Hide comment
@pakLebah

pakLebah Nov 27, 2016

@ingconti: Do you have any sample how to interact with standardInput? And why Process is Task on Swift 3 for Linux? Thank you.

@ingconti: Do you have any sample how to interact with standardInput? And why Process is Task on Swift 3 for Linux? Thank you.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Mar 16, 2017

is it possible to run multiple commands?

ghost commented Mar 16, 2017

is it possible to run multiple commands?

@shishanyu

This comment has been minimized.

Show comment
Hide comment
@shishanyu

shishanyu Dec 24, 2017

Updated for Swift 4

//: Playground - noun: a place where people can play

import Cocoa
import Foundation

var task = Process()
var pipe = Pipe()

task.executableURL = URL(fileURLWithPath: "/bin/ls")
task.arguments = ["-la","/Users/"] //multiple options
task.standardOutput = pipe
try task.run()

var handle = pipe.fileHandleForReading
var data = handle.readDataToEndOfFile()
var printing = String (data: data, encoding: String.Encoding.utf8)
print(printing!)

Updated for Swift 4

//: Playground - noun: a place where people can play

import Cocoa
import Foundation

var task = Process()
var pipe = Pipe()

task.executableURL = URL(fileURLWithPath: "/bin/ls")
task.arguments = ["-la","/Users/"] //multiple options
task.standardOutput = pipe
try task.run()

var handle = pipe.fileHandleForReading
var data = handle.readDataToEndOfFile()
var printing = String (data: data, encoding: String.Encoding.utf8)
print(printing!)

@alisanie

This comment has been minimized.

Show comment
Hide comment
@alisanie

alisanie Jan 12, 2018

I'm using this method for running app but I got "Message from debugger: Terminated due to signal 9".Any idea?

 let path=("\(FileManager.default.currentDirectoryPath)/data/app/setup.pkg")
            
            let buildTask: Process = Process()
            
            //Path for open
            buildTask.launchPath = "/usr/bin/open"
            buildTask.arguments = [path]
            buildTask.launch()

Message from debugger: Terminated due to signal 9

I'm using this method for running app but I got "Message from debugger: Terminated due to signal 9".Any idea?

 let path=("\(FileManager.default.currentDirectoryPath)/data/app/setup.pkg")
            
            let buildTask: Process = Process()
            
            //Path for open
            buildTask.launchPath = "/usr/bin/open"
            buildTask.arguments = [path]
            buildTask.launch()

Message from debugger: Terminated due to signal 9

@welljsjs

This comment has been minimized.

Show comment
Hide comment
@welljsjs

welljsjs Feb 8, 2018

buildTask.launch() is not up to date anymore. Since High Sierra, Process().lauch() is deprecated. Use Process().run instead and catch the error that may be thrown.
See Apple Developer Documentation for launch()

This Tutorial goes a bit more into detail regarding NSTasks.

welljsjs commented Feb 8, 2018

buildTask.launch() is not up to date anymore. Since High Sierra, Process().lauch() is deprecated. Use Process().run instead and catch the error that may be thrown.
See Apple Developer Documentation for launch()

This Tutorial goes a bit more into detail regarding NSTasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment