MacGap.Task.create(path, callback)

Creates a task object to execute a system command.

Arguments

Argument description
path A string path to the executable system command, ‘/usr/ls’ or ‘/bin/cat’
callback A callback function to receive the result of the command

Callback

The callback will be invoked when the task is complete. A result object will be passed to the function.

Property description
status An integer, 0 for success or non-zero for
stdIn ?
stdErr ?
stdOut A string representing what std out of the system command

Example

This example creates a task calling the whoami function to identify the current user.

var myTask = MacGap.Task.create('/usr/bin/whoami', function(result) {
  if(result.status == 0) {
    console.log('The current user is ', result.stdOut);
  } else {
    console.log('An error occurred retrieving the current user.');
  }
});
myTask.pipeOutput = true;
myTask.launch();

MacGap.Task Object

The object to interface with the task.

Properties

Propertiy name Description
isRunning is task currently running, returns boolean
waitUntilExit primarily used as a setter, see NSTask reference for what this is
arguments set/get arguments to pass to task. Passed arguments need to be in the form of an array, i.e. task.arguments = [‘arg1’, 3, ‘whatever’];
environment set’s the tasks environment variables, defaults to application’s
pipeOutput converts the stdOut value to string format in the callback result object, default false

Methods

Method name Arguments description
launch none launches task
terminate none kills currently running task

Example

This example initiates a say task with an argument, launches the command and terminates it if it’s still running in 500ms.

var myTask = MacGap.Task.create('/usr/bin/say');
myTask.arguments = ['hello there'];
myTask.launch();

// terminate if running too long
setTimeout(function(){
  if(myTask.isRunning){
    myTask.terminate();
  }
}, 500);