Internationalisation
Bundled Plugins API
ipc
Provides Hammerspoon with the ability to create both local and remote message ports for inter-process communication.
The most common use of this module is to provide support for the command line tool hs which can be added to your terminal shell environment with hs.ipc.cliInstall. The command line tool will not work unless the hs.ipc module is loaded first, so it is recommended that you add require("hs.ipc") to your Hammerspoon init.lua file (usually located at ~/.hammerspoon/init.lua) so that it is always available when Hammerspoon is running.
This module is based heavily on code from Mjolnir by Steven Degutis.

API Overview

API Documentation

Deprecateds

Signature
hs.ipc.cliGetColors() -> table
Type
Deprecated
Description
Signature
hs.ipc.cliResetColors()
Type
Deprecated
Description
Signature
hs.ipc.cliSetColors(table) -> table
Type
Deprecated
Description

Functions

cliColors

Signature
hs.ipc.cliColors([colors]) -> table
Type
Function
Description
Get or set the terminal escape codes used to produce colorized output in the hs command line tool
Parameters
  • colors - an optional table or explicit nil specifying the colors to use when colorizing output for the command line tool. If you specify an explicit nil, the colors will revert to their defaults. If you specify a table it must contain one or more of the following keys with the terminal key sequence as a string for the value:
  • initial - this color is used for the initial tagline when starting the command line tool and for output to the Hammerspoon console that is redirected to the instance. Defaults to "\27[35m" (foreground magenta).
  • input - this color is used for input typed in by the user into the cli instance. Defaults to "\27[33m" (foreground yellow).
  • output - this color is used for output generated by the commands executed within the instance and the results returned. Defaults to "\27[36m" (foreground cyan).
  • error - this color is used for lua errors generated by the commands executed within the instance. Defaults to "\27[31m" (foreground red).
Returns
  • a table describing the colors used when colorizing output in the hs command line tool.
Notes
  • For a brief intro into terminal colors, you can visit a web site like this one http://jafrog.com/2013/11/23/colors-in-terminal.html
  • Lua doesn't support octal escapes in it's strings, so use \x1b or \27 to indicate the escape character e.g. ipc.cliSetColors{ initial = "", input = "\27[33m", output = "\27[38;5;11m" }
Signature
hs.ipc.cliInstall([path][,silent]) -> bool
Type
Function
Description
Installs the cmdpost command line tool
Parameters
  • path - An optional string containing a path to install the tool in. Defaults to /usr/local
  • silent - An optional boolean indicating whether or not to print errors to the CommandPost Error Log
Returns
  • A boolean, true if the tool was successfully installed, otherwise false
Notes
  • If this function fails, it is likely that you have some old/broken symlinks. You can use hs.ipc.cliUninstall() to forcibly tidy them up
Signature
hs.ipc.cliSaveHistory([state]) -> boolean
Type
Function
Description
Get or set whether or not the command line tool saves a history of the commands you type.
Parameters
  • state - an optional boolean (default false) specifying whether or not a history of the commands you type into the command line tool should be saved between sessions.
Returns
  • the current, possibly changed, value
Notes
  • If this is enabled, your history is saved in hs.configDir .. ".cli.history", which is usually "~/.hammerspoon/.cli.history".
  • If you have multiple invocations of the command line tool running at the same time, only the history of the last one cleanly exited is saved; this is a limitation of the readline wrapper Apple has provided for libedit and at present no workaround is known.
Signature
hs.ipc.cliSaveHistorySize([size]) -> number
Type
Function
Description
Get or set whether the maximum number of commands saved when command line tool history saving is enabled.
Parameters
  • size - an optional integer (default 1000) specifying the maximum number of commands to save when hs.ipc.cliSaveHistory is set to true.
Returns
  • the current, possibly changed, value
Notes
  • When hs.ipc.cliSaveHistory is enabled, your history is saved in hs.configDir .. ".cli.history", which is usually "~/.hammerspoon/.cli.history".
  • If you have multiple invocations of the command line tool running at the same time, only the history of the last one cleanly exited is saved; this is a limitation of the readline wrapper Apple has provided for libedit and at present no workaround is known.

cliStatus

Signature
hs.ipc.cliStatus([path][,silent]) -> bool
Type
Function
Description
Gets the status of the hs command line tool
Parameters
  • path - An optional string containing a path to look for the hs tool. Defaults to /usr/local
  • silent - An optional boolean indicating whether or not to print errors to the Hammerspoon Console
Returns
  • A boolean, true if the hs command line tool is correctly installed, otherwise false
Signature
hs.ipc.cliUninstall([path][,silent]) -> bool
Type
Function
Description
Uninstalls the hs command line tool
Parameters
  • path - An optional string containing a path to remove the tool from. Defaults to /usr/local
  • silent - An optional boolean indicating whether or not to print errors to the CommandPost Error Log
Returns
  • A boolean, true if the tool was successfully removed, otherwise false
Notes
  • This function used to be very conservative and refuse to remove symlinks it wasn't sure about, but now it will unconditionally remove whatever it finds at path/bin/cmdpost and path/share/man/man1/cmdpost.1. This is more likely to be useful in situations where this command is actually needed (please open an Issue on GitHub if you disagree!)

Constructors

localPort

Signature
hs.ipc.localPort(name, fn) -> ipcObject
Type
Constructor
Description
Create a new local ipcObject for receiving and responding to messages from a remote port
Parameters
  • name - a string acting as the message port name.
  • fn - the callback function which will receive messages.
Returns
  • the ipc object
Notes
  • a remote port can send messages at any time to a local port; a local port can only respond to messages from a remote port
Signature
hs.ipc.remotePort(name) -> ipcObject
Type
Constructor
Description
Create a new remote ipcObject for sending messages asynchronously to a local port
Parameters
  • name - a string acting as the message port name.
Returns
  • the ipc object
Notes
  • a remote port can send messages at any time to a local port; a local port can only respond to messages from a remote port

Methods

delete

Signature
hs.ipc:delete() -> None
Type
Method
Description
Deletes the ipcObject, stopping it as well if necessary
Parameters
  • None
Returns
  • None

isRemote

Signature
hs.ipc:isRemote() -> boolean
Type
Method
Description
Returns whether or not the ipcObject represents a remote or local port
Parameters
  • None
Returns
  • true if the object is a remote port, otherwise false
Notes
  • a remote port can send messages at any time to a local port; a local port can only respond to messages from a remote port

isValid

Signature
hs.ipc:isValid() -> boolean
Type
Method
Description
Returns whether or not the ipcObject port is still valid or not
Parameters
  • None
Returns
  • true if the object is a valid port, otherwise false

name

Signature
hs.ipc:name() -> string
Type
Method
Description
Returns the name the ipcObject uses for its port when active
Parameters
  • None
Returns
  • the port name as a string
Signature
hs.ipc:sendMessage(data, msgID, [waitTimeout], [oneWay]) -> status, response
Type
Method
Description
Sends a message from a remote port to a local port
Parameters
  • data - any data type which is to be sent to the local port. The data will be converted into its string representation
  • msgID - an integer message ID
  • waitTimeout - an optional number, default 2.0, representing the number of seconds the method will wait to send the message and then wait for a response. The method may block up to twice this number of seconds, though usually it will be shorter.
  • oneWay - an optional boolean, default false, indicating whether or not to wait for a response. It this is true, the second returned argument will be nil.
Returns
  • status - a boolean indicathing whether or not the local port responded before the timeout (true) or if an error or timeout occurred waiting for the response (false)
  • response - the response from the local port, usually a string, but may be nil if there was no response returned. If status is false, will contain an error message describing the error.
Last modified 1mo ago