ADBLOCK_MSG
Fast switching playback devices using AutoItPublished: Thursday, 26 May 2011 15:49 Written by Ogri Hits: 15145
Not so long ago I bought a bluetooth stereo headset (Motorola S9-HD by the way, this one:
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:
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:
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.
Latest News
-
Saturday, 10 February 2018 18:48
Implementation of Windows Mail into Windows 10 version 1709. MAPI restored! -
Tuesday, 02 January 2018 16:40
Implementation of Windows Mail into Windows 10 version 1709 -
Friday, 18 November 2016 19:14
Reinstall Windows 10 without losing programs and files (Repair install) -
Thursday, 16 April 2015 19:17
Reinstall Windows 7 without losing programs and files (Repair install) -
Wednesday, 07 May 2014 14:08
Enabling Windows Mail in Windows 7 - retrospective review three years later
Articles Most Read
-
57567
Enabling Windows Mail in Windows 7 -
29958
Reinstall Windows 7 without losing programs and files (Repair install) -
29220
Migrating Windows 2000 / XP to a VMWare virtual machine -
20238
Windows XP Installation: The "34 minutes remaining" syndrome -
17319
How to determine whether my PC is running Windows 8 of 32 bit or 64 bit
Login
Guest Column
Recent comments
-
Implementation of Windows Mail into Windows 10 version 1709
-
Gerd Wrede
05.04.2020 16:16
Sehr geehrte Damen und Herren, das Problem, das ich in meiner Mail vom 03.04.2020 dargestellt habe, ist ...
-
Gerd Wrede
03.04.2020 09:06
Sehr geehrte Damen und Herren, großartig, dass Sie eine Lösung anbieten, mit der „Windows Mail“ ...
-
Gerd Wrede
05.04.2020 16:16
-
Implementation of Windows Mail into Windows 10 version 1709. MAPI restored!
-
Wolfram
15.05.2021 16:31
schick mir ne Mail, auf wolframjahn at t- .... meine Antwort war wohl zu groß, wurde immer als ...
-
Michael
04.02.2021 09:57
Are the zip archives no longer available? I can't download anything.
-
Mike
13.01.2021 17:32
Ok I think I fixed it I followed this, but instead of adding to WLM which i don't have i added them into ...
-
Mike
13.01.2021 17:20
This is the error It doesn't seem to be able to send or receive emails An unknown error has occurred.
-
Mike
13.01.2021 17:19
This is the error It doesn't seem to be able to send or receive emails An unknown error has occurred.
-
Mike
13.01.2021 12:22
Hi, Does this work with the latest update of windows 10 I'm having a few issues now since updating ...
-
Ronald
02.07.2020 12:35
Hi. I need assistance with step 5 "Copy all files from the SysWOW64 in the archive to the folder ...
-
kevin Malcolm
01.02.2020 02:08
Thank you for restoring Winmail.
-
Wolfram
15.05.2021 16:31
Comments
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.)
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!
Stealing other people's work doesn't make you a programmer