deweller / switchaudio-osx

Change the audio source for Mac OS X from the command line.
Other
1.19k stars 88 forks source link

unable to switch to AirPlay output 'devices' #9

Open arkarkark opened 8 years ago

arkarkark commented 8 years ago

I'm unable to switch to my airplay 'speakers'

image

$ SwitchAudioSource -a
Built-in Microphone (input)
Built-in Output (output)
$ SwitchAudioSource -t output  -s "Garage"
Could not find an audio device named "Garage" of type output.  Nothing was changed.
philfreo commented 8 years ago

+1

s00500 commented 8 years ago

this used to work... +1

s00500 commented 8 years ago

But: It still let's you switch if you call -s and the exact name of the device! (use "\ " for spaces) Just switch to your airplay device using syspreferences and run -c and it will also tell you the current device Edit: This strangly works just as long as system prefercenses are open...

philfreo commented 7 years ago

I don't think it has anything to do with System Preferences being open. At first I thought I had it working for a second too, but really what I see now (with macOS Sierra) is:

screenshot 2016-12-27 16 17 47

Any thoughts @deweller?

deweller commented 7 years ago

I don't use AirPlay speakers, so naturally I haven't encountered this bug.

I'd be happy to review a patch if anyone is able to fix it.

lfilho commented 7 years ago

Also not working for AirPlay devices for me. OSX El Capitan + latest SwitchAudio version from brew (as of today).

Buadhai commented 6 years ago

+1

bermann commented 6 years ago

Any news on this? 😟

hightechfreak commented 6 years ago

Same problem here on High Sierra. Any news? Looks like AirPlay outputs are treated differently. Just to confirm: if my AirPlay receiver has been selected using the menu, I can run -c AirMusic-Box and it confirms, but when I run the same command with any other device selected it claims the device is unknown. Unfortunately I am a newbie to XCode and it has been a while for me to program in C but I will try insert some diagnostic code and find out... Maybe the airport receivers have a different device type than "output"...

rien333 commented 6 years ago

Whats more so interesting is that say -a '?' also does not show airplay speakers (at least with my configuration using shairport-sync). Therefore, Apple themselves also seem to have not avoided this problem.

mloeschner commented 6 years ago

Even say --audio-device=? does not find available airplay devices on Mac OS [High] Sierra. One has to connect them manually to become available. Once connected and selected, streaming interactively with say to an airplay speaker doesn't work as expected.

raptor235 commented 6 years ago

Same issue here :( can't use this with AIrplay speakers

fharper commented 5 years ago

Same here! This project doesn't seem supported anymore, but is anyone created a patch?

zuckschwerdt commented 5 years ago

As @deweller said, the developers don't use AirPlay. Someone needs to write the code.

timonvanhasselt commented 4 years ago

Not the greatest solution, but a possible workaround with AppleScript. I combined 2 AppleScripts, based on this tutorial: http://apetronix.com/switch-audio-outputs-with-a-keyboard-shortcut-on-os-x/ HDMI and AirSpeaker are stand-ins.

Set the currentAudioSource to (do shell script "/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -c") if currentAudioSource is equal to "HDMI" then tell application "System Events" tell process "SystemUIServer" click (menu bar item 1 of menu bar 1 whose description contains "Volume") set Airplay to menu 1 of result click ((menu item 1 where its name starts with "AirSpeaker") of Airplay) end tell end tell else do shell script "/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -s \"HDMI\"" end if

samlin39 commented 3 years ago

+1

samlin39 commented 3 years ago

+1 BIG SUR 11.0.1 (20B29)

Suplanus commented 3 years ago

The great workaround from @timonvanhasselt sadly don't work under Big Sur.

alanhe421 commented 3 years ago

I don't use AirPlay speakers, so naturally I haven't encountered this bug.

I'd be happy to review a patch if anyone is able to fix it.

I will have a try....

deweller commented 3 years ago

Thanks @alanhg!

demyxco commented 3 years ago

I was originally gonna use switchaudio-osx but then AppleScript can do it natively:

set HomePod to "Bedroom"
tell application "System Events"
    tell application process "ControlCenter"
        set soundMenu to menu bar item "Sound" of menu bar 1
        tell soundMenu to click
        set soundCheckbox to checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title contains HomePod
        set soundCheckboxValue to value of soundCheckbox
        tell soundCheckbox to click
        tell soundMenu to click
    end tell
end tell
Suplanus commented 3 years ago

Great, thank you @demyxco

Attention: The Window and menu bar items are localized. In German I had to change Sound and Control Center:

set HomePod to "Bedroom"
tell application "System Events"
    tell application process "ControlCenter"
        set soundMenu to menu bar item "Ton" of menu bar 1
        tell soundMenu to click
        set soundCheckbox to checkbox 1 of scroll area 1 of group 1 of window "Kontrollzentrum" whose title contains HomePod
        set soundCheckboxValue to value of soundCheckbox
        tell soundCheckbox to click
        tell soundMenu to click
    end tell
end tell
demyxco commented 3 years ago

Here is another one that uses System Preferences but you only changed the "Bedroom" string:

tell application "System Preferences" 
    reveal anchor "output" of pane id "com.apple.preference.sound" 
end tell 

tell application "System Events" to tell process "System Preferences" 
    tell table 1 of scroll area 1 of tab group 1 of window 1 
        select (row 1 where value of text field 1 is "Bedroom") 
    end tell 
end tell 

quit application "System Preferences" 
alanhe421 commented 3 years ago

I found that I didn't have the energy to learn C and how to release the new package, so I decided to use AppleScript and Shell.

it works.

image

Any user who uses Alfred can download this workflow. Easy to switch to any audio include airplay.

https://github.com/alanhg/alfred-workflows/tree/master/switch-audio

kbd commented 3 years ago

@alanhg is the source for that available?

alphabt commented 3 years ago

I took the script folks here have shared and enhanced it to make it more reliable to wait for the volume menu to open before switching to the airplay speaker.

I also attached it to a hotkey so I can simply press ⌘ + ' to trigger the switch.

set soundOutputName to "Dining Room"

tell application "System Events"
    tell application process "ControlCenter"
        -- Open Sound menu
        set soundMenu to menu bar item "Sound" of menu bar 1
        tell soundMenu to click

        -- Select specified output
        repeat until exists checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title is equal to soundOutputName
        end repeat

        set soundCheckbox to checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title is equal to soundOutputName
        tell soundCheckbox to click

        -- Close Sound menu
        tell soundMenu to click
    end tell
end tell

https://github.com/alphabt/macos-automations/tree/master/Set%20Sound%20Output has more detail on how to setup the hotkey using macOS's Automator workflow.

alanhe421 commented 3 years ago

@alanhg is the source for that available?

of course

kbd commented 3 years ago

@alanhg is the source for that available?

of course

Where? I only see the alfredworkflow file.

alanhe421 commented 3 years ago

@alanhg is the source for that available?

of course

Where? I only see the alfredworkflow file. Sorry, Alfred workflow is an archive file, only works in alfred.

For Apple Script, you can view the readme. I have posted link.

https://gist.github.com/alanhg/21f7fd110e0bdac1d0cce66ca40e78ea

nvrmindu commented 3 years ago

I have similar issue with my SDAC. Figured out, what actual name is "SDAC ", with space, for some reason.

Suplanus commented 3 years ago

Have someone a solution in Monterey?

demyxco commented 3 years ago

@Suplanus download Hammerspoon and use my key bind. You can change the hotkey and the string Bedroom since this is what I named my HomePod mini.

hs.hotkey.bind({'ctrl'}, 'Home', function()
    hs.applescript([[
        quit application "System Preferences"

        do shell script "networksetup -setairportpower en0 off"
        do shell script "networksetup -setairportpower en0 on"

        do shell script "sleep 2"

        tell application "System Preferences"
            reveal anchor "output" of pane id "com.apple.preference.sound"
        end tell

        do shell script "sleep 2"

        tell application "System Events" to tell process "System Preferences"
            tell table 1 of scroll area 1 of tab group 1 of window 1
                select (row 1 where value of text field 1 is "Bedroom")
            end tell
        end tell

        quit application "System Preferences"
    ]])
end)
Suplanus commented 3 years ago

Thank you @demyxco. Hammerspoon is not needed. But I used your apple script inside :)

Technofrikus commented 2 years ago

@demyxco @Suplanus Oh yeah! Monterey broke the script I used before, but the apple Script works great from your post. Thanks a lot!

demyxco commented 2 years ago

@Technofrikus no problem, I've made several revisions since then but here's the current one lol:

quit application "System Preferences"

do shell script "networksetup -setairportpower en0 off"
do shell script "networksetup -setairportpower en0 on"

set wifi to false
repeat until wifi is true
    try
        do shell script "ping -o -t 2 1.1.1.1"
        set wifi to true
    on error
        say "Connecting"
        delay 1
    end try
end repeat

if (wifi = true) then

    tell application "System Preferences"
        reveal anchor "output" of pane id "com.apple.preference.sound"
    end tell

    delay 1

    tell application "System Events" to tell process "System Preferences"
        tell table 1 of scroll area 1 of tab group 1 of window 1
            select (row 1 where value of text field 1 is "Bedroom")
        end tell
    end tell

    quit application "System Preferences"

end if
Technofrikus commented 2 years ago

@demyxco Not sure why you are disabling wifi in the beginning? But if I delete the first part, the rest works still great, thanks!

Sometimes I have a problem to connect to the HomePod, but changing Wifi doesn't help for me. Restarting the HomePod does though.

demyxco commented 2 years ago

@Technofrikus sometimes my Mac has trouble connecting to the HomePod but this rarely happens. Glad I can help out!

deverman commented 2 years ago

Was just trying to use this software to so airplay speakers too. +1

jdsimcoe commented 1 year ago

@demyxco thanks for this little script:

set HomePod to "Bedroom"
tell application "System Events"
    tell application process "ControlCenter"
        set soundMenu to menu bar item "Sound" of menu bar 1
        tell soundMenu to click
        set soundCheckbox to checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title contains HomePod
        set soundCheckboxValue to value of soundCheckbox
        tell soundCheckbox to click
        tell soundMenu to click
    end tell
end tell

I'm trying to use it on macOS Ventura but it is giving this error:

Can’t get menu bar item "Sound" of menu bar 1 of application process "ControlCenter".

Any ideas how to update it?

Technofrikus commented 1 year ago

@jdsimcoe As far as I can tell, the new System Settings dont work with AppleScript anymore? I am using the app Keysmith now. Works via simulated mouse clicking, which is not so great, but still better than doing it by hand.

jdsimcoe commented 1 year ago

@Technofrikus thanks for the suggestion! I tried to create a macro to set the sound output to my HomePod but all it did is this, which doesn't do anything:

image

It's so frustrating that  breaks AppleScript support for things without a clear path forward.

Technofrikus commented 1 year ago

@jdsimcoe Yes via Control Center it doesnt seem to work, just tried it. I do it via SystemSettings. CleanShot 2022-11-22 at 19 14 30@2x

demyxco commented 1 year ago

@jdsimcoe here's a more recent/updated one I have but I haven't used it in Ventura yet since I switched to a bluetooth speaker:

tell application "System Preferences"
    reveal anchor "output" of pane id "com.apple.preference.sound"
end tell

delay 1

tell application "System Events" to tell process "System Preferences"
    tell table 1 of scroll area 1 of tab group 1 of window 1
        select (row 1 where value of text field 1 is "Mac Studio Speakers")
    end tell
end tell

delay 2

tell application "System Events" to tell process "System Preferences"
    tell table 1 of scroll area 1 of tab group 1 of window 1
        select (row 1 where value of text field 1 is "HomePod mini")
    end tell
end tell

quit application "System Preferences"

Let me know how it goes.

jdsimcoe commented 1 year ago

Thanks for sharing, @demyxco. I tried your new one but it also doesn't work. I think Ventura just broke a ton of Applescript API stuff.

demyxco commented 1 year ago

@jdsimcoe yeah most likely. When I get more time, I'll try to test it out.

JayBrown commented 1 year ago

@noonchen has developed BTAudioSwitch, which is supposed to toggle i.a. previously paired Bluetooth/AirPlay devices. It does detect my HomePod, even when it's not set as the output device, but sadly doesn't toggle it (using the -toggleSwitch argument). But maybe there's something in the code that can give @deweller an idea how to proceed in this matter.

EDIT (fyi): Rogue Amoeba's SoundSource is also unable to detect, let alone switch to, my HomePod.

deweller commented 1 year ago

I don't have an AirPlay device handy. Did the latest change which replaces the deprecated audio devices help anything?

JayBrown commented 1 year ago

Sadly didn't change anything, i.e. SwitchAudioSource prints "AirPlay" after manually switching to the HomePod, but once you use SwitchAudioSource -s or -n, it's not possible anymore to switch back to "AirPlay", and the HomePod has been dropped from the output of -a.

However, all the other programs I've tested that allegedly support Bluetooth pairing, switching & connecting incl. audio devices (BTAudioSwitch, blueutil and BluetoothConnector) aren't working either. So it looks more like something in macOS has changed (or is even broken).

But I assume that the approach to setting a Bluetooth audio device as audio output on the command-line is a lot different under-the-hood than with wired or virtual devices… pair first, then connect & establish whether it's even an audio device in the first place, and if yes, select as audio device. (?)

JayBrown commented 1 year ago

Addendum: macOS isn't able to handle this either. When I use the system utility Audio MIDI Setup to create an aggregate or multi-output device, e.g. one that includes both "External Headphones" and "AirPlay", macOS will list them in the utility and in the system's sound preferences (devices list), and SwitchAudioSource -a also has them. But when activating them, only the non-AirPlay part of the multi/aggregate device will work because macOS has obviously "lost track" of the HomePod. It does seem that something is broken in macOS, or that Apple developers haven't really thought this through.

JayBrown commented 1 year ago

This AppleScript seems to work for me on Ventura… combined from online sources. (Change the name of your AirPlay device in line 1, if necessary.) However, unlike before, the whole new UI scripting with scroll area, splitter group etc. doesn't seem to work anymore with System Settings launched hidden, ergo the activate in line 4. If someone finds a way to tweak this, so it works hidden (or at least in the background), please post your solution. 🙏

set myDevices to {"HomePod"}

tell application "System Settings"
    activate
    reveal anchor "output" of pane id "com.apple.Sound-Settings.extension"
end tell

delay 2

tell application "System Events"
    tell application process "System Settings"
        set theRows to (every row of table 1 of scroll area 1 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Sound")
        repeat with myDevice in myDevices
            set theDevice to myDevice as string
            repeat with aRow in theRows
                if name of first item of static text of group 1 of UI element 1 of aRow is equal to theDevice then
                    set selected of aRow to true
                    exit repeat
                end if
            end repeat
        end repeat
    end tell
end tell

quit application "System Settings"

You can prepend a second audio device comma-delimited in line 1, e.g. "External Headphones", and then the script will first enable the headphones, and then the AirPlay device, which might be a workaround if your AirPlay device is disconnected & doesn't reconnect.