No internet connection
  1. Home
  2. Support

Change computer audio output?

By Jonathan Grossman @Jonathan_Grossman
    2020-05-15 01:50:40.318Z

    Is there a way to program a computer audio output switcher? Go from source-nexus (routed thru pro tools) or Apogee Maestro going to an external mixer?

    Solved in post #6, click to view
    • 19 replies
    1. You can do something like this:

      var volumeMenu = sf.ui.app('com.apple.systemuiserver').getElement("AXMenuBar").children.whoseDescription.startsWith('volume ').first;
      volumeMenu.popupMenuSelect({
          isOption: true,
          menuPath: ['Headphones']
      });
      
      1. MMatt Friedman @Matt_Friedman
          2022-05-04 00:53:24.713Z

          This is awesome but do you have a Monterey update to this script? better yet maybe some logic so it can work in both Monterey and older OS?

          It looks like in Monterey, it's now the control center.

          sf.ui.app("com.apple.controlcenter").getElement("AXExtrasMenuBar")

          But I can't get further than that so I can't figure out how to make it select the desired output.

          Thanks

          1. In reply tochrscheuer:
            Nathan Salefski @nathansalefski
              2023-12-06 05:37:53.150Z

              Bumping this. I've found this here and it seems to work however it seems a bit clunkier than need be. https://apple.stackexchange.com/questions/217148/using-apple-script-to-manage-sound-output-selection

              sf.system.execAppleScript({
                  script:
                      `set outputA to 2 --change this to the actual 'line number' of your first desired output
                      set outputB to 3 --change this to the actual 'line number' of your second desired output
              
                      tell application "System Preferences"
                          activate
                          set current pane to pane "com.apple.preference.sound"
                      end tell
              
              
                      tell application "System Events"
                          tell application process "System Preferences"
                              repeat until exists tab group 1 of window "Sound"
                              end repeat
                              tell tab group 1 of window "Sound"
                                  click radio button "Output"
                                  if (selected of row outputA of table 1 of scroll area 1) then
                                      set selected of row outputB of table 1 of scroll area 1 to true
                                  else
                                      set selected of row outputA of table 1 of scroll area 1 to true
                                  end if
                              end tell
                          end tell
                      end tell
                      
                      tell application "System Preferences" to quit`
              });
              
              1. In reply tochrscheuer:
                Nathan Salefski @nathansalefski
                  2023-12-06 07:47:58.236Z

                  I have also made the smallest measurable amount of progress with Apple Scripts lol. I can open the volume panel but cannot for the life of me figure out how to select an option

                  sf.system.execAppleScript({
                      script:
                          `tell application "System Events"
                  			tell its application process "Control Center"
                  				click menu bar item "Sound" of menu bar 1
                  			end tell
                  		end tell`
                  });
                  
                • J
                  Jonathan Grossman @Jonathan_Grossman
                    2020-05-15 17:35:20.777Z

                    Amazing - so now if I want to toggle between these with one button, how far off am I with this script....

                    globalState.inputswitcherScriptState = !globalState.inputswitcherScriptState;
                    if (globalState.inputswitcherScriptState) {
                    (var volumeMenu = sf.ui.app('com.apple.systemuiserver').getElement("AXMenuBar").children.whoseDescription.startsWith('volume ').first;
                    volumeMenu.popupMenuSelect({
                    isOption: true,
                    menuPath: ['Duet USB']);
                    } else {
                    (var volumeMenu = sf.ui.app('com.apple.systemuiserver').getElement("AXMenuBar").children.whoseDescription.startsWith('volume ').first;
                    volumeMenu.popupMenuSelect({
                    isOption: true,
                    menuPath: ['Source-Connect']);
                    }

                    1. MMartin Pavey @Martin_Pavey
                        2021-05-02 14:45:15.826Z

                        Hi There,
                        I'm trying to do a similar thing here.
                        Did you ever get this Core Audio toggle script to work?
                        This one doesn't seem to work for me.
                        Christian's original script works but the one button toggleing (which would be great) doesn't.
                        Thanks

                        1. I did! Will send a script when I get in the studio a bit later.

                          1. In reply toMartin_Pavey:

                            Hi Martin:

                            here's the code I use. Sorry it took another day!

                            const itemsToSwitch = ['ONEv2', 'Source-Nexus'];
                            
                            var volumeMenu = sf.ui.app('com.apple.systemuiserver').getElement("AXMenuBar").children.whoseDescription.startsWith('volume ').first;
                            volumeMenu.popupMenuSelect({
                                menuSelector: menuItems => {
                                    const candidates = menuItems.filter(mi => itemsToSwitch.indexOf(mi.names[0]) >= 0);
                                    const selectedItem = candidates.map((menuItem, index) => ({ menuItem, index })).filter(m => m.menuItem.element.isMenuChecked)[0];
                                    const oldIndex = selectedItem ? selectedItem.index : -1;
                                    let newIndex = oldIndex + 1;
                                    if (newIndex >= candidates.length) newIndex = 0;
                                    return candidates[newIndex];
                                },
                            });
                            
                            Reply2 LikesSolution
                            1. MMartin Pavey @Martin_Pavey
                                2021-05-04 15:12:10.623Z

                                Wow!
                                Thanks so much Jonathan, it works perfectly.
                                I owe you a virtual beer.
                                Cheers
                                Martin

                                1. haha! Can't take credit...I'm only the messenger. that's from the wonderful coded mind of Christian

                                  1. MMartin Pavey @Martin_Pavey
                                      2021-05-04 15:36:43.689Z

                                      Ha! Nice one.
                                      Beers all round then!

                            2. Ben Rubin @Ben_Rubin
                                2021-09-30 15:27:25.974Z

                                Thanks so much for this one!!!

                                1. Ben Rubin @Ben_Rubin
                                    2022-05-04 01:43:49.464Z

                                    jumping back on this thread, i'm realizing that because I hide my menu bar by default, this script will often fail. Does anyone know how to force the OS to show the menu bar?

                                    thanks
                                    ben

                                    1. P
                                      @pepsin
                                        2022-09-22 16:40:32.281Z

                                        Has anyone gotten this command to work on Monterey? I'm currently on 12.3.

                                        Thanks for your time!

                                        1. M
                                          Matt Friedman @Matt_Friedman
                                            2022-09-30 18:40:40.130Z

                                            Bump! Any ideas?

                                            1. I tried a few things and I could locate the menu button (I don't remember the code right now unfortunately) but I couldn't get the menu items to be found.
                                              It's possible we should look at this as a feature request to change audio input/output settings directly via a SF action instead of relying on these menus.

                                              1. Hi all, please let me know if I'm in the wrong place for this. I'm trying to switch between my Focusrite Red8Pre and Dante Virtual Sound Card using button(s) on my Stream Deck. I need the Red8Pre for all recording/mixing and the Dante Virtual Sound card for Atmos 7.1.4 playback from Apple Music through the Dante Virtual Soundcard into the Dante Controller into the RedNet software. I'm currently switching using the "Sound" menu currently showing up in the menu items, or I open Audio/Midi setup and change it there. I've tried using Christian's solution on post #6 (adding the changes suggested for use in Monterey) but alas I have little to almost no clue how to code. My two devices are showing in the "Output" menu (in the "Sound" Menu as as "Focusrite Thunderbolt" and "Dante Virtual Soundcard". Is it possible to toggle between these two devices using a button on a Stream Deck by creating a button(s) in SoundFlow? Thanks! If I'm out of my depth here I'll accept that. If I'm only a few tweaks away from the solution I appreciate any help someone would be willing to offer. Thanks! Alex J

                                                1. In reply tochrscheuer:
                                                  Nathan Salefski @nathansalefski
                                                    2023-10-26 20:10:07.413Z

                                                    Hey @chrscheuer any update on this one?

                                                    1. In reply tonathansalefski:

                                                      You can use something like this:
                                                      https://github.com/deweller/switchaudio-osx

                                                      Install it first via Homebrew, then it should be easy to run to switch audio outputs in Terminal.

                                                      Once you know how to do it in Terminal, you can then write a SF script that runs a Terminal command to do it for you.