Skip to content

Instantly share code, notes, and snippets.

@omgmog
Last active May 9, 2025 15:44
Show Gist options
  • Save omgmog/8c2139c9270431b7cddd93a947c9018c to your computer and use it in GitHub Desktop.
Save omgmog/8c2139c9270431b7cddd93a947c9018c to your computer and use it in GitHub Desktop.
Hammerspoon switch audio output devices
local function switchToDevice(deviceUid, alertIcon, alertMessage)
local log = hs.logger.new("switchToDevice", "debug")
local device = hs.fnutils.find(hs.audiodevice.allOutputDevices(), function(_device)
local uid = _device:uid()
local name = _device:name()
if uid and name then
return string.match(uid, deviceUid)
else
return false
end
end)
if not device then
hs.alert.closeAll()
hs.alert.show(alertIcon .. " " .. deviceUid .. " not found")
return
end
local success, errMsg = pcall(function() device:setDefaultOutputDevice() end)
if success then
for _,screen in ipairs(hs.screen.allScreens()) do
hs.alert.show(alertIcon .. " " .. alertMessage, {
textSize = 100,
strokeColor = { white = 0, alpha = 0 },
}, screen)
end
else
hs.alert.closeAll()
hs.alert.show("Error setting " .. device:name() .. " as default device: " .. errMsg)
end
end
-- Bind F12 without any modifiers to switch to the G635 Gaming Headset
hs.hotkey.bind({}, "F12", function()
hs.alert.closeAll()
switchToDevice("G635 Gaming Headset", "🎧", "Headphones")
end)
-- Bind F13 to switch to the built-in speaker device
hs.hotkey.bind({}, "F13", function()
hs.alert.closeAll()
switchToDevice("BuiltInSpeakerDevice", "🔊", "Speakers")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment