Monday, April 4, 2011

Switching Mac Audio Output with iRedLite

It is often convenient to use headphones to watch TV to avoid disturbing others in the household.  Initially, I was just plugging the headphones into the Sony TV, but this meant the cord was draped across the floor and untidy.  Then, I remembered that the Mac Mini had a headphone socket which had the advantage that the headphones could be left connected all the time as the Mac Preferences allowed switching the sound output between the headphones and the Sony TV (via HDMI output).

However, the switching was still a bit messy as I had to use the Mac keyboard and call up the Preferences. A quick Google search revealed several Applescripts which various people had written to automate switching the audio output between various devices, which meant I could potentially control the audio device switching from the Logitech 525 remote rather than using the keyboard.

After slightly modifying one of the scripts I found online, here is the final script I used:

--Toggle sound output between Sony TV and Headphones.
--Script requires Universal Access Assistive Devices to be enabled.
tell application "System Preferences" to activate
tell application "System Events"
get properties
tell process "System Preferences"
click menu item "Sound" of menu "View" of menu bar 1
delay 1
set theRows to every row of table 1 of scroll area 1 of ¬
tab group 1 of window "sound"
set theOutputs to {} as list
--Scan through each row of option list and
--toggle first false selection found to true
repeat with aRow in theRows
if selected of aRow is false then
set selected of aRow to true
exit repeat
end if
end repeat
delay 2
end tell
end tell
tell application "System Preferences" to quit


The main modification I did, apart from adding comments, was to the three rows shown in blue.  The original script searched though the two rows of options in the Preferences menu for a match with the desired output, either "Headphones" or "Sony TV", and then selected that row.  I changed the search strategy to look for a row which was presently not selected, and select that one, then exit the search.  This had the effect of toggling the audio output between the two available options each time the script is run.

To simply activate the script from the Logitech remote, I configured iRed Lite to use the # key on the remote which is not used for other functions in EyeTV, the software we most commonly use when watching TV.  I had already linked the # key to a script which zoomed out the Mac screen to full screen view and I wanted to keep this feature.  So, I used another feature of iRed Lite so that the audio output toggle script was activated when the # key was held down for a second or so. This enables two functions to be activated from the same key.

One trick when plugging the headphones into the Mac Mini, is that the TV volume and mute controls don't work for the headphones.  I had already configured some of the programmable buttons on the Logitech 535 remote for the EyeTV volume and mute controls, so these can be used instead when watching EyeTV.  If watching a online video in a browser, the video viewer generally has mute and volume controls which can be used.

In the process of creating the script to toggle the audio outputs, I also created scripts in iRed Lite to select specific audio outputs although I am not actually using these at the moment.  For these scripts, the blue highlighted lines in the main script above are replaced with specific searches as follows:

if (value of text field 1 of aRow as text)
is equal to "SONY TV" then
set selected of aRow to true
exit repeat
end if

if (value of text field 1 of aRow as text)
is equal to "Headphones" then
set selected of aRow to true
exit repeat
end if

Potentially, I could link these scripts to specific keys on the Logitech remote to select Headphones or Sony TV direct, but I intend to try the toggle arrangement for a while first.