Skip to content

Instantly share code, notes, and snippets.

@omgmog
Created June 26, 2024 08:15
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 device = hs.fnutils.find(hs.audiodevice.allOutputDevices(), function(_device)
return string.match(_device:uid(), deviceUid)
end)
if not device then
hs.alert.show(alertIcon .. " " .. deviceUid .. " not found")
return
end
local success, errMsg = pcall(device.setDefaultOutputDevice, device)
if success then
hs.alert.show(alertIcon .. " " .. alertMessage)
else
hs.alert.show("Error setting " .. device:name() .. " as default device: " .. errMsg)
end
end
hs.hotkey.bind({}, "F12", function()
switchToDevice("G635 Gaming Headset", "🎧", "Headphones")
end)
hs.hotkey.bind({}, "F13", function()
switchToDevice("BuiltInSpeakerDevice", "🔊", "Speakers")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment