W3cubDocs

/Hammerspoon

hs.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

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

See hs.ipc.cliColors.

Source extensions/ipc/ipc.lua line 112
cliResetColors
Signature hs.ipc.cliResetColors()
Type Deprecated
Description

See hs.ipc.cliColors.

Source extensions/ipc/ipc.lua line 136
cliSetColors
Signature hs.ipc.cliSetColors(table) -> table
Type Deprecated
Description

See hs.ipc.cliColors.

Source extensions/ipc/ipc.lua line 124

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" }

  • Changes made with this function are saved with hs.settings with the following labels and will persist through a reload or restart of Hammerspoon: "ipc.cli.color_initial", "ipc.cli.color_input", "ipc.cli.color_output", and "ipc.cli.color_error"

Source extensions/ipc/ipc.lua line 63
cliInstall
Signature hs.ipc.cliInstall([path][,silent]) -> bool
Type Function
Description

Installs the hs 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 Hammerspoon Console
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
  • You may need to pre-create /usr/local/bin and /usr/local/share/man/man1 in a terminal using sudo, and adjust permissions so your login user can write to them
Source extensions/ipc/ipc.lua line 268
cliSaveHistory
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.

  • Changes made with this function are saved with hs.settings with the label "ipc.cli.saveHistory" and will persist through a reload or restart of Hammerspoon.

Source extensions/ipc/ipc.lua line 148
cliSaveHistorySize
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.

  • Changes made with this function are saved with hs.settings with the label "ipc.cli.historyLimit" and will persist through a reload or restart of Hammerspoon.

Source extensions/ipc/ipc.lua line 171
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
Source extensions/ipc/ipc.lua line 194
cliUninstall
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 Hammerspoon Console
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/hs and path/share/man/man1/hs.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!)
Source extensions/ipc/ipc.lua line 294

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
Source extensions/ipc/libipc.m line 78
remotePort
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
Source extensions/ipc/libipc.m line 128

Methods

delete
Signature hs.ipc:delete() -> None
Type Method
Description

Deletes the ipcObject, stopping it as well if necessary

Parameters
  • None
Returns
  • None
Source extensions/ipc/libipc.m line 338
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
Source extensions/ipc/libipc.m line 174
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
Source extensions/ipc/libipc.m line 195
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
Source extensions/ipc/libipc.m line 156
sendMessage
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 indicating 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.
Source extensions/ipc/libipc.m line 213

© 2014–2017 Hammerspoon contributors
Licensed under the MIT License.
https://www.hammerspoon.org/docs/hs.ipc.html