Things I'll Forget In A Month Tech scratchpad and musings

25Feb/103

Toggling audio devices in Mac OS X

I frequently switch between my external computer speakers and my headphones. Repeatedly plugging and unplugging my headphones started wearing out my headphone jack, so I hooked up an iMic to a USB port to give myself a second headphone jack. This allows switching between them in software through the Sound preference pane, which is more convenient, but still takes too long. So the next step is automating the switch. This thread on Mac OS X Hints has people discussing various ways to do it. The easiest GUI way to do the switch on Snow Leopard is just option-clicking the menu bar volume control and selecting the new output device (on Leopard, you can do effectively the same by installing SoundSource).

I'd like to avoid using the mouse at all, of course. Someone got the sources to SoundSource and created a little command-line app called audiodevice that can do the switch without a GUI. Making progress, but that still requires executing a custom command to switch from the headphones to speakers or vice versa. As a final step, I threw together a quick script that uses audiodevice to toggle between the two without any arguments:

#!/usr/bin/perl -w

use strict;

my $device1 = 'Headphones'; # my speakers, plugged into my computer's jack
my $device2 = 'iMic USB audio system'; # my headphones, plugged into my iMic's jack

foreach (`audiodevice`) {
  if (/output: (.*)/) {
    if ($1 eq $device1) {
      system("audiodevice output '$device2'");
      exit;
    } elsif ($1 eq $device2) {
      system("audiodevice output '$device1'");
      exit;
    } else {
      print "Unrecognized output device '$1'.\n";
      exit;
    }   
  }
}

print "Failed to find output device.\n";


I saved this script as audioswitch and added it to my LaunchBar index so I can call it from anywhere using only 4 or 5 keystrokes. Works exactly as I'd like, and you can modify it to toggle between any two devices by substituting the name of the devices (in the Output tab of the Sound preference pane) into the script.

UPDATE (3/11/10): I've fixed up the above script so that it automatically figures out what the two output devices are and switches between the two accordingly. This means that it'll work for switching between the iMic and either internal or external speakers. Also fixed a problem where system sounds would continue to play through the original device after switching and added a Growl notification with growlnotify.

#!/usr/bin/perl -w

use strict;

my $device1;
my $device2;

foreach (`audiodevice output list`) {
  chomp;
  if (defined $device1) {
    $device2 = $_; 
    last;
  } else {
    $device1 = $_; 
  }
}

foreach (`audiodevice`) {
  if (/output: (.*)/) {
    if ($1 eq $device1) {
      activate($device2);
      exit;
    } else {
      activate($device1);
      exit;
    }   
  }
}

print "Failed to find output device.\n";

sub activate {
  my $device = shift;
  system("audiodevice output '$device'");
  system("audiodevice system '$device'");
  system("growlnotify -n audioswitch -m 'switching to $device'");
}

Filed under: Hacks, Mac Leave a comment
Comments (3) Trackbacks (0)
  1. Hello,
    Could you please clarify where do you save this script (and where do you save audiodevice) and how do you set a keyboard shortcut to invoke it?

    Thanks!

  2. Actually, I want to use the eject key to toggle between my headphones and speakers, since I use mac mini without DVD drive and this key isn’t used. Can I do that?

  3. The place you save them doesn’t matter too much, unless you want to run them from the command line, in which case you should put them in a folder located in your $PATH. For easily running them, I use LaunchBar and set up a ‘run shell script’ action to execute audioswitch. This means it’s 4 keystrokes to run — command-space to start LaunchBar, A to select audioswitch, then enter to run.

    I doubt you’d be able to reprogram the eject key to run a shell script. If you could, I’m sure you’d have to use some sort of third-party OS hack — OS X doesn’t easily let you reprogram function keys like that as far as I know.


Leave a comment

No trackbacks yet.