Back when I used Windows as my daily OS, I used Task Scheduler a lot to automate small tasks11. The most memorable use was for keeping my Vencord fork up to date with upstream. Every time I logged in, a terminal window would pop up running git pull && pnpm build --dev for 10 seconds or so. I couldn’t find a clean way to hide the terminal popup, so I just lived with it. ↩. When I switched to macOS, I did some searching online and found that launchd jobs (cron jobs were deprecated) are the usual way to automate scheduled tasks on macOS.
However, I quickly got tired of creating a .plist file for every single automation I wanted to set up. Plists for launchd jobs have a lot of boilerplate for even simple jobs and I personally think XML is quite verbose compared to JSON or YAML. For example, one simple task I had was to periodically dump my installed packages from Homebrew for backup purposes. You would first have to create a plist file with the following contents:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>Label</key> <string>updatebrewfile</string> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/Users/newwares/bin/update-brewfile.sh</string> </array> <key>StandardErrorPath</key> <string>/tmp/updatebrewfile.err</string> <!-- apparently these were optional --> <key>StandardOutPath</key> <string>/tmp/updatebrewfile.out</string> <key>StartInterval</key> <integer>3600</integer></dict></plist>And then a separate file somewhere else with the actual script22. I actually use more arguments to dump tools that aren’t installed through Homebrew as well, like cargo/uv/npm/App Store, but I wanted to keep this snippet simple. ↩:
#!/bin/bash/opt/homebrew/bin/brew bundle dump --global --forceNeeding to keep track of 2 files and 19 lines of XML for a 1-line script was not okay. I quickly started searching for an alternative to launchd and came across Hammerspoon.
Hammerspoon is a scriptable macOS automation tool. It exposes many macOS APIs via Lua, so you have a lot of control over how you organize your automations and can do a lot more than just cron jobs. The code to do the same task of dumping a Brewfile now becomes:
local M = {}M.brewfile_timer = hs.timer.doEvery(3600, function() hs.execute("/opt/homebrew/bin/brew bundle dump --global --force")end)return MThis is much more concise! One pitfall when using Hammerspoon is that if you create a timer, watcher or anything that needs to be long-lived, you must ensure it is reachable throughout Hammerspoon’s runtime. Otherwise, the Lua garbage collector will clean it up and your watcher will stop working. Usually this means assigning the result to a variable and exposing it globally. In order to avoid cluttering the global scope, I assigned the result to a table that a loader file then requires. This is a common idiom in regular Lua as well.
Over time, I set up more sophisticated scripts to tweak macOS behaviour that I wouldn’t be able to do natively. Some of my current tweaks are:
-
Problem: My keyboard’s RGB config resets itself every time it loses connection.
Solution: Set up a USB watcher that calls a simple script using qmk-via-api to set my desired RGB as soon as my keyboard gets a USB connection. -
Problem: When I play music with earbuds in, my volume is sometimes set too high at first which causes discomfort.
Solution: Watch for Spotify’scom.spotify.client.PlaybackStateChangeddistributed notification and if I’m using earbuds, set my volume to a fixed number of ticks. -
Problem: When editing Markdown or Typst files in Neovim, I don’t have an easy way to do a split-screen live preview like in VS Code. The most well-known Markdown/Typst live preview plugins require opening a localhost URL in a web browser to see the preview.
Solution: This one was more involved than the other automations I had33. This took 50 lines of code in Hammerspoon and 5 lines in Neovim. ↩. The main idea is to have Neovim and Hammerspoon communicate through Hammerspoon URL schemes. Configure the Typst/Markdown preview Neovim plugin to also open the Hammerspoon URL scheme when starting the preview, passing the localhost URL as a query parameter. Then, in Hammerspoon, handle that URL event and launch a guest-mode web browser with the provided localhost URL. Simultaneously, arrange the Neovim window and the browser window to create a split-screen view. To clean up after quitting Neovim, close the browser and restore the original window layout.
A lot of the functionality I required (USB connection watching, checking the primary audio output) is built into Hammerspoon’s Lua API already, so it’s easy to use. Another great thing about being able to use Hammerspoon for all of my automation needs is that everything is in one place. That way, when something behaves unexpectedly, I only have to check one location.
Overall, I think Hammerspoon is a pretty painless way to tweak macOS. I managed to achieve everything that I wanted to do (for now) and it wasn’t difficult to learn Hammerspoon when I first tried it out.
Footnotes
-
The most memorable use was for keeping my Vencord fork up to date with upstream. Every time I logged in, a terminal window would pop up running
git pull && pnpm build --devfor 10 seconds or so. I couldn’t find a clean way to hide the terminal popup, so I just lived with it. ↩ -
I actually use more arguments to dump tools that aren’t installed through Homebrew as well, like cargo/uv/npm/App Store, but I wanted to keep this snippet simple. ↩
-
This took 50 lines of code in Hammerspoon and 5 lines in Neovim. ↩