Created
June 26, 2024 08:15
-
-
Save omgmog/8c2139c9270431b7cddd93a947c9018c to your computer and use it in GitHub Desktop.
Hammerspoon switch audio output devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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