Change Sound Output Script?

Hey guys, does anyone have a script that can switch sound outputs on macOS? I want to be able to switch over to my Airport Express-connected speakers with either a keystroke or a BetterTouchTool gesture, and I assume that I’d need a script to make that happen. (Yes, I know I can just drop down from the sound icon in the menu bar. But I like the keyboard!) Many thanks!

-Eric
MBP 13" with Touchbar (2016)

Looks like the only way to change audio sources without clicking in the menu bar or opening system preferences is by using CoreAudio, which as far as I know you can’t do with the basic tools, at least not easily. With that said, it looks like some other folks have wanted to do what you’re thinking about and have written applications to solve the problem. Here are a couple options:

http://hints.macworld.com/article.php?story=20050614171126634

Thank you! I will try to work with those scripts. I appreciate the help!

-Eric

You could do it with a Keyboard Maestro macro.

Trigger the script via macro or do the actual sound output change via macro? I hadn’t seen a way to do the latter in KM. Thanks!

–Eric

This AppleScript still works for me!

http://www.macdrifter.com/2012/09/keyboard-maestro-macro-to-toggle-sound-devices.html

Yes. Using the a KM macro without a script. The Action that I have found extremely helpful is the “Click on found image” which I first heard about on a Dr. Drang blog post:
Here is what this type of macro might look like:

  1. Set a trigger (i.e. a typed string or other of your choosing)
  2. Action 1: Open a System Preference Pane. (select Sound from the drop down)
    This will open the preference pane which remembers whether Input or Output or Sound Effects was selected so you will have to take screenshots of the various states the image may present itself
  3. Action 2: Click on found image
    –> 32%20PM
    —>toggle the gear icon and deselect “Failure aborts macro”
    –>47%20PM
  4. Repeat Step 3 with screenshots of different presentations of the sound selection options so that you get a match:
    –>52%20PM23%20PM
  5. In all cases select mouse to click on center of found image so “Output” is selected
  6. Repeat “click on found image” Action with screenshot of desired output. for example:
    –>03%20PM
  7. final step is just to close the preference pane window
    The only other thing I have found helpful is to place approx 1 second pauses between each step so KM steps have a chance to execute before next step.

Hope that helps!

I don’t do it any more as I use Spotify Connect and an Amazon Echo Dot to control the music in my office now, but I used to have a need to switch audio output programmatically on my Mac. I found trying to do it with key presses not exactly reliable and certainly, not palatable to a purer solution.

That’s when I got myself a copy of Porthole by Danger Cove, a little app that allowed me to control some audio switching via AppleScript triggered from Keyboard Maestro. BUT, Porthole is alas no more.

My KM & AS for anyone particularly interested

image

I had a quick look around and AirFoil by Rogue Amoeba is AppleScriptable. Looking at the details shown, it would seem that a similar option to AppleScript a solution using AirFoil may therefore be viable.

Hope that helps.

This is all great stuff! Thanks! Unfortunately, I’ll have to wait to play around with it… Because, fortunately, I can leave my work computer behind as I get ready for a 2-week vacation!

Thanks for all the tips, everyone. I had seen some of it before on Google searches, but you all have confirmed some things I had suspected. I appreciate it!

–Eric

Audio Switch for Alfred:

See also

which has detail on some underlying scripts that should be applicable if you’re not using Alfred.

If you’re going through all that you could just record changing the output by clicking the volume icon in the menu bar volume with Automator, save it as a service and assign it a keyboard shortcut.

It’s going to take control of the mouse which is why I was leaning towards something scripted but if that works, then it works.