Class

Process

Using the NSTask class, your program can run another program as a subprocess and can monitor that program’s execution. An NSTask object creates a separate executable entity; it differs from NSThread in that it does not share memory space with the process that creates it.

Overview

A task operates within an environment defined by the current values for several items: the current directory, standard input, standard output, standard error, and the values of any environment variables. By default, an NSTask object inherits its environment from the process that launches it. If there are any values that should be different for the task, for example, if the current directory should change, you must change the value before you launch the task. A task’s environment cannot be changed while it is running.

An NSTask object can only be run once. Subsequent attempts to run the task raise an error.

Nested Types

Process.TerminationReason

These constants specify the values that are returned by terminationReason.

Symbols

Creating and Initializing an NSTask Object

class func launchedProcess(launchPath: String, arguments: [String])

Creates and launches a task with a specified executable and arguments.

init()

Returns an initialized NSTask object with the environment of the current process.

Returning Task Information

var arguments: [String]?

Sets the command arguments that should be used to launch the executable.

var currentDirectoryPath: String

Sets the current directory for the receiver.

var environment: [String : String]?

Sets the environment for the receiver.

var launchPath: String?

Sets the receiver’s executable.

var processIdentifier: Int32

Returns the receiver’s process identifier.

var standardError: Any?

Sets the standard error for the receiver.

var standardInput: Any?

Sets the standard input for the receiver.

var standardOutput: Any?

Sets the standard output for the receiver.

Running and Stopping a Task

func interrupt()

Sends an interrupt signal to the receiver and all of its subtasks.

func launch()

Launches the task represented by the receiver.

func resume()

Resumes execution of the receiver task that had previously been suspended with a suspend() message.

func suspend()

Suspends execution of the receiver task.

func terminate()

Sends a terminate signal to the receiver and all of its subtasks.

func waitUntilExit()

Block until the receiver is finished.

Querying the Task State

var terminationStatus: Int32

Returns the exit status returned by the receiver’s executable.

var terminationReason: Process.TerminationReason

Returns the reason the task was terminated.

Configuring an NSTask Object

var arguments: [String]?

Sets the command arguments that should be used to launch the executable.

var currentDirectoryPath: String

Sets the current directory for the receiver.

var environment: [String : String]?

Sets the environment for the receiver.

var launchPath: String?

Sets the receiver’s executable.

var standardError: Any?

Sets the standard error for the receiver.

var standardInput: Any?

Sets the standard input for the receiver.

var standardOutput: Any?

Sets the standard output for the receiver.

Task Termination Handler

var terminationHandler: ((Process) -> Void)?

Invoked when the task is completed.

Constants

Process.TerminationReason

These constants specify the values that are returned by terminationReason.

Notifications

class let didTerminateNotification: NSNotification.Name

Posted when the task has stopped execution. This notification can be posted either when the task has exited normally or as a result of terminate() being sent to the NSTask object. If the NSTask object gets released, however, this notification will not get sent, as the port the message would have been sent on was released as part of the task release. The observer method can use terminationStatus to determine why the task died. See Ending an NSTask for an example.

Relationships

Inherits From

Conforms To