Skip to content

Processes

The Processes module similarly to the windows module is used to enumerate and manipulate processes running on the local machine.

All processes

The all() methods enumerates all processes on the local machine. It returns an array of process proxy objects.

javascript
var ps = Processes.all();
for (var i=0; i<ps.length; i++) {
  // then do something with each process proxy
}

Current

Get the current process for the application (for which the flow is defined).

javascript
var p = Processes.current;
Debug.showDialog(p.name);

Spawning new processes

The spawn(...) method can be used to create new processes. It takes 4 arguments;

  • path to the executable to launch
  • arguments for the executable (optional - default null)
  • working directory (optional - default null)
  • shell (boolean) whether to launch the process in a shell environment - this must be set to true for url-handlers to activate (optional - default false)

It returns a process proxy object fronting the process spawned.

javascript
var p = Processes.spawn("C:\\Path\\To\Executable.exe");
Debug.showDialog(p.name);

Process proxy

Kill

Kills a process.

javascript
var p = Processes.all()[0];
p.kill();

or with more violence:

javascript
p.kill({ force: true });

which will use taskkill to kill the process.

Wait for a process to exit

The wait(...) method will wait for the given process to exit. It takes an integer, the maximum number of milliseconds to wait for the process as its argument. It returns a boolean indicating whether the processes exited (true) or the given timespan elapses (false).

javascript
// Wait max 1s for the first process to exit
if (Processes.all()[0].wait(1000)) {
  // it exited
} else {
  // 1s elapsed
}

Send input (via standard-in)

Sending some input to a running process is achieved with the stdin(...) method.

This can normally only be done for processes spawned by yourself via the Processes.spawn(...)](#spawning-new-processes) method.

javascript
var p = Processes.spawn(...);
p.stdin("hello");

Read from process output (standard-out)

Reading from the output of a process is done via the stdout(...) method. It takes an int - the number of lines to read - and returns a task which completes with the lines read as an array of strings once the given number of lines has been read.

This can normally only be done for processes spawned by yourself via the Processes.spawn(...)](#spawning-new-processes) method.

javascript
var p = Processes.spawn(...);
var lines = null;
// Read 3 lines, then kill the process
p.stdout(3).then(function(threelines) {
  lines = threelines;
  p.kill();
});
p.wait(20000);
Debug.ger(lines);

It is also possible to read from standard-error output - simply use the stderr(...) method instead of stdout(...).

An alternative approach to reading from stdout when you dont know how many lines you need to read upfront is to use processStdout and processStderr.

js
var lines = [];
// p is a ProcessProxy
// Here we'll process 100 lines but the termination condition could be anything
p.processStdout(
  function(line) {
    // do something with the given line
    lines.push(line);
    // if you return true the processing will continue - false it will stop
    return lines.length < 100; 
  },
  // Deadline is 10s
  10000
);
for (var i=0; i<lines.length; i++) {
  Log.info("key", "Line #"+i+":"+lines[i]);
}

Read output from cmd.exe/powershell.exe

As a practical example of the use of Processes.spawn, we can retrieve all registered printers on the system. Note that the termination criteria here amount to reading until we are just getting empty lines out, with an upper limit for safety.

js
var p = Processes.spawn('cmd.exe', '/c "wmic printer list brief"');
var lines = [];
var emptyLines = 0;
p.processStdout(
  function(line) {
    if (line) {
      lines.push(line);
      // A non-empty line was received: Reset empty counter
      emptyLines = 0;
    } else {
      emptyLines++;
    }
    // if you return true the processing will continue - false it will stop
    return lines.length < 100 && emptyLines < 2; 
  },
  // Deadline is 10s
  10000
);
for (var i=0; i<lines.length; i++) {
  Log.info('key', lines[i]);
}

The same thing can be done with powershell by replacing the first line with the following:

js
var p = Processes.spawn('powershell.exe', '-Command "wmic printer list brief"');

Process Id

Get the id of the process.

javascript
var pid = Processes.current.id;

Process name

Get the name of the process.

javascript
var pname = Processes.current.name;

Process path

Get the path of the executable for the process.

javascript
var path = Processes.current.path;

Process working directory

Get the working directory of the executable for the process.

javascript
var pwd = Processes.current.wd;

Process mem usage

Get the virtual or private memory (integers) usage of the process.

javascript
var virtualMem = Processes.current.vmem;
var privateMem = Processes.current.pmem;

Process exited?

Gets a boolean indicating whether the process has exited.

javascript
if (Processes.current.exited) {
  // whoops
}

Process uptime

Gets the number of milliseconds elapsed since the process was spawned (as longs as it has not exited).

javascript
var uptime = Processes.current.uptime;

Process arguments

Get the arguments supplied to the process - can only be counted on to return valid arguments if process was spawned by Manatee.

javascript
var args = Processes.current.arguments;

Process windows

javascript
process.mainWindow;

A list of all windows for process

javascript
process.windows;
// returns an array of WindowProxy objects

Process commandline

Get the full commandline incl arguments for the process.

javascript
process.commandLine;

Process filename

Get the full path to the filename of the executable.

javascript
process.fileName;