#hs.task

Execute processes in the background and capture their output

Notes:

  • This is not intended to be used for processes which never exit. While it is possible to run such things with hs.task, it is not possible to read their output while they run and if they produce significant output, eventually the internal OS buffers will fill up and the task will be suspended.
  • An hs.task object can only be used once

#API Overview

Functions - API calls offered directly by the extension

Methods - API calls which can only be made on an object returned by a constructor


#API Documentation

#Functions

| | | | --------------------------------------------|-------------------------------------------------------------------------------------| | Signature | hs.task.new(launchPath, callbackFn[, streamCallbackFn][, arguments]) -> hs.task object | | Type | Function | | Description | Creates a new hs.task object | | Parameters |

  • launchPath - A string containing the path to an executable file. This must be the full path to an executable and not just an executable which is in your environment's path (e.g. /bin/ls rather than just ls).
  • callbackFn - A callback function to be called when the task terminates, or nil if no callback should be called. The function should accept three arguments: exitCode - An integer containing the exit code of the process stdOut - A string containing the standard output of the process stdErr - A string containing the standard error output of the process
  • streamCallbackFn - A optional callback function to be called whenever the task outputs data to stdout or stderr. The function must return a boolean value - true to continue calling the streaming callback, false to stop calling it. The function should accept three arguments: task - The hs.task object or nil if this is the final output of the completed task. stdOut - A string containing the standard output received since the last call to this callback stdErr - A string containing the standard error output received since the last call to this callback
  • arguments - An optional table of command line argument strings for the executable
| | Returns |
  • An hs.task object
| | Notes |
  • The arguments are not processed via a shell, so you do not need to do any quoting or escaping. They are passed to the executable exactly as provided.
  • When using a stream callback, the callback may be invoked one last time after the termination callback has already been invoked. In this case, the task argument to the stream callback will be nil rather than the task userdata object and the return value of the stream callback will be ignored.
| | Examples | None | | Source | extensions/task/libtask.m line 176 |


#Methods

Signaturehs.task:closeInput() -> hs.task object
TypeMethod
DescriptionCloses the task's stdin
Parameters
  • None
Returns
  • The hs.task object
Notes
  • This should only be called on tasks with a streaming callback - tasks without it will automatically close stdin when any data supplied via hs.task:setInput() has been written
  • This is primarily useful for sending EOF to long-running tasks
ExamplesNone
Sourceextensions/task/libtask.m line 323

Signaturehs.task:environment() -> environment
TypeMethod
DescriptionReturns the environment variables as a table for the task.
Parameters
  • None
Returns
  • a table of the environment variables for the task where each key is the environment variable name.
Notes
  • if you have not yet set an environment table with the hs.task:setEnvironment method, this method will return a copy of the Hammerspoon environment table, as this is what the task will inherit by default.
ExamplesNone
Sourceextensions/task/libtask.m line 745

Signaturehs.task:interrupt() -> hs.task object
TypeMethod
DescriptionInterrupts the task
Parameters
  • None
Returns
  • The hs.task object
Notes
  • This will send SIGINT to the process
ExamplesNone
Sourceextensions/task/libtask.m line 535

Signaturehs.task:isRunning() -> boolean
TypeMethod
DescriptionTest if a task is still running.
Parameters
  • None
Returns
  • true if the task is running or false if it is not.
Notes
  • A task which has not yet been started yet will also return false.
ExamplesNone
Sourceextensions/task/libtask.m line 683

Signaturehs.task:pause() -> boolean
TypeMethod
DescriptionPauses the task
Parameters
  • None
Returns
  • If the task was paused successfully, returns the task object; otherwise returns false
Notes
  • If the task is not paused, the error message will be printed to the Hammerspoon Console
  • This method can be called multiple times, but a matching number of hs.task:resume() calls will be required to allow the process to continue
ExamplesNone
Sourceextensions/task/libtask.m line 563

Signaturehs.task:pid() -> integer
TypeMethod
DescriptionGets the PID of a running/finished task
Parameters
  • None
Returns
  • An integer containing the PID of the task
Notes
  • The PID will still be returned if the task has already completed and the process terminated
ExamplesNone
Sourceextensions/task/libtask.m line 433

Signaturehs.task:resume() -> boolean
TypeMethod
DescriptionResumes the task
Parameters
  • None
Returns
  • If the task was resumed successfully, returns the task object; otherwise returns false
Notes
  • If the task is not resumed successfully, the error message will be printed to the Hammerspoon Console
ExamplesNone
Sourceextensions/task/libtask.m line 596

Signaturehs.task:setCallback(fn) -> hs.task object
TypeMethod
DescriptionSet or remove a callback function for a task.
Parameters
  • fn - A function to be called when the task completes or is terminated, or nil to remove an existing callback
Returns
  • the hs.task object
NotesNone
ExamplesNone
Sourceextensions/task/libtask.m line 254

Signaturehs.task:setEnvironment(environment) -> hs.task object | false
TypeMethod
DescriptionSets the environment variables for the task.
Parameters
  • environment - a table of key-value pairs representing the environment variables that will be set for the task.
Returns
  • The hs.task object, or false if the table was not set (usually because the task is already running or has completed)
Notes
  • If you do not set an environment table with this method, the task will inherit the environment variables of the Hammerspoon application. Set this to an empty table if you wish for no variables to be set for the task.
ExamplesNone
Sourceextensions/task/libtask.m line 770

Signaturehs.task:setInput(inputData) -> hs.task object
TypeMethod
DescriptionSets the standard input data for a task
Parameters
  • inputData - Data, in string form, to pass to the task as its standard input
Returns
  • The hs.task object
Notes
  • This method can be called before the task has been started, to prepare some input for it (particularly if it is not a streaming task)
  • If this method is called multiple times, any input that has not been passed to the task already, is discarded (for streaming tasks, the data is generally consumed very quickly, but for now there is no way to synchronize this)
ExamplesNone
Sourceextensions/task/libtask.m line 278

Signaturehs.task:setStreamingCallback(fn) -> hs.task object
TypeMethod
DescriptionSet a stream callback function for a task
Parameters
  • fn - A function to be called when the task outputs to stdout or stderr, or nil to remove a callback
Returns
  • The hs.task object
Notes
  • For information about the requirements of the callback function, see hs.task.new()
  • If a callback is removed without it previously having returned false, any further stdout/stderr output from the task will be silently discarded
ExamplesNone
Sourceextensions/task/libtask.m line 351

Signaturehs.task:setWorkingDirectory(path) -> hs.task object | false
TypeMethod
DescriptionSets the working directory for the task.
Parameters
  • path - a string containing the path you wish to be the working directory for the task.
Returns
  • The hs.task object, or false if the working directory was not set (usually because the task is already running or has completed)
Notes
  • You can only set the working directory if the task has not already been started.
  • This will only set the directory that the task starts in. The task itself can change the directory while it is running.
ExamplesNone
Sourceextensions/task/libtask.m line 401

Signaturehs.task:start() -> hs.task object | false
TypeMethod
DescriptionStarts the task
Parameters
  • None
Returns
  • If the task was started successfully, returns the task object; otherwise returns false
Notes
  • If the task does not start successfully, the error message will be printed to the Hammerspoon Console
ExamplesNone
Sourceextensions/task/libtask.m line 454

Signaturehs.task:terminate() -> hs.task object
TypeMethod
DescriptionTerminates the task
Parameters
  • None
Returns
  • The hs.task object
Notes
  • This will send SIGTERM to the process
ExamplesNone
Sourceextensions/task/libtask.m line 507

Signaturehs.task:terminationReason() -> exitCode | false
TypeMethod
DescriptionReturns the termination reason for a task, or false if the task is still running.
Parameters
  • None
Returns
  • a string value of "exit" if the process exited normally or "interrupt" if it was killed by a signal. Returns false if the termination reason is unavailable (the task is still running, or has not yet been started).
NotesNone
ExamplesNone
Sourceextensions/task/libtask.m line 710

Signaturehs.task:terminationStatus() -> exitCode | false
TypeMethod
DescriptionReturns the termination status of a task, or false if the task is still running.
Parameters
  • None
Returns
  • the numeric exitCode of the task, or the boolean false if the task has not yet exited (either because it has not yet been started or because it is still running).
NotesNone
ExamplesNone
Sourceextensions/task/libtask.m line 652

Signaturehs.task:waitUntilExit() -> hs.task object
TypeMethod
DescriptionBlocks Hammerspoon until the task exits
Parameters
  • None
Returns
  • The hs.task object
Notes
  • All Lua and Hammerspoon activity will be blocked by this method. Its use is highly discouraged.
ExamplesNone
Sourceextensions/task/libtask.m line 628

Signaturehs.task:workingDirectory() -> path
TypeMethod
DescriptionReturns the working directory for the task.
Parameters
  • None
Returns
  • a string containing the working directory for the task.
Notes
  • This only returns the directory that the task starts in. If the task changes the directory itself, this value will not reflect that change.
ExamplesNone
Sourceextensions/task/libtask.m line 379