Wednesday, January 14, 2009

Change audio devices via shell script

I searched for a long time for a way to activate Soundflower as part of a script, but I couldn't find any way to do so, short of opening system preferences and using UI scripting to control the settings.

There had to be a better way.

I found SoundSource, but I couldn't find any way to activate it from applescript, even with UI scripting. I asked the developers about scriptability, and after a few emails back and forth, they decided to release the source, which I used to build audiodevice.

Audiodevice
Source

This the basic functionality of SoundSource with a quick and dirty command line interface slapped on. Type "audiodevice help" for usage information. Since I got my Airport express, I haven't had much use for my Soundflower script, but I still regularly use it in conjunction with Proxi to activate my bluetooth headset from the keyboard.

25 comments:

Dex†er said...

Hey! How do you use this file? I'm really a noob on this stuff and I just want an easy way to change my audio output without going to all the trouble on the system settings. I don't really know if this is that kind of stuff but would be very happy if somebody could help me. I'm running a Mac OS X ver 10.5.8 Intel Core 2.

Please mail me at andreasdigrazia@hotmail.com
or leave a comment please!

Thanks!

Best regards

whosawhatsis? said...

If you're a noob, you may just want to use SoundSource. It puts a menu in the right side of your menu bar that lets you select audio devices. This is fast enough access for most purposes. This program is intended to make it slightly faster for those who are already using a launcher, or for those who need to include the functionality in a larger script.

ze'ev said...

AWESOME! Just what I was looking for!
Thank you!

Zettt said...

Could you help me, please? This isn't working for me anymore. I've installed it yesterday and it worked just fine, but an hour ago (right after I was testing another audiodevice shell thing http://www.macosxhints.com/article.php?story=20050614171126634) it didn't work anymore.
Do you know what I broke?

whosawhatsis? said...

I assume you're talking about the SwitchAudioSource program. I don't know how that works, but I would guess that if you set everything to built-in audio and reboot, it should work as normal. If the prefpane works, Audiodevice should work too.

Zettt said...

Simply opening the Sound PrefPane did the trick. Thanks a lot!

Christian said...

Hi,

Really great work. We put your command shell in all our macs in our school lab and then we can use ARD to change the device used by the students macs.

We have a request more, is there a way to implement the possibility to choose the volume for one port ? By example : audiodevice output "Micronas USB Headset" volume 50

Thanks in advance !

Christian

whosawhatsis? said...

I never did figure out how to do that. As explained, the code that actually does the work in this program isn't mine, I just wrote a little interface code.

Nick said...

I've taken your command-line utility, wrapped it in a shell script to switch back and forth between two inputs and then call it from a php file. This lets me integrate into my home automation application and switch audio inputs from my ipad. You can read more about it here: http://forum.micasaverde.com/index.php?topic=6137.0

Januz said...

Hi,

thanks for the script! Sadly, in my case it doesn´t work as it should. I have to output devices:

Internal Speakers
USB Sound Device

I can select the internal speakers with

audiodevice output "Internal Speakers"

but the same for the USB device fails with the error "device not found!". When I don´t use quotation marks, I don´t get the error but the source isn´t changed...

Would be nice if you could help me.

Thanks,

Januz

Peacegrove said...

Awesome work!
I'm using you program in a Alfred extension. http://dl.dropbox.com/u/514866/uploaded/Change_audio_device.zip

Fabian said...

Trying to use this on Lion, but the terminal simply ignores the command...? Could you help?

whosawhatsis? said...

What command are you trying? Does the list command work? Are you getting any output?

Jeff C said...

Great work on this. I use it with a little Applescript to mute Spotify ads: http://jeffcouturier.com/2012/04/mute-spotify-ads-in-osx-lion/

Unknown said...

I was having trouble switching to my "USB Sound Device" for output. Turns out there were a bunch of spaces (8) trailing the name. When I get the list of output devices (./audiodevice output list) I realized when I was highlighting "USB Sound Device" that there was extra whitespace at the end, the others didn't have it. I got it to work using the following command:

./audiodevice output "USB Sound Device "

Thom Patterson said...

Looks like blogger mashed all my spaces down to 1, suffice it to say in my previous post there are 8 spaces before the closing quote.

Dave said...

Hey,
I have a quick question. Just getting into playing with bash, and I was wondering if I could "install" this utility, so I don't have to navigate to the directory and use "./audiodevice" to get it to run.

Basically I want to just be able to type "audiodevice output 'Dave's Room'" at the prompt and have it run. Currently if I do this, it says "-bash: audioadvice: command not found"

I've tried Googling how to install a shell script, but just not had any luck finding what I'm after yet.

Oh, and I'm running OS X 10.8.3 :)

Thanks! :)

whosawhatsis? said...

Use the command "echo $PATH" to get a colon-separated list of the folders you can install the script in to make it available from anywhere, without the "./" in front.

Abdul Hakeem said...

How do disable headphones on mac. I have something stuck in my headphone jack and the internal speakers only chime on startup . After that it is disabled. Is there any script to disable the input. thx

Abdul Hakeem said...

Just clarifying my earlier update. I have an end of a headphone stuck in the computer. Hence there is no audio output from the computer coz it thinks there is a headphone attached. I can only use the bluetooth output. However when the computer starts up , the chime is sounded, but then the speaker icon is disabled after the os comes up. I was wondering if there is any script to disable the headphone . Thx

whosawhatsis? said...

The OS only has an option for "Built-in Audio", which goes to the headphone jack if there is something plugged into it, and to the speakers if there isn't (the startup chime plays before the part of the system that controls this is available). As far as I can tell, there is no way to select between Headphones and built-in speakers in software.

ABWC said...

This is outstanding work. Thank you !!

Unknown said...

Fantastic work. Have you considered posting the source to GitHub, to make it easy to browse and examine?

NJ said...

Great job on this. It really came in handy for me. I was wondering if you know a way to mirror your Mac desktop screen to an AirPlay Display using a similar method with scripting. I know you can use AppleScript to set the device but I was wondering if there is a script that I can just pass in the AirPlay Display name (Apple TV) and it would automatically mirror my desktop screen to that device. I haven't been able to find anything on that in Apple's Developer Docs.

Adam Christianson said...

I am trying to use this to select an AirPlay audio output source and it seems to work, but only if I use the name of the last AirPlay I had selected in the System Preferences pane. For example, if I have "Internal Speakrs and two AirPlay destinations "AirPlay 1" and "AirPlay 2" and "AirPlay 1" is currently selected. I can run:

audiodevice output "Internal Speakers"

and then

audiodevice output "AirPlay 1"

and it will work great. But if I try running:

audiodevice output "AirPlay 2"

it will not work until I have manually selected that output source in the System Preferences pane. Then after that I can run:

audiodevice output "AirPlay 2"

And it will switch, but then:

audiodevice output "AirPlay 1"

no longer works.

hope this makes sense