Not so long ago I bought a bluetooth stereo headset (Motorola S9-HD by the way, this one:

Ogri's Playground - ogri.me | Bluetooth stereo headset Motorola S9-HD

Cool sound, built-in microphone. I'm very happy about the device and can recommend it to anyone). Paired the headset with my laptop and have been enjoying the brilliant sound quality. Sometimes I've also been connecting my good old wired headphones, for example, when forget to charge the battery of the new ones. The process of switching playback devices is not so time-consuming: right-click on the Speaker icon in the system tray -> select Playback Devices -> in the Playback tab of the Sound dialog choose the appropriate device and then click the Set Default button. But still it's annoying to perform this sequence every time you want to pass to different device, so I was wondering if there is an easy way to automate it.

The utility AutoIt is perfectly matched for this purpose (you can download it for free at the official site). Also the appropriate script has been found in the Web, here it is:

 ;-----Configuration-----
 ;The title of the sound config window.
 Dim $ConfigWindowTitle = "Sound"
 ;-----End of configuration----
 
 Dim $ItemNumber = 1
 If $CmdLine[0] >= 1 Then ;If we have a parameter...
     $ItemNumber = $CmdLine[1] ;...we should press the button the specified number of times.
 EndIf
 
 Run("control mmsys.cpl") ;Run the sound control applet and hide it.
 
 WinWaitActive($ConfigWindowTitle) ;Wait for it to be active before sending keystrokes.
 
 Send("{TAB}{TAB}{TAB}{TAB}") ;Put the focus on the list
 
 For $i = 1 to $ItemNumber Step 1
     Send("{DOWN}")
 Next
 
 Send("!s") ;Press Alt + S to set the selected device as the default.
 WinClose($ConfigWindowTitle)

Copy this code to a text file and save it with the extension .au3. For example, toggle-playback.au3. Place it to the scripts folder of AutoIt3, I used an existing folder Examples. Then open the Sound dialog as described above. Mine looks like this:

Ogri's Playground - ogri.me | Sound -> Playback Dialog

I'm interested in first two devices. Bluetooth Audio Device - this is the headset, number 1 in the list. Number 2 - built-in speakers and sound card output. We'll need these numbers for further customization, so let's remember them.

From now on there are two possible alternatives.

1. Create two (or more - according to the number of switching devices) text files with an extension typical for console batch files - .cmd or .bat. Let's name them, for example, Bluetooth.cmd and Speakers.cmd. Open them for editing and type the following code.

Bluetooth.cmd file:

echo off
D:\Common\AutoIt3\AutoIt3_x64.exe D:\Common\AutoIt3\Examples\toggle-playback.au3 1

D:\Common in my case - the path to the folder AutoIt3. Replace it with your own. I've been also using the 64-bit version of AutoIt, owners of 32-bit system are to replace AutoIt3_x64.exe with AutoIt3.exe. The command itself, as is clear, uses two parameters. The first one is the script file name (including full path), and the second parameter is the serial number of an appropriate playback devices in the list, as clarified earlier.

Similarly, Speakers.cmd file will contain exactly the same code, except that the value of the second parameter is 2:

echo off
D:\Common\AutoIt3\AutoIt3_x64.exe D:\Common\AutoIt3\Examples\toggle-playback.au3 2

Now launching of each batch file will activate the appropriate playback device.

2. The folder Aut2Exe contains utility of the same name for compilation of .au3 script files to an executable. It is extremely simple and easy to use. It's required to fill input boxes with the source script file, path and name of exe-file, and optionally - assign an icon and bit capacity:

Ogri's Playground - ogri.me | Program Aut2Exe

Press the Convert button. The file TogglePlayback.exe will appear in the subfolder Examples.

Then, as in the first option, create two batch files Bluetooth.cmd and Speakers.cmd. Since both the functionality of AutoIt and the script are already built into TogglePlayback.exe, it's enough to run it with a single parameter - the number of your device. Content of Bluetooth.cmd is:

echo off
D:\Common\AutoIt3\Examples\TogglePlayback.exe 1

Code of Speakers.cmd, respectively -

echo off
D:\Common\AutoIt3\Examples\TogglePlayback.exe 2

That's it. Let's enjoy the opportunity to switch the sound between playback devices in one click.

Actually, AutoIt is a powerful tool for automating of various system processes. I would recommend to both myself and you to explore its functionality. But use it with reasonable care, get well-tested scripts from trusted sources to avoid potential issues in you system.

Comments  

Overkill
0 # Overkill 2013-08-22 03:28
Not a bad script! I am working on something similar at the moment.

As a recommendation, I would recommend storing the current audio device (IE: 1 or 2) in the registry using RegWrite(...) at the end of the script, and at the beginning of the script read the value with $DEVICE = RegRead(...)

You can handle the values a few different ways:
If there are only two devices you want to use, and they show up adjacent to each other in the list, you can use Abs($DEVICE-$X) where $X = the sum of both positions (for 1,2 use 3 ... for 3,4 use 7) or you can use a switch/case expression for non-adjacent or more than two values - Switch $DEVICE, CASE 1, CASE 4, CASE 6 (etc.)
Ogri
0 # Ogri 2013-08-23 00:57
Dear Overkill, thanks for your comment.

This script is really not too bad, but, as I mentioned in the article, it have been found in the internet, so it's written by someone else and I've been using it as is in this scenario.

Nevertheless, being a programmer, I can evaluate your recommendations and am considering them valuable. Your enhancement of the script is absolutely correct and may save the work - only one batch or exe file is needed to be created. Good alternative, especially if the number of devices is relatively large. Nice stuff!
binary_atrophy
0 # binary_atrophy 2016-08-20 17:26
So you took someone else's code, without their permission, and posted it on your website without even crediting the author?

Stealing other people's work doesn't make you a programmer :sigh:
Ogri
0 # Ogri 2016-08-20 20:57
No, it doesn't. As well as your stupid comment doesn't make you a clever person.

Users must be registered and logged in to post comments.

By working with this site, you agree to our use of cookies necessary to keep the settings you select, as well as for the normal operation of Google services.