18 ms·
Hammerspoon – Lua-based powerful tool automation of macOS
- avgcorrection 5y agoI don’t use Mac but it seems that this is the app that keeps popping up whenever I find out that some kind of desktop/window manager automation is easier on Mac than on Linux. Nice.
- I_complete_me 5y agoI thought AHK on Windows was awesome but was disappointed when I moved permanently to Linux and found it was not ported. Autokey is doing an ok stand-in job. I suspect Hammerspoon is good. I've never been on a Mac, maybe some day when I sell the mare.
- mmargerum 5y agohmmm I could use this to spoof "productivity" monitoring software. I was planning on build my own app to do this but this looks like a nice alternative. Thanks!
- usui 5y agoThe only shortcoming I run into frequently with Hammerspoon is the fact that it is not low-level enough in the keyboard listening stack. I use Karabiner-Elements which solves most of the things I need such as literally remapping the “fn” key because there’s no way for Hammerspoon to know if the “fn” key is pressed, only the more standard modifier keys. It also gets very tricky because I replace Caps Lock with “fn” and combine it with other modifier keys. It also can’t distinguish between “Left shift” and “Right shift” keys for example. Overall though I’m extremely grateful for Hammerspoon and if it ever stops working I might consider moving to Linux as my personal machine. I am able to chain key presses infinitely and categorize the different functions. For example, I press “Caps Lock” + “Left shift” + “a” to open a global key chording menu, press “w” for my “window management” category which opens another key chord menu, then press “w” again to maximize my current window. I replace the final “w” with “m” to minimize, “p” to open the most recently used window in a stack of windows in an application. Not only that, these keys are case sensitive so that effectively doubles my space of usable infinite key chords. Hammerspoon is so amazing for developer workflows after you put the time in.
- Graziano_M 5y agoI'm a big fan of hammerspoon, but not so much Lua. I also use emacs with Doom, where a lot of bindings are behind a 'leader key'. I found an awesome framework called 'spacehammer'[1] that fits very well into the way I like to work. It similarly hides binding behind a leader, and it's written in Fennel, a lisp that compiles to Lua. I feel like I get to expand the customizability of Emacs out to my whole system and I love it. Hammerspoon is pretty bare on its own so I suggest you check out spacehammer even if it's just a show case of the potential of hammerspoon. [1] https://github.com/agzam/spacehammer https://github.com/agzam/spacehammer
- eccentric_j 5y agoWas using a few paid apps for various automation tasks on OS X: - Hazel for automated tasks - Mosaic for layout shortcuts - Moom for layout grid drawing Since discovering spacehammer, was able to use it to replace all those tools. Even had the opportunity to create one of my dream features: Can highlight any code sample in a browser or most other apps, press leader-t and select the tmux session to send the code, turning just about any page into a repl driven workflow. A few years back I started contributing to spacehammer and refactored it to use a centralized config to make it easier for people to jump in and customize the menu and features. Lua is ok to me but much prefer lisp languages like fennel, code feels more expressive and faster to produce using paredit lisp editing shortcuts in my editor.
- endymi0n 5y agoReally nice at first sight. I'm getting melancholic vibes of AutoHotKey reading the docs, which was arguably my first entry into professional programming. Knowing nothing about coding, I built a service desk automation back then — with (network file-) shared hotkeys and a real GUI, all in one single file... good times. Anyway, ever since switching to MacOS more than a decade ago, I've always been searching for an AHK analog for those simple keystroke automations, text-based, with just enough of a glue language to hold it all together — Automator never quite cut it for me, after wielding the full power of AHK, it always felt too limited, rigid and clicky-colory. Even though I don't have immediate use cases for it anymore, it makes me very happy that I know where to go back now!
- artdigital 5y agoHave you tried AppleScript scripting? You can glue applications together and even use JavaScript if you’re not a fan of AppleScript
- endymi0n 5y agoDid some, but the syntax wasn't my cup of tea. The _really_ great feature of AHK to me was having the triggers and the actions all in one place though, which is something I couldn't really replicate with AppleScript that only has the actions, with the triggers in Automator. So with that runtime you could easily just write whatever would happen if you press which key combo (or do cronjob-like things managed by the runtime) and then write what happens inline. Write it all to a single script, copy it to a coworker's computer, run it, several dozen key combos just work now. That's something I couldn't manage to pull off in AppleScript or Automator, but then again maybe I was just too dumb.
- 1123581321 5y agoTry Keyboard Maestro or BetterTouchTool. They remind me the most of AHK out of all the Apple options. I also really like Hammerspoon. The desktop automation scene on macOS is strong.
- riston 5y agoI really like HS especially filling the macOS missing features place like for windows management + either opening/focusing specific applications with shortcuts.
- pjm331 5y agoI like hammerspoon. I’ve been using it for years and never got beyond setting up window resizing with the visual grid - which I think is like one of the very first steps in the tutorial - but I’d recommend it just for that.
- nsonha 5y agoDoes anyone knows a cross platform tools that can map keyboard keys to mouse button? Preferably in a layer?
- Arubis 5y agoI’ve especially enjoyed using Hammerspoon in combination with the prebaked config “Spacehammer” (https://github.com/agzam/spacehammer https://github.com/agzam/spacehammer), which takes a lot of inspiration from Spacemacs (and, of course, enables a systemwide “edit this text field with emacs” workflow).
- faizshah 5y agoIs there something like this for python?
- kbd 5y agoThe language used for this is incidental. The important part is all the OS interfaces exposed. If you know Python, you'll feel comfortable in Lua quickly.
- faizshah 5y agoIm just looking for some way to slap something like this on top of some python scripts. Currently I’m only aware of calling applescript like this for python: https://stackoverflow.com/a/41318195 https://stackoverflow.com/a/41318195
- Oddskar 5y agoProbably faster to just re-write in Lua.
- Syzygies 5y agoI fell briefly in love with Hammerspoon, but stopped using it. My MacOS automation is now my QMK keyboard calling Alfred workflows; I use Yabai for window management. I admire Lua, but ran into some sharp edges learning to program in it. Still, one can get up to speed quickly if one knows other scripting languages. Hammerspoon window management was deathly slow. Yabai is close to instantaneous.
- luaisgarbage 5y agoLua is nothing but sharp edges and gotchas. I hate Lua more than I hate JavaScript.
- eddyg 5y agoDoesn’t Yabai require disabling SIP?
- hirvi74 5y agoCertain advanced features do, but a majority of its features does not require disabling SIP.
- Oddskar 5y agoI’ve never had any problem with latency when using Hammerspoon for window management.
- cranekam 5y agoHammerspoon is great. I use it to manage windows (eg set vscode and a browser side-by-side when connected to an external display, full screen when not), add a hotkey to translate selected text with Deepl, to mute sound when the machine sleeps, and to launch a terminal with an fzf-based browser history searcher that beats Firefox’s hands down. It’s easy to use and has great docs. Lua is fun to write. HS is a really nice project.
- redleather 5y agoDo you mind sharing your code for the translation and fzf-history hotkeys? Those sound like useful applications.
- 9935c101ab17a66 5y agoNot OP, but I found this after a little bit of searching: https://gist.github.com/dshnkao/10865f32d69e40dc591e08e3af970e9d https://gist.github.com/dshnkao/10865f32d69e40dc591e08e3af97... Shouldn't be that hard to spawn a terminal with this running from hammerspoon. Here's some Spoons that handle translation: https://github.com/Hammerspoon/Spoons/blob/master/Source/DeepLTranslate.spoon/init.lua https://github.com/Hammerspoon/Spoons/blob/master/Source/Dee... https://github.com/Hammerspoon/Spoons/blob/master/Source/PopupTranslateSelection.spoon/init.lua https://github.com/Hammerspoon/Spoons/blob/master/Source/Pop... I have quite a bit of experience with HS, let me know if you have any questions / need any help.
- christiangenco 5y agoI love Hammerspoon. It's one of the first must-have-for-a-usable-laptop tools I set up when I get a new MacBook. Here are the top ways I'm using it right now: 1) Hide/show apps similar to how iTerm lets you bind a hotkey to hide/show a terminal. I've got ctrl+space set to Vimcal, alt+space set to midnight.app (a time tracker I'm building), and ctrl+alt+space set to Things. 2) Start/stop playing my work playlist of lofi hiphop. I got tired of the friction around opening Spotify, going into my work playlist, hitting play, waiting several moments for the playlist to load, etc, so I downloaded a bunch of mp3s from YouTube and put them in ~/Music/work/. Now my work music is a single keyboard shortcut (semicolon+m) and a few miliseconds away. 3) Set up screen recording. There are a few things that need to happen before I start a screen recording (opening CamHead.app, setting my screen resolution, and showing the dock at a certain height so I can later crop the video to 16:9) and I have it all bound to a single hotkey (semicolon+r). I've got another hotkey set up to unset it (semicolon+e). 4) You may have noticed I'm using keyboard shortcuts with semicolon as a modifier key. That's done with Hammerspoon too! I've got an extra layer of hotkeys available to me to set up whatever else I can think of in the future. The code I wrote to use semicolon like this sometimes breaks if I try to type too many actual semicolons in a row but I usually rely on JS Beautify to add those for me.
- aasasd 5y ago> I got tired of the friction around opening Spotify, going into my work playlist, hitting play, waiting several moments for the playlist to load, etc, so I downloaded a bunch of mp3s from YouTube and put them in ~/Music/work/. Now my work music is a single keyboard shortcut (semicolon+m) and a few miliseconds away. Aka how everyone did it before Spotify took off. But actually, I'm told time and again that Spotify has some APIs to control it—presumably when desktop apps are used (dunno for sure because don't use it myself). Personally I'd use Alfred for that over shortcuts.
- avgcorrection 5y agoYes. There are terminal applications to control Spotify. You need to add an API key in order to use them/it. So apparently you can write your own controller. The Spotify app also has to be running in order to use the terminal application (at least the one that I tried).
- pkukkapalli 5y agoJust curious do people use these types of automations to help with coding/software engineering? If so, in what way?
- Rebelgecko 5y agoI use a Hammerspoon-based clipboard manager which is incredibly helpful
- deleted 5y ago[deleted]
- wasyl 5y agoI like Hammerspoon, but it's a bit of a pain to write the scripts with Lua - I don't use the language often and it usually takes a while to figure out syntax and necessary stdlib/Hammerspoon functions. Does someone have a hint on how to set up e.g. VSCode or IJ with Lua support and completion for Hammerspoon functions?
- luxurytent 5y agoI’ve been learning Lua as part of Advent of Code. Honestly, the language has its quirks buts it’s so tiny that you could pick it up quite quickly. It’s become enjoyable to write —- I recommend!
- deleted 5y ago[deleted]
- dorian-graph 5y agoI use this! - Mute my computer when I put it to sleep or when I change wifi networks - Hyper key - Mail.app menubar indicator - Spotify menubar current song - Shortcut to move mouse cursor to another monitor - Window switcher that only lists apps in the current workspace
- gregwebs 5y agoCan you share your config?
- dorian-graph 5y agohttps://github.com/jesse-c/dotfiles/tree/main/.hammerspoon https://github.com/jesse-c/dotfiles/tree/main/.hammerspoon
- cmsj 5y agoSince we've hit the front page of HN again, I've just released Hammerspoon 0.9.92, which contains a few fixes and some additions, but mostly refactored a significant amount of internal project structure to allow our build system to be replaced with something that isn't 5 years old and terrible. As a result, it's possible that something broke which we didn't find in our internal testing. Please file GitHub issues :) Happy Christmas y'all!
- luckman212 5y agoHammerspoon (and Alfred) are the 2 apps that practically guarantee I'll never leave macOS. Just too damn useful. Thank you
- lycopodiopsida 5y agoThank you very much for your work! I am slowly oscillating between Keyboard Maestro and Hammerspoon, as well as between Alfred and Launchbar, but currently I am again in the Hammerspoon/Launchbar side of the pool :) One side I've noticed Hammesrpoon is a bit difficult to work with is debugging it can be done with logging to the console, but is not very convenient. Are there plans to introduce something more advanced at some point in the future?
- rufugee 5y agoI've used ZeroBrane to debug HS in the past: https://github.com/Hammerspoon/hammerspoon/issues/1268 https://github.com/Hammerspoon/hammerspoon/issues/1268
- lycopodiopsida 5y agoAh, almost, but it seems that ZeroBrane Studio is not arm-ready :( dlopen(/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/bin/clibs53/socket/core.dylib, 0x0006): tried: '/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/bin/clibs53/socket/core.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/core.dylib' (no such file)
- 5y ago
- rcarmo 5y agoI've been using Phoenix (https://github.com/kasper/phoenix https://github.com/kasper/phoenix), which is hackable in JavaScript (here's my config: https://gist.github.com/rcarmo/5132874cdaf2755e42907508802e864f https://gist.github.com/rcarmo/5132874cdaf2755e42907508802e8...) and has a smaller scope (only window management), but haven't found it compelling enough to keep hacking at my config, so I'm back to Moom (https://manytricks.com/moom/ https://manytricks.com/moom/), which would be perfect if it supported chaining sequences of sizing instructions (i.e., tile a window twice to the left to turn it from 1/2 to 1/3 width).
- rcarmo 5y agoIn case people are looking for more of these for Mac or Windows, well... I've been making a list, and checking it twice: - https://taoofmac.com/space/apps/window_managers https://taoofmac.com/space/apps/window_managers
- pasiaj 5y agoI've hooked up Hammerspoon to a bunch of APIs for fast lookups: translation, autocorrect, company lookup, etc. Translate (Requires Google Translate API key) https://github.com/pasiaj/Translate-for-Hammerspoon https://github.com/pasiaj/Translate-for-Hammerspoon Autocorrect: https://github.com/nathancahill/Anycomplete https://github.com/nathancahill/Anycomplete
- beaugunderson 5y agoOne of my more useful things, cmd+shift+m and cmd+shift+u to minimize all windows and un-minimize all windows of the active application. The un-minimize gets used more often (e.g. I may have a few Chrome windows minimized during my workday that I want to un-minimize en masse after work is done). local cmdShift = {'cmd', 'shift'} hs.hotkey.bind(cmdShift, 'm', function() local app = hs.application.frontmostApplication() for _, window in pairs(app:allWindows()) do window:minimize() end end) hs.hotkey.bind(cmdShift, 'u', function() local app = hs.application.frontmostApplication() for _, window in pairs(app:allWindows()) do window:unminimize() end end)
- nighthawk454 5y agoInteresting, I knew about cmd+option+m which does the minimizing part already. Weirdly doesn't seem to be an un-minimize shortcut though.
- beaugunderson 5y ago> Weirdly doesn't seem to be an un-minimize shortcut though. Yup, the existing minimize shortcut's missing twin led me to learn a bit more Lua so I could implement it :)
- beaugunderson 5y agoAnother fun thing you can do is install Hammerspoon as your URL handler, and make certain URLs open in different Chrome profiles--any work websites open in my work Chrome profile, websites related to a game I play opens in a Chrome profile specific to that, and anything else opens up in a personal Chrome profile. I've also setup cmd+shift+1/2/3 to brings the aforementioned profiles into the foreground, so e.g. cmd+shift+2 brings my work Chrome profile to the foreground.
- NateEag 5y agoI've wanted to send YouTube links directly to VNC for ages, while having all others open in the default browser, but have never taken the time to hack up a listener. I already use Hammerspoon, so this should be a piece of cake to add. Thanks for mentioning this ability!
- cmer 5y agoLove Hammerspoon! I wish there was an easier way to find great scripts, though. They seem to be all over the internet in random places.
- xenodium 5y agoAnother happy Hammerspoon user reporting. I feel Hammerspoon's Lua is to macOS what elisp is to Emacs. You can use it to glue all sorts of wonderful things. I wrote a bit about it here: https://xenodium.com/emacs-utilities-for-your-os https://xenodium.com/emacs-utilities-for-your-os (building my own Alfred/Quicksilver-like experience) and https://xenodium.com/cycling-window-layouts-via-hammerspoon https://xenodium.com/cycling-window-layouts-via-hammerspoon (cycle through my preferred window layouts). edit: typos
- kbd 5y agoIf anyone cares, here's my config: https://github.com/kbd/setup/blob/master/HOME/.hammerspoon/init.lua https://github.com/kbd/setup/blob/master/HOME/.hammerspoon/i... It shows off a tiny bit of what you can do with Hammerspoon: - window and app management - keyboard shortcuts for common apps (eg. hyper+T for text editor, hyper+B for browser, hyper+S for shell) - if you can code a tiny bit of Lua there's no need for apps like Spectacle, Rectangle, Moom, etc. because it's easy to position apps wherever you want. eg. hyper+L arranges my browser+editor+terminal in a standard layout, and I have other shortcuts to set apps to halves or thirds of the screen, or to another monitor. - fuzzy-find popups (like Alfred/fzf) are built-in. I use it to choose audio input/output. - a replacement for Caffeine (menubar icon to keep computer from going to sleep) I have my "hyper" key bound to caps lock using Karabiner Elements (but it still works as a normal caps lock if you hold it for half a second). Here's that config: https://github.com/kbd/setup/blob/master/HOME/.config/karabiner/karabiner.json https://github.com/kbd/setup/blob/master/HOME/.config/karabi...
- VTimofeenko 5y agoThank you so much for your sample config. I recently got a Macbook for work, and coming back to non-tiling wm has been a challenge, especially with multiple monitors. With this config I am finally back to my comfortable meta+{h,l} tiling, dedicated shortcuts for applications and easy way to move application between monitors. And it took less than 10 minutes. Excellent way to start a Monday.
- kbd 5y agoAwesome, you made my day better knowing that it helped someone. Thanks for your comment! FWIW the rest of my (Mac-based) system config is all open source (both the files I linked are in the same repo) so feel free to steal anything else.
- VTimofeenko 5y agoThanks! I have perused some of the configs and will definitely take a few snippets from zshrc. Also, kdiff3 looks quite interesting. I have been relying on vimdiff/colordiff.
- NateEag 5y agoI'm a big fan of Hammerspoon. At least a few years ago, OS X's window management when plugging/unplugging external monitors was pretty bad. Thanks to Hammerspoon, I don't have to think about it at all - I plug in monitors when I want to and all my windows resize and move themselves based on rules I defined. It means stepping away from my desk or coming back to it is a no-brainer. Maybe it's not needed these days, but it sure was a lifesaver back when I first adopted it. My config, if people are interested: https://github.com/NateEag/dotfiles/blob/master/src/.hammerspoon/init.lua https://github.com/NateEag/dotfiles/blob/master/src/.hammers...
- tonymet 5y agoFyi applescript offers a JS engine
- p2hari 5y agoLove Hammerspoon, I have a small snippet to store bookmarks. I copy the url, open the shortcut, add tags and it is saved to sql lite file. local function insertDB(db, linkval, value) local url = tostring(linkVal) local categ = tostring(value) db:exec[=[ INSERT INTO bookmarks VALUES (NULL,`url`,`categ`) ]=] end hs.hotkey.bind({"alt", "ctrl"}, "S", function() db = hs.sqlite3.open('/dropbox/user/bookmarks.db') local insert_stmt = assert( db:prepare("INSERT INTO bookmarks VALUES (NULL, ?, ?)") ) local url = hs.pasteboard.getContents() local tag, value = hs.dialog.textPrompt(url, "Please enter category:"); if tag == 'OK' then insert_stmt:bind_values(url, value) insert_stmt:step() insert_stmt:reset() -- insertDB(db, linkVal, value) end end)
- nonissue 5y agoMy pretty extensive (but poorly organized) config: https://github.com/nonissue/hammerspoon https://github.com/nonissue/hammerspoon Features include: - TOTP menubar app (that automatically types the code when clicked) - A menubar app for quickly changing screen resolution - A menubar sleep timer - Window management with convenient hot keys and undo functionality - Capslock rebinding to ESC when pressed alone, CTRL when pressed with other keys - A utility to allow you to both save a screenshot to disk AND have it copied to clipboard (AFAIK you can't do this natively in macOS). There's other a bunch of other stuff, but those are the highlights. If you have any questions about it, please let me know! (email: andy@nonissue.org or open an issue on github).
- harryvederci 5y agoDoes anyone have a wrapper for this and suckless it's dmenu? I'd love to have one config that delivers the same workflow for my macos and my Debian system. Maybe it's a nobrainer with a simple shell script, I haven't looked into it yet myself.