My first tutorial for the year! Only 12 days late…
For those who do not wish to read source is located here
So I got my hands on some really nice Bowers & Wilkins MM-1 speakers for my mac they have a built in DAC (Digital Analog Convertor) which means I can connect them up through a USB port instead of the line out (Which means my optical connection to my amp can stay in). Now here comes the problem if I want to change between the Amp or the speakers I have to open ‘System Preferences’ open the audio section open the output tab and select what I want my audio to go through.
Well thats just not gonna happen.
In my bragging to my mate Sam about my sexy speakers, he said that he has to do the same thing. I said well I’m about to write a script to do it for me. He asked if I could show him how.
So I siad yeah I will write up a tutorial. A simple Applescript application that sits on my dock that when you click it, it will toggle between the two audio outputs. There is no error handling in this version so if the USB DAC is not connected it will cause problems. Error handling will come later. My end goal is to have a physical button on my desktop which when pushed will switch it for me, with a screen displaying the current audio output. I think i might even put the current itunes song information as well maybe.
The above image is a example of the selected output. (On a side note how good is my pixel art!)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
--Writing by Ashley Hughes derived from examples found at --http://blog.plasticmind.com/code/audio-output-toggle-applescript/ --Jan 2013 --If shit blows up not my problem! --Basic Version of the software --Requires you to manually edit the names of the devices --Selected for the notification. You will also have to have --Enable access for assistive devices option on in the --Accessibility section of system preferences for this --Script to be able to function. --Script and example code for Notifications Scripting from --http://www.cooperative-fruitiere.com/notifications/index_en.html --Note you will have to have download Notifications scripting app --From their website and place it in you Applications folder --TODO --Tidy update code --Add error handling if two audio devices are not connected tell application "System Preferences" activate set current pane to pane "com.apple.preference.sound" end tell tell application "System Events" tell application process "System Preferences" tell tab group 1 of window "Sound" click radio button "Output" if (selected of row 2 of table 1 of scroll area 1) then set selected of row 1 of table 1 of scroll area 1 to true set deviceselected to "Digital Out" else set selected of row 2 of table 1 of scroll area 1 to true set deviceselected to "MM-1 DAC" end if end tell end tell end tell tell application "System Preferences" to quit tell application "Notifications Scripting" --set event handlers script path to (path to me) set dict to {theName:"Notifications Scripting", theVersion:"1.0", theScript:event handlers script path} display notification "HughesY Audio Changer" subtitle "Swapping Audio" message deviceselected user info dict end tell using terms from application "Notifications Scripting" on notification delivered end notification delivered on notification activated end notification activated end using terms from |
I have just smashed some code together it works well for me, note it is not the best way of doing it or the most correct. But it works and as this project gets more complicated I will rewrite the code nicely.
I will break the above code into managable chunks and explain what is going on. If you do want to follow on copy the above code into Applescript editor and have fun.
You will also have to have Enable access for assistive devices option on in the Accessibility section of system preferences for this Script to be able to function.
Please note that the Notification section was not developed by me. You will have to download the application from
http://www.cooperative-fruitiere.com/notifications/index_en.html
And then just place the application into your Applications folder. You do not have to open or run this Applescript just talks to it when it needs to. If you do open it you may need to kill the process with activity monitor if you get error 1000 from the Applescript editor
The Applescript code that we write use that application to display the Notifications in Mountain Lion.
|
1 2 3 4 |
tell application "System Preferences" activate set current pane to pane "com.apple.preference.sound" end tell |
- The above opens ‘System Preferences’ and selects the sound section.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
tell application "System Events" tell application process "System Preferences" tell tab group 1 of window "Sound" click radio button "Output" if (selected of row 2 of table 1 of scroll area 1) then set selected of row 1 of table 1 of scroll area 1 to true set deviceselected to "Digital Out" else set selected of row 2 of table 1 of scroll area 1 to true set deviceselected to "MM-1 DAC" end if end tell end tell end tell |
The above section is where we actually change the audio output selection over.
- Lines 1 – 4, Select the output tab of the audio page in system preferences
- Line 5, Checks which row is already selected if is row 2 then line 6 changes it to row 1 or if row 1 is already selected line 9 changes it to row 2.
- Line 7 & 10, set a variable ‘deviceselected’ to the name of the audio output so we can use it later. Not these are manually set and you will need to change the name to the name of your outputs.
- The rest is just closing of the tell’s and if statements that we opened in the first few lines.
|
1 |
tell application "System Preferences" to quit |
- Closes ‘System Preferences’ for us.
Now the rest of the code is for the Notifications, you do not have to use this section if you don’t want. I thought it was cool when it popped up to let me now that it worked and has changed over the output. As I said before you need the Notification scripting application. (This will only work on Mountain Lion!!!)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
tell application "Notifications Scripting" --set event handlers script path to (path to me) set dict to {theName:"Notifications Scripting", theVersion:"1.0", theScript:event handlers script path} display notification "HughesY Audio Changer" subtitle "Swapping Audio" message deviceselected user info dict end tell using terms from application "Notifications Scripting" on notification delivered end notification delivered on notification activated end notification activated end using terms from |
Ok so for the notifications
- Lines 1 – 6, tell application ‘Notifications Scripting’ the one you downloaded to display a notification
- Line 2, is not needed its there for my testing
- Line 3, sets a dictionary telling it that we will be handling all replies that come back and which version we would like to use.
- Line 4, is the actual notification.
- Lines 8 – 16, Just handle and replies that we get back, not I am not using any of them but they are still required to be there. If you would like to handle the user clicking on the notification etc. Your code would go in there.
Once all that is in the Applescript editor the easiest way to use it is to click ‘file’ -> ‘export’ and change the file format to Application save it some where and drag a copy on to your dock.
And that is it all done for the basic version. As I said before there is no error handling in this version. I plan on working on the hardware version next with a little screen and physical buttons, more than likely based around an Arduino.
Please subscribe and let me know how well it worked for you!

