How-To

Automate everything: Getting started with Hammerspoon and Lua

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.

mac-pc

About the author

Jessica Thornsby

Jessica Thornsby is a technical writer based in Sheffield. She writes about Android, Java, Kotlin and all things Apple. She is the co-author of O'Reilly's "iWork: The Missing Manual," and the author of "Android UI Design," from Packt Publishing.

Add Comment

Click here to post a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.