Mac event driven automation using Hammerspoon.
Hammerspoon is a system event handler for Mac.
The code is written in Lua.
Includes:
- Auto-switching to Multi-Output for Shazaming when connecting headphones
- Automatically running Speed Test when connecting to a wifi network
Install Hammerspoon using Homebrew:
brew install hammerspoon --caskmkdir -p -v ~/githubgit clone https://github.com/HariSekhon/Hammerspoon ~/github/hammerspoonln -svFhw ~/github/hammerspoon ~/.hammerspoonOutput:
/Users/hari/.hammerspoon -> /Users/hari/github/hammerspoon
Finally, start Hammerspoon, you should see its icon in the menu bar.
open -a HammerspoonSet Hammerspoon to start at login (you can tick this in the Hammerspoon Preferences UI) or just run this:
osascript -e 'tell application "System Events" to make login item at end with properties {path:"/Applications/Hammerspoon.app", hidden:false}'Optionally allow pop-up desktop notifications from Hammerspoon so that your code can notify you when it's done something for you (I recommend Banners that automatically disappear without having to click them).
System Settings -> Notifications ->Hammerspoon:
open "x-apple.systempreferences:com.apple.Notifications-Settings.extension?bundleId=org.hammerspoon.Hammerspoon"Automatically switches from AirPods / Headphones connection to Multi-Output Audio Device and Blackhole input to be able to Shazam songs while watching a movie or TV show on AirPods / Headphones.
You will need to install the Blackhole audio driver and set up a Midi Multi-Output Audio device too.
See HariSekhon/Knowledge-Base - Audio page section for more details on this setup.
If you set your Sound output to the in-built Mac Speaker then the code resets the mic to use the standard Mac built-in Mic to keep the input and output aligned in an intuitive way.
If you set your Sound output to any device with Multi-Output or Blackhole in the name then
automatically switches to the Blackhole input for the loop shazamming trick.
TODO
The top level init.lua is the entry point that imports the rest of the code structured thus:
$ tree -P '*.lua' --prune
.
├── audio
│ ├── devices.lua
│ ├── switch.lua
│ └── watcher.lua
├── init.lua
└── utils
└── notify.lua
3 directories, 5 filesThe rest of my original source repos are here.
Pre-built Docker images are available on my DockerHub and can be re-generated using the my Dockerfiles repo.