Thereβs no shortage of automation tools available for macOS, but if you want to automate every part of your Mac, then Hammerspoon is one of the most powerful and versatile automation tools out there.
Rather than building workflows via menus and drag-and-drop interfaces, Hammerspoon is built on a powerful Lua scripting engine. While this does mean that getting to grips with Hammerspoon requires significant time and effort, once youβve mastered the basics youβll be able to create custom notifications, your own menu bar items, and generally automate pretty much every task on your Mac.
In this article, Iβm going to get you up and running with Hammerspoon, and create a few simple scripts that display various notifications.
Hammerspoon Quickstart
Letβs start by getting Hammerspoon setup:
- Head over to its GitHub page and download the latest stable release. Make sure you download the Hammerspoon zip file, rather than the source code!
- Once the file has finished downloading, unzip it and launch the resulting app.
- The first time you launch Hammerspoon, itβll present you with a preference pane. You can select and deselect the various options, depending on your preferences, but you must enable accessibility in order to use Hammerspoon properly, so give the βEnable Accessibilityβ button a click.
- When prompted, click βOpen System Preferences.β This should launch macOSβ regular βSecurity & Privacyβ window.
- Make sure the βPrivacyβ tab is selected.
- Click the little padlock in the bottom-left corner, and enter your admin password.
- Find the βHammerspoonβ app, and give its accompanying checkbox a click.
- Close the βSecurity & Privacyβ window.
- At this point, you should see Hamerspoonβs Lua console. Select βFile > Open Configβ from the Hammerspoon menu bar.
You should now see a βinit.luaβ text file. This is where weβll be creating our first script.
Your first Hammerspoon script: Displaying a notification
Letβs start with something simple that you can test straight away, by creating a keyboard shortcut that triggers a notification.
Copy/paste the following into the init.lua window:
hs.hotkey.bind({"cmd", "h"}, "W", function() hs.alert.show("Hello from Hammerspoon!") end)
- Select βFile > Saveβ from the menu bar.
- Select the βHammerspoonβ icon from your Macβs menu bar and then click βReload Config.β
- Press the βCommand + h + wβ keys on your keyboard. Your custom notification should now appear onscreen.
Getting Wi-Fi notifications
Now we know how to issue a notification, the next step is displaying a notification in response to certain events.
If you donβt have a reliable Wi-Fi network, or your Mac regularly switches between different networks, then it may help to display notifications about the current state of your Wi-Fi connection.
In this section, weβre going to create a script thatβll display a notification whenever the Wi-Fi signal is lost, and then displays another notification every time your Mac connects to a new network, complete with the name of the network that itβs just connected to.
Copy/paste the following into your init.lua file:
wifiwatcher = hs.wifi.watcher.new(function () net = hs.wifi.currentNetwork() if net==nil then hs.notify.show("You lost Wi-Fi connection","","","") else hs.notify.show("Connected to Wi-Fi network","",net,"") end end) wifiwatcher:start()
- Select βFile > Saveβ from your Macβs menu bar.
- Select the βHammerspoonβ icon and choose βReload config.β
Try disconnecting from your Wi-Fi network, by selecting the βWi-Fiβ icon in your Macβs menu bar and choosing βTurn off Wi-Fi.β You should see a notification warning you that the Internet is disconnected. Reconnect to your network, and it should trigger another notification, complete with the name of the network that youβve just connected to.
Running multiple Hammerspoon scripts
We may have pasted our Wi-Fi script over our original βHello from Hammerspoonβ script, but itΒ is possible to run multiple Hammerspoon scripts side by side. In this final section, weβre going to create our scripts as separate .lua files, and then reference each script from that all-important init.lua file.
- Select βFile > Open config.β This launches the default βinit.luaβ file.
- Select βFile > Renameβ¦β
- Give this file the name βhello.lua.β
- Copy/paste your βHello from Hammerspoonβ script into this file.
- Select βFile > Save.β
- Close this window.
- Select βFile > Open config.β Once again this launches the βinit.luaβ file.
- Select βFile > Renameβ¦β and give this file the name βwifi.lua.β
- Copy/paste your Wi-Fi script into this window.
- Click βFile > Save.β Close the window.
At this point, we have a separate βhello.luaβ and βwifi.luaβ file. We now need to reference both of these files, from init.lua.
- Select βFile > Open config.β This launches the default βinit.luaβ file β at last, this is the file we actually need!
- Reference βhello.luaβ and βwifi.luaβ,β by copy/pasting the following script into βinit.lua.β
local wifi = require('wifi') local hello = require('hello')
- Select βFile > Save.β
You can now put these three scripts to the test:
- Select the βHammerspoon icon from your Macβs menu bar, and then click βReload config.β
- Press the βCommand + h + wβ keys β this should trigger the βHello from Hammerspoonβ notification.
- Disconnect from the Wi-Fi, and you should get a notification warning you about the lost connection.
Where next?
The great thing about Hammerspoon, is that you donβt have to write your own scripts, as thereβs countless ready-made scripts available online. Whenever you find a script that you want to use, simply save it as a .lua file and then reference this file from your main βinit.luaβ file.
Before you go
After spending over 20 years working with Macs, both old and new, theres a tool I think would be useful to every Mac owner who is experiencing performance issues.
CleanMyMac is highest rated all-round cleaning app for the Mac, it can quickly diagnose and solve a whole plethora of common (but sometimes tedious to fix) issues at the click of a button. It also just happens to resolve many of the issues covered in the speed up section of this site, so Download CleanMyMac to get your Mac back up to speed today.

Add Comment