Automate Tasks with Hammerspoon

Using Hammerspoon (Lua) to automate tasks on macOS: small utilities, Wi‑Fi events, audio control, and notifications.

Sep 28, 2019 · 1 min read · 147 Words · -Views -Comments · Software & Tools
Productivity Tools 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

I’d heard of Hammerspoon but hadn’t tried it. I wrote a few scripts recently — fun and useful — sharing notes here.

Below, hs stands for Hammerspoon.

A small utility

Features

  1. Auto‑mute system audio
  2. Switch MVN settings

Code

-- audio mute 
-- change mvn setting 

local workWifi = 'company-wifi'
local outputDeviceName = 'Built-in Output'
hs.wifi.watcher.new(function()
    local currentWifi = hs.wifi.currentNetwork()
    local currentOutput = hs.audiodevice.current(false)
    if not currentWifi then return end
    if (currentWifi == workWifi and currentOutput.name == outputDeviceName) then
        hs.audiodevice.findDeviceByName(outputDeviceName):setOutputMuted(true)

        local changeMvnCommand="ln -sfn ~/.m2/settings_company.xml ~/.m2/settings.xml"
        hs.execute(shell_command)

        hs.notify.new({title="HS Robot", informativeText="Connect to Company"}):send()
            
    end
end):start()

How it differs from Alfred

  • Alfred offers fixed interaction patterns (keywords, hotkeys) to boost productivity.
  • Hammerspoon uses Lua bindings to macOS APIs for things Alfred can’t do: listen for Wi‑Fi changes, run shell commands, manage windows, etc.
  • They complement each other well.

References

For simple tools, the official docs are best:

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover