A gentle intro to Hammerspoon
Automate your Mac with Lua
Hammerspoon is a powerful automation tool for macOS. It allows you to write scripts in Lua to automate tasks on your Mac. You can use it to create custom keyboard shortcuts, automate repetitive tasks, and more.
Here's a simple example of a Hammerspoon script that opens an app or even a file within an app (e.g. a journal file in your Obsidian vault); in my case, press Ctrl + K
to open the KakaoTalk app (a popular messaging app in South Korea):
hs.hotkey.bind({"ctrl"}, "K", function()
hs.execute("open -a 'KakaoTalk'")
end)
To get started with Hammerspoon, you'll need to install it from the official website. Once you have it installed, you can create a ~/.hammerspoon/init.lua
file and start writing your scripts.
To use extensions, you can install them via luarocks. For example, to install the json
extension, you can run:
luarocks install lunajson
Then you can use it in your Hammerspoon scripts like this, in a use case for sending a message to a Slack channel:
hs.timer.doAt("08:00", "1d", function()
payload = json.encode({
text = "Good morning y'all! :wave:"
})
url = "https://hooks.slack.com/services/CHANGE/THIS/URI"
result, err = hs.http.asyncPost(url, payload, {}, function(status, body, headers)
if status == 200 then
debug("Message sent to Slack!")
else
debug("Error sending message to Slack: " .. inspect(body))
end
end)
end)