
Hammerspoon – macOS automation with Lua - antfarm
https://www.hammerspoon.org/
======
m0th87
In case it's helpful, here's how I use hammerspoon:

* As a replacement for the classic caffeine app. [1]

* To provide shortcuts for window managements. [2]

* To shutdown bluetooth on sleep. Otherwise, devices I have setup to pair with both my mac and phone will tend to pair with the mac, even though it's asleep. [3]

1:
[https://gist.github.com/heptal/50998f66de5aba955c00](https://gist.github.com/heptal/50998f66de5aba955c00)

2: [https://github.com/miromannino/miro-windows-
manager](https://github.com/miromannino/miro-windows-manager)

3:
[https://gist.github.com/ysimonson/fea48ee8a68ed2cbac12473e87...](https://gist.github.com/ysimonson/fea48ee8a68ed2cbac12473e87134f58)

~~~
iLemming
I update my Slack status to "home|work" based on what wifi my laptop gets
connected.

~~~
yinyang_in
how do you do it, can you share your config

------
nathancahill
Hammerspoon is one of my favorite tools. I especially like the low latency of
the system hooks, other tools like Automator or Folder Actions have a couple
second delay betweeen the trigger and the action. Hammerspoon is pretty much
instant.

On top of that, Hammerspoon is very actively developed and the maintainers are
super knowledgeable and responsive.

I've shared a couple scripts on here before, like Anycomplete (Google
autocomplete anywhere):
[https://news.ycombinator.com/item?id=13065670](https://news.ycombinator.com/item?id=13065670)

~~~
eastendguy
It is a truly great tool. The caveat is that using Hammerspoon is essentially
programming. So it is "only" suitable for technical users like developers.

For the casual Mac user (e. g. product manager or the guy from sales) the
visual Sikuli or UI.Vision RPA tools are more suitable for creating automation
scripts.

------
inglor
I mean, macOS already has built in automation (with Automator) that you can
write in AppleScript or JavaScript JXA. [https://github.com/JXA-Cookbook/JXA-
Cookbook](https://github.com/JXA-Cookbook/JXA-Cookbook) is not very updated
but its wiki contains quite a bit of useful recipes.

Why would I prefer this over the built in option?

~~~
saagarjha
AppleScript is awful to write and JXA is impossible to find documentation for.

~~~
kccqzy
One trick to finding JXA documentation is simply to look at the AppleScript
documentation and translate the code to JavaScript syntax, or, depending on
what you've doing, look at the Objective-C Cocoa documentation and translate
the syntax.

For example, to obtain the current date and one day from now, in Objective-C
you might write:

    
    
        NSDate *now = [NSDate date];
        NSDate *tmr = [date dateByAddingTimeInterval: 86400];
    

The equivalent in JavaScript is simply

    
    
        var now = $.NSDate.date;
        var tmr = now.dateByAddingTimeInterval(86400);

~~~
saagarjha
> look at the AppleScript documentation and translate the code to JavaScript
> syntax

Yeah, I can’t do this. It’s non-obvious to me how it translates until I
actually stumble upon the correct thing.

~~~
deergomoo
AppleScript looks fantastically simple and easy to pick up until you actually
come to write some. Then, because of the "conversational" structure of it, it
feels more like guessing the right magic incantation than programming.

It certainly reads nicer to non-programmers, but I feel like writing it
requires just as much learning as any more "traditional" scripting language,
so I'm not really sure what the benefit is.

~~~
Wowfunhappy
I'm more of a hobbyist than a professional programmer, so take that for what
you will:

I really like Applescript. I _agree_ that it's harder to write, and especially
harder to write _well_ , because functional Applescript won't necessarily come
out as english sentences, if you aren't careful to construct it the right way.

But the enhanced readability makes it worthwhile. I love that I can look at my
own Applescript code and just read what it's doing in english.

I actually wish I could do _more_ with Applescript than just Mac scripting.
I've been thinking for a few years now that I ought to play around with
Hypercard, but PowerPC emulators are a pain to get running. There's a modern
clone called SuperCard, but it costs $200...

P.S. The "open dictionary" option built into the Script Editor is helpful for
finding the right "incantation".

~~~
WaltPurvis
LiveCode is the modern (and vastly more powerful) version of HyperCard, and it
has a free community version: [https://livecode.com/](https://livecode.com/)

------
andai
I used Hammerspoon as an AutoHotKey replacement for automating repetitive
actions in games like Terraria.

I had macros for mining up, down, left, right, and in all diagonals
(eventually expanded to also placing dirt below me so I don't fall into a
chasm and die), and eventually integrated mining with placing rails for
underground transport!

My reasoning was that I enjoyed the action parts but not the walking parts, so
I made a train tunnel, but that was even more boring than walking, so I
automated it. Eventually I was having more fun automating the game than
playing it.

Oh, side note, getting infinite money and life in CheatEngine (also has Mac
version now) taught me an important life lesson. Using my cheat-superpowers I
defeated Wall of Flesh (who had been obstructing my progress for weeks), and
suddenly felt a deep sense of dread. I hadn't really achieved anything, I had
cheated! Suddenly I realized I had the exact same attitude towards the rest of
my life, always trying to find an easy way out, a way to win without playing
the game, and that this was a source of great dissatisfaction.

~~~
Quiark
Then you definitely should not learn about Minetest because it has open Lua
API and this is how I play it

------
dmazin
My favorite Hammerspoon use is binding applications to hotkeys, e.g. Ctrl-3
for editor, Ctrl-4 for terminal, Ctrl-5 for browser, etc. It is so much faster
going directly to an app than cycling through the apps in the alt-tab
switcher.

Additionally, I use it to bind caps lock to escape, unless it’s pressed with
another key, in which case it’s curl. So it’s great to not need Karabiner
anymore.

~~~
bcrosby95
> My favorite Hammerspoon use is binding applications to hotkeys

In linux and windows you can just use the superkey+1, 2, etc. I'm surprised
macos doesn't have the equivalent to this built in.

------
strathos
I moved at work from Ubuntu to Mac six months ago. I had AutoKey script for
getting my most used password with keepassxc-cli and pasted it as text, which
I had bound to Ctrl+F12. I tried to find an alternative to that on Mac and
Hammerspoon was the most frequent search result. I couldn't find an easy
enough example for me, though, so I went with Alfred + alfred-keepassxc
workflow. It's completely different way to get passwords, how ever I prefer
that now.

One funny thing while searching for information was that almost every
Hammerspoon discussion had the mandatory "why not AppleScript?!" comment.

~~~
hhas01
"why not AppleScript?!"

Rhetorical, shurely!?

------
merricksb
Another big discussion here 3 years ago:

[https://news.ycombinator.com/item?id=12097303](https://news.ycombinator.com/item?id=12097303)

(Understand it's not a dupe; sharing for info purposes).

~~~
flanbiscuit
I thought I recognized Hammerspoon! thanks

------
dowakin
For me one important addition to Hammerspoon is Karabiner-Elements. I've
binded my right Command and Option in a way so all my Hammerspoon shortcuts
are just 2 keys. Like Right Command+T to open terminal.

~~~
gimboland
If you want to go deeper into Karabiner-Elements, perhaps check out goku [0] —
it allows you to write Karabiner configuration in a much nicer configuration
language.

[0]
[https://github.com/yqrashawn/GokuRakuJoudo](https://github.com/yqrashawn/GokuRakuJoudo)

------
macrael
Please keep posting your little use cases!

I wrote a simple window manager that just lets me pick a set of windows across
applications and bring them all to the front and cycle through them.

[https://github.com/macrael/panes](https://github.com/macrael/panes)

It probably could be faster but it's so nice to be able to keep my terminal
and my editor frontmost while I'm running tests with `entr`.

------
wilsonmitchell
I recently picked up this tool and picked up someone's config that does some
modal keybindings similar to spacemacs [1]. It actually inspired me to hack
around with their config and then write a spacemacs layer for fennel-mode
which someone had written a decent version of already [2]. I'm a bit of an
emacs and fennel noob, but it seemed like a fun little problem to solve. I'm
especially interested in getting autocomplete working better. I've been trying
to base things off of other lisps' layers, but all ears for any tips on how to
approach it. I'll edit in my github later as I have everything local at the
moment.

[1]
[https://github.com/agzam/spacehammer](https://github.com/agzam/spacehammer)
[2] [https://gitlab.com/technomancy/fennel-
mode](https://gitlab.com/technomancy/fennel-mode)

~~~
iLemming
Hi, I'm the author of Spacehammer. You may want to check development branch of
the project. There are some major improvements. Sadly I've been swamped with
work lately and having trouble finding more time to work on it. One of the
things I'm banging my head around is adding a REPL that can work with
Hammerspoon's IPC. Once I figure that out, I'm planning to experiment on a
bunch of new things and improve existing features.

~~~
slumos
My idea is to extend fennel-mode with a fennel-hs-mode that will take
preprocess code by removing newlines[1] and wrapping in something like

    
    
      fennel.eval([=[ (code) ]=])
    

and then sending to the hs cli.

I haven't done much yet; this is the first time I've tried to write a real
Emacs mode. It would be much nicer to have a real Fennel REPL integrated in
Hammerspoon, but the above would be enough for things like eval-last-sexp and
org-babel-eval.

[1] _Sadly, the hs cli does not handle multi-line strings._

------
rcarmo
This is really nice. I repeatedly wish Apple hadn’t neutered AppleScript and
the scripting bridges to the point they are unusable for doing window
management and suchlike.

------
saagarjha
Love love love Hammerspoon; I use it every day. People point me to all their
little utility apps to keep their screen on or perform window snapping and I
just write a little script and bind it to my hyper key :)

~~~
jf
What do you use it for?

~~~
hibbelig
I use hs.hotkey.modal to make Alt-Space pop up a modal window that tells me
what I can do. Various keys open specific apps, e.g. Return opens Finder and
Space opens iTerm. Other keys move and resize the currently window to specific
areas. For example, I have a specific size and position for browser windows,
another one for Finder windows, a third one for instant messaging windows, and
a fourth one for terminal windows.

Very very rarely do I have to manually move windows around -- I find that
almost all of my needs are covered with a set of specific window positions and
sizes.

I also have a window selector that displays all windows and their names and
allows me to use substrings to filter the list. But I find I never use it.

I used to use Hammerspoon to implement a "vi navigation" mode, toggled by
tapping the Cmd key. In vi navigation mode, hjkl move the cursor, x deletes a
character, and so on. But I discovered that Karabiner Elements can do
something similar, and now I'm using that instead.

I used to use Hammerspoon for a "vi

------
iLemming
Hammerspoon is awesome, can't even imagine how difficult it would be without
it. I've built Spacemacs inspired config so I can do everything and still keep
my fingers in the home row
[https://github.com/agzam/spacehammer](https://github.com/agzam/spacehammer)

\- Tossing windows around?

\- Controlling music?

\- Jumping between apps?

\- Adding more convenient shortcuts for your favorite apps?

\- Editing any text anywhere with your favorite editor?

Why not modality then?

------
wlesieutre
Others have mentioned the window positioning features, but the other thing I
use Hammerspoon for is a dark mode toggle in my menu bar.

Not something most people are switching all the time, but if you're writing
CSS for a

    
    
        prefers-color-scheme: dark
    

media query, then being able to pop back and forth on demand is handy.

~~~
saagarjha
You can toggle between the two quickly from Safari’s Web Inspector.

~~~
wlesieutre
Just for the web view or for the whole system? I think I’d rather look at and
design dark mode content within the context of the whole screen in dark mode,
but I could see that being handy for making quick little fixes.

~~~
saagarjha
Just the current website. It’s faster than waiting for the entire computer to
switch…

------
k__
I used it to get my IDE window into a pixel perfect size for recording
screencasts. Pretty nifty tool.

------
lilyball
I love the idea of Hammerspoon but I have yet to figure out a single concrete
use-case for me that I actually care about. It feels like a lot of what I see
people talking about is window management but I don't want window management
(I actually _like_ overlapping windows). Jumping between apps doesn't really
matter either, I usually need my mouse with a lot of apps so it's not a big
deal to click them in the dock if cmd-tab isn't good. And I already use an
Alfred workflow for hotkeys to control iTunes.

I actually had Hammerspoon installed for multiple years on my home computer
and the only thing I ever wrote with it was a hotkey to reload my Hammerspoon
config.

~~~
save_ferris
While I’ve found hammerspoon works great for window management, I’ve recently
explored trying to create semi advanced automation workflows with it and I
agree that it’s not really fleshed out for this type of work (although it
seems doable)

------
fouc
Seems like it's Lua with hooks into the OSX api.

Lots of useful examples [https://www.hammerspoon.org/go/#table-of-
contents](https://www.hammerspoon.org/go/#table-of-contents)

------
statbit
I would probably have to stop using a mac without hammerspoon at this point.

For me it is indispensable as a hotkey launcher, keyboard, window
mover/resizer, clipboard history manager, stay-awake, GIPHY API lookup,
pomodoro timer, and a snippet manager.

My most used combo is ctrl (remapped capslock) - ' as a launcher, then C for
Chrome, J for IntelliJ, V for MacVim, ... keeps me off cmd-tab which keeps me
off seeing notification number in mail and slack.

Hammerspoon is my #1 tool for modding OSX to make it more usable.

------
zzamboni
I absolutely love Hammerspoon, it's my favorite Mac tool. My config is
available here, in case anyone wants some ideas of what it can do:
[https://github.com/zzamboni/dot-
hammerspoon/blob/master/init...](https://github.com/zzamboni/dot-
hammerspoon/blob/master/init.org)

------
deathtrader666
There's a great book by LeanPub on Hammerspoon.

~~~
zzamboni
I'm the author of that book! Glad you found it useful :)

------
theonething
I've been using the totally outdated Slate [0] for years now and it still
works through multiple MacOS upgrades. One day I'll port to Hammerspoon I keep
telling myself. It probably won't happen until Slate stops working.

[0] [https://github.com/jigish/slate](https://github.com/jigish/slate)

------
OldGuyInTheClub
Hammerspoon is invaluable for providing window rearranging functions lost when
Spectacle stopped working on my Mac. Unfortunately I haven't been able to
understand the structure well enough to create my own scripts.

------
methou
happy user. I LOVE Lua, and would happily use anything in lua.

------
fanf2
I mostly use Hammerspoon for moving windows to a fixed position with a
keypress, e.g. left half, right third, move to other monitor. Not fully
tiling, but close.

------
dep_b
I would love to have it to automate

    
    
        sudo killall -STOP -c usbd
    

Let's see if it works - Automator didn't work too well

~~~
saagarjha
Just curious, why do you need to do this?

~~~
the_other
Possibly for the bug/feature that causes an iPhone connected by USB to connect
and disconnect repeatedly on a ~2sec cycle, The bug leads to endless modals
appearing on the iPhone, and sometimes iTunes opening with each reconnection.

~~~
dep_b
That's right! Drove me nuts when I didn't know I could do this since the
problem would go away for a while when I bought a new cable.

~~~
saagarjha
Have you tried unloading the daemon instead of stopping it?

------
rado
Can it fix the language input switching (respect the default language and each
app's language)?

~~~
aasasd
Yes. I've made myself a config for that. Will share it in maybe a few hours.

However it works a bit slowly sometimes, but dunno if that's a problem with
Hammerspoon or with my machine.

------
bobbylarrybobby
Does anyone know how Hammerspoon compares to keyboard maestro?

~~~
alexruf
Hammerspoon is free, but you have to write your automation scripts in Lua. If
your comfortable with Lua this might be totally fine and give you a lot of
flexibility.

Keyboard Maestro on the other side is commercial software with a nice UI
editor were you can compose automation scripts by simply dragging and dropping
actions. Personally I use Keyboard Maestro and find it absolutely worth its
money. Even without any programming experience you can create powerfull
scripts within minutes. I use it as clipboard manager, snippet manager, text
expansion and much more.

------
sergiotapia
Also see WinTask for windows automation with VBA

------
tuananh
hammerspoon would be great if they have a plugin store where non-power user
can just browse and install plugin of their choice.

------
crimsonalucard
Anybody know if there is a python equivalent?

------
m00dy
what do you guys think about RPA market ?

------
armatav
This is siiiiick

