function to switch audio input

248 views
Skip to first unread message

d mg

unread,
Jul 21, 2021, 6:13:53 PM7/21/21
to Hammerspoon
hi everybody, 

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.
is there a "better" way to ask the user for a string than using textPrompt?

thank for you any suggestions,

--daniel

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

Chris Jones

unread,
Jul 21, 2021, 6:59:20 PM7/21/21
to d mg, Hammerspoon
I would use hs.chooser

Cheers,
--
Chris Jones

On 21 Jul 2021, at 23:13, d mg <dmge...@gmail.com> wrote:

hi everybody, 
--
You received this message because you are subscribed to the Google Groups "Hammerspoon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hammerspoon...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/hammerspoon/05fbdcb2-1a14-4bd6-9606-742fa6c647a4n%40googlegroups.com.

dmg

unread,
Jul 21, 2021, 7:31:37 PM7/21/21
to Chris Jones, Hammerspoon
Thanks a lot for the suggestion. it is now muchhhh better:

local audiochoices = {}

for i,v in ipairs(hs.audiodevice.allOutputDevices()) do
table.insert(audiochoices, {text = v:name(), idx=i})
end

local audioChooser = hs.chooser.new(function(choice)
if not choice then hs.alert.show("Nothing chosen"); return end
obj:print_table0(choice)
local idx = choice["idx"]
local name = choice["text"]
dev = hs.audiodevice.allOutputDevices()[idx]
if 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)

audioChooser:choices(audiochoices)
hs.hotkey.bind({"cmd", "alt"}, "A", function() audioChooser:show() end)
--
--dmg

---
D M German
http://turingmachine.org

Bryan Banz

unread,
Mar 23, 2022, 9:56:32 AM3/23/22
to Hammerspoon
Thanks for this DMG! I've been looking over the web for this for a while, then remembered I use Hammerspoon, I bet it can do what I want.. and your script worked great. I only received an error on the `obj:print_table0(choice)` line, which I think was just for debugging purposes. Quick comment that out and I was in business!
Reply all
Reply to author
Forward
0 new messages