luahammerspoon

How to use the hammerspoon CLI?


I'd like to be able to run a command like hs -m foo -c bar -- baz and run some code using bar and/or baz. The hs -h help message says that baz should be available via _cli._args or _cli.args, but I can't figure out where the _cli value exists.

Here's the code I have:

local ipc = require('hs.ipc')

local port

function fooHandler()
    print('Hello, World!')
end

port = ipc.localPort('foo', fooHandler)

The output of hs -m foo -c bar -- baz is:

-- Legacy mode enabled --
nil

I'm sure there's a simple answer, but I can't find it.


Solution

  • Although I was unable to figure out how to use it with a local port, I was able to figure out how it works with the default remote one. Here's the code:

    require('hs.ipc')
    function bar(arg)
      print("Hello from bar! The arg is ".. arg)
    end
    

    If you run hs -c "bar('baz')", you'll get the following output:

    Hello from bar! The arg is baz