Here’s a proven fix if you need both analog and HDMI audio outputs and require a way of switching between them. In my case, I had an application where a rack mounted Mac Mini serves as both a media server and an audio source for a whole house paging system.
I first tried using the headphone output as an analog audio output. The problem is that if anything is plugged into the headphone jack, it is not possible to select HDMI for sound output via the Sound preference panel or via any other means. Various people have posted methods for outputting audio to both HDMI and headphone out simultaneously, but my application requires only one output to be active at any given time.
The hardware piece of the solution is a relatively cheap Turtle Beach USB DAC. I’d imagine other USB audio converters would work equally well.
After plugging in the DAC, I confirmed that I could switch audio sources from the Sound preference panel.
The next step was to to automate the audio switching. I found various Applescript techniques on the web for doing this by scripting the sound preference panel UI, but that approach seemed kludgy and probably slower than using a compiled command line tool. If such a tool is included in OS X Mavericks, I couldn’t find it. Fortunately, a generous developer created an output switching utility called audiodevice, which is available here.
Audiodevice works perfectly but has one minor quirk. Some output devices have trailing spaces after their names, which need to be included in audiodevice commands.
Here is an example of a shell script that uses audiodevice to switch audio output from HDMI to USB DAC, plays a sound effect, outputs a string that was passed to it from the command line as text to speech, and then switches back to HDMI:
/usr/local/bin/Audiodevice/audiodevice output "USB Sound Device " /usr/bin/afplay "/Library/Audio/Apple Loops/Apple/iLife Sound Effects/Machines/Communication Engaged.caf" say $1 /usr/local/bin/Audiodevice/audiodevice output "HDMI Matrix "
Notice the spaces after USB Sound Device and HDMI Matrix. If you are not using the Turtle Beach DAC, the USB DAC may have a different name. Use the audiodevice output list command to get a list of the audio output devices installed on your Mac.