-- ~/.hammerspoon/init.lua local function refreshWidget() hs.http.asyncGet("http://127.0.0.1:7776/widget/sound/refresh", nil, function(status, body, headers) -- optional: log failures if status ~= 200 then print("Widget refresh failed with status: " .. tostring(status)) end end) end local function audioWatcherCallback(uid, eventName, scope, element) -- "vmvc" = volume changed, "mute" = mute toggled if eventName == "vmvc" or eventName == "mute" then refreshWidget() end end local function setupAudioWatcher() local device = hs.audiodevice.defaultOutputDevice() if device then device:watcherCallback(audioWatcherCallback) device:watcherStart() end end setupAudioWatcher() -- Re-attach the watcher if the default output device changes (e.g. plugging in headphones) hs.audiodevice.watcher.setCallback(function(event) if event == "dOut " then -- note: the trailing space is intentional, it's part of the event name setupAudioWatcher() end end) hs.audiodevice.watcher.start()