Hammerspoon: The Best Way of Passing Path Arguments to HS?

Created on 27 Dec 2019  Â·  8Comments  Â·  Source: Hammerspoon/hammerspoon

Hello everyone,
I’m interested in finding out if there’s an ideal way of passing paths to HS functions from other programs — LaunchBar, for example. Paths selected in LB can be passed on to LaunchBar “Actions”, which are basically scripts that accept any argument.

From thereon, I can do pretty much whatever I want, and in context of passing input to HS:

  1. One option is AppleScript, with something like tell app "Hammerspoon" to execute lua code "spoon.mySpoon.myFunc(" & thePaths & …"), but I feel it's shaky and prone to AS’ idiosyncracies and escaping issues.

  2. Another options is to use hs.urlevent.bind, and then run a shell script in LB, something along the lines of open -g hammerspoon://myFunc?path1=foo. However, I would like the extra work required in preparing each argument and formatting the URL, and then again it feels like a lot more can go wrong with this approach compared to simply calling an executable with space-delimited paths as arguments.

Thanks in advance!

Most helpful comment

You can use the hs command line tool to control Hammerspoon.

For example, if you have this function in init.lua:

function myTest()
    print("test")
end

...then trigger hs -c "myTest()" from Terminal, it will write "Test" in the Console.

All 8 comments

There's also: hs.ipc

Hi @latenitefilms,
I’ve read through the module’s docs, but still couldn’t figure out a way to pass arguments to HS from another app. Specifically, how will I be able to receive those arguments within the Lua code?

Thanks.

You can use the hs command line tool to control Hammerspoon.

For example, if you have this function in init.lua:

function myTest()
    print("test")
end

...then trigger hs -c "myTest()" from Terminal, it will write "Test" in the Console.

Thanks for the reply — it’s a shame I haven’t even considered checking if there’s a man for the hs tool. I had no idea it has such depth.

Anyway, how do I pass parameters to the function I specify after -c? I did the following:

  1. Listed some quoted, space-separated paths after the function, with or without a preceding —, for example: hs -c “myTest() '/Users/foo/bar' '/usr/local/bin’.
  2. To access _cli.args or _cli._args within the Lua code, the most reasonable thing to me was to treat args (or _args) as a property of the single parameter passed to the function (i.e. myTest(_cli)), but that didn’t work.

You can use command substitution to pass along the output of other commands as arguments.

For example, if you have this function in init.lua:

function alert(message)
    hs.alert(message)
end

You can use this Terminal command to display the date as an alert:

hs -c "alert(\"$(date)\")"

To clarify, by passing the -c argument to hs it:

Specifies a Hammerspoon command to execute. May be specified more than once and commands will be executed in the order they appear. Disables colorized output unless -N is present. Disables interactive mode unless -i is present. If -i is present or if stdin is a pipe, these commands will be executed first.

So you can essentially pass along ANY Lua code, in the similar way to how tell app "Hammerspoon" to execute lua code in AppleScript-land works.

Thanks! I think I’ll stick with urlevent. Seems safer when working with paths.

You can use the hs command line tool to control Hammerspoon.

For example, if you have this function in init.lua:

function myTest()
    print("test")
end

...then trigger hs -c "myTest()" from Terminal, it will write "Test" in the Console.

i am definitely missing something here, but where do i find the hs command line tool.

I installed Hammerspoon using brew

brew cask install hammerspoon

and searching through my $PATH env var I do not see any CLI cmd called hs

Update

wow that hs bin was really buried within the app bundle, and I ended up having to load the ipc module within ~/.hammerspoon/init.lua in order to even run the most trivial _print_ function.

Is there a particular place where this is documented somewhere?

Having to sift through github issues opened / closed to find info is somewhat a PITA.

_for future me_ 👴🏻

/Applications/Hammerspoon.app/Contents/Resources/extensions/hs/ipc/bin/hs

You can install the Command Line tool via:

hs.ipc.cliInstall()

For documentation on the tool see here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agzam picture agzam  Â·  3Comments

jiahut picture jiahut  Â·  3Comments

latenitefilms picture latenitefilms  Â·  3Comments

zhenwenc picture zhenwenc  Â·  4Comments

piskov picture piskov  Â·  4Comments