I wrote the function below to switch the output audio device using the keyboard.
It lists the devices and ask the user to chose one by index.
it does work, but I wonder if there a nicer user interface to ask information from the user.
function obj:select_output_audio_device(name)
local name = 'unknown'
local wlist = ''
local prompt = 'Select number of the device to select'
for i,v in ipairs(hs.audiodevice.allOutputDevices()) do
wlist = wlist .. tostring(i) .. " " .. v:name() .. "\n"
end
local result,idx = hs.dialog.textPrompt(prompt,
wlist, 'default', 'ok', 'cancel')
idx = tonumber(idx)
if result == 'ok' and idx then
dev = hs.audiodevice.allOutputDevices()[idx]
if dev then
name = hs.audiodevice.allOutputDevices()[idx]:name()
end
if not dev or not dev:setDefaultOutputDevice() then
hs.alert.show("Unable to enable audio output device " .. name)
else
hs.alert.show("Audio output device is now: " .. name)
end
end
end