Platforms to show: All Mac Windows Linux Cross-Platform

Back to NSTaskMBS class.

NSTaskMBS.arguments as string()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The command arguments that should be used to launch the executable.

NSTaskMBS.Constructor

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The constructor.

On success the handle value is not zero.

NSTaskMBS.Destructor

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 15.3 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The destructor.

NSTaskMBS.environment as dictionary

Type Topic Plugin Version macOS Windows Linux iOS Targets
property Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
A dictionary of variables for the environment from which the receiver was launched.

The dictionary keys are the environment variable names.
(Read and Write computed property)

NSTaskMBS.interrupt

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sends an interrupt signal to the receiver and all of its subtasks.

If the task terminates as a result, which is the default behavior, an NSTaskDidTerminateNotification gets sent to the default notification center. This method has no effect if the receiver was already launched and has already finished executing. If the receiver has not been launched yet, this method raises an NSInvalidArgumentException.
It is not always possible to interrupt the receiver because it might be ignoring the interrupt signal. interrupt sends SIGINT.

NSTaskMBS.launch

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Launches the task represented by the receiver.
Example
// Launch "ls -l -a -t" in the current directory, and then read the result into a string:

dim task as new NSTaskMBS

task.launchPath = "/bin/ls"

dim arguments(-1) as string = array("-l", "-a", "-t")

task.setArguments arguments

dim pipe as new NSPipeMBS

task.setStandardOutput pipe

dim file as NSFileHandleMBS = pipe.fileHandleForReading

task.launch

dim data as string = file.readDataToEndOfFile
dim text as string = DefineEncoding(data, encodings.UTF8)

MsgBox text

Raises an NSInvalidArgumentException if the launch path has not been set or is invalid or if it fails to create a process.
If you get an exception with posix_spawn and error 13, that's a permission denied.

NSTaskMBS.resume as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Resumes execution of the receiver task that had previously been suspended with a suspend message.

If multiple suspend messages were sent to the receiver, an equal number of resume messages must be sent before the task resumes execution.

NSTaskMBS.setArguments(arguments() as string)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the command arguments that should be used to launch the executable.
Example
// Performing complex pipelines.
// You can create multiple NSTasks and a bunch of NSPipes and hook them together,
// or you can use the "sh -c" trick to feed a shell a command, and let it parse
// it and set up all the IPC. This pipeline cats /usr/share/dict/words, finds
// all the words with 'ham' in them, reverses them, and shows you the last 5.

dim task as new NSTaskMBS

task.LaunchPath="/bin/sh"

dim arguments(-1) as string

arguments.Append "-c"
arguments.Append "cat /usr/share/dict/words | grep -i ham | rev | tail -5"

task.setArguments arguments

NSTaskMBS.setStandardError(p as NSFileHandleMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the standard error for the receiver.

This method can be used with NSPipeMBS or NSFileHandleMBS object.

If file is an NSPipe object, launching the receiver automatically closes the write end of the pipe in the current task. Don't create a handle for the pipe and pass that as the argument, or the write end of the pipe won't be closed automatically.
If this method isn't used, the standard error is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException if the receiver has already been launched.

See also:

Some examples using this method:

NSTaskMBS.setStandardError(p as NSPipeMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the standard error for the receiver.

This method can be used with NSPipeMBS or NSFileHandleMBS object.

If file is an NSPipe object, launching the receiver automatically closes the write end of the pipe in the current task. Don't create a handle for the pipe and pass that as the argument, or the write end of the pipe won't be closed automatically.
If this method isn't used, the standard error is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException if the receiver has already been launched.

See also:

NSTaskMBS.setStandardInput(p as NSFileHandleMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the standard input for the receiver.

file: The standard input for the receiver, which can be either an NSFileHandle or an NSPipe object.

If file is an NSPipe object, launching the receiver automatically closes the read end of the pipe in the current task. Don't create a handle for the pipe and pass that as the argument, or the read end of the pipe won't be closed automatically.
If this method isn't used, the standard input is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException if the receiver has already been launched.

See also:

NSTaskMBS.setStandardInput(p as NSPipeMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the standard input for the receiver.

file: The standard input for the receiver, which can be either an NSFileHandle or an NSPipe object.

If file is an NSPipe object, launching the receiver automatically closes the read end of the pipe in the current task. Don't create a handle for the pipe and pass that as the argument, or the read end of the pipe won't be closed automatically.
If this method isn't used, the standard input is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException if the receiver has already been launched.

See also:

NSTaskMBS.setStandardOutput(p as NSFileHandleMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the standard output for the receiver.

file: The standard output for the receiver, which can be either an NSFileHandle or an NSPipe object.

If file is an NSPipe object, launching the receiver automatically closes the write end of the pipe in the current task. Don't create a handle for the pipe and pass that as the argument, or the write end of the pipe won't be closed automatically.
If this method isn't used, the standard output is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException if the receiver has already been launched.

See also:

Some examples using this method:

NSTaskMBS.setStandardOutput(p as NSPipeMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the standard output for the receiver.

file: The standard output for the receiver, which can be either an NSFileHandle or an NSPipe object.

If file is an NSPipe object, launching the receiver automatically closes the write end of the pipe in the current task. Don't create a handle for the pipe and pass that as the argument, or the write end of the pipe won't be closed automatically.
If this method isn't used, the standard output is inherited from the process that created the receiver. This method raises an NSInvalidArgumentException if the receiver has already been launched.

See also:

NSTaskMBS.standardError as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the standard error file used by the receiver.

The standard error file used by the receiver.

Standard error is where all diagnostic messages are sent. The object returned is either an NSFileHandle or an NSPipe instance, depending on what type of object was passed to setStandardError.

NSTaskMBS.standardInput as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the standard input file used by the receiver.

The standard input file used by the receiver.

Standard input is where the receiver takes its input from unless otherwise specified. The object returned is either an NSFileHandle or an NSPipe instance, depending on what type of object was passed to the setStandardInput method.

NSTaskMBS.standardOutput as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the standard output file used by the receiver.

Standard output is where the receiver displays its output. The object returned is either an NSFileHandle or an NSPipe instance, depending on what type of object was passed to the setStandardOutput method.

NSTaskMBS.suspend as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Suspends execution of the receiver task.

Returns true if the receiver was successfully suspended, false otherwise.

Multiple suspend messages can be sent, but they must be balanced with an equal number of resume messages before the task resumes execution.

NSTaskMBS.terminate

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sends a terminate signal to the receiver and all of its subtasks.

If the task terminates as a result, which is the default behavior, an NSTaskDidTerminateNotification gets sent to the default notification center. This method has no effect if the receiver was already launched and has already finished executing. If the receiver has not been launched yet, this method raises an NSInvalidArgumentException.
It is not always possible to terminate the receiver because it might be ignoring the terminate signal. terminate sends SIGTERM.

NSTaskMBS.waitUntilExit

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Tasks MBS MacCocoa Plugin 9.7 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Block until the receiver is finished.
Example
dim args(-1) as string
dim task as NSTaskMBS = NSTaskMBS.launchedTaskWithLaunchPath("/bin/ls", args)

task.waitUntilExit

MsgBox "done"

This method first checks to see if the receiver is still running using isRunning. Then it polls the current run loop using NSDefaultRunLoopMode until the task completes.

Some examples using this method:

The items on this page are in the following plugins: MBS MacCocoa Plugin.


The biggest plugin in space...