Last active
August 29, 2015 13:56
-
-
Save mherwig/8975353 to your computer and use it in GitHub Desktop.
SwitchAudio.vbs is a script for switching between two audio devices in Microsoft Windows. You need NirCmd for this script to work (download it here: http://www.nirsoft.net/utils/nircmd.html). Please read my comment in this gist for further instructions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Headset | |
Speakers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: read two device names from the arguments list | |
set DEVICE_1=%1 | |
set DEVICE_2=%2 | |
:: query regedit | |
FOR /F "skip=2 tokens=2,*" %%A IN ('reg.exe query "HKEY_CURRENT_USER\Environment" /v "AUDIO_SWITCH_DEVICE"') DO set "CURRENT_DEVICE=%%B" | |
:: toggle devices | |
if "%CURRENT_DEVICE%" == "%DEVICE_1%" goto SET_TO_DEVICE_2 | |
if "%CURRENT_DEVICE%" == "%DEVICE_2%" goto SET_TO_DEVICE_1 | |
goto SET_TO_DEVICE_1 | |
:SET_TO_DEVICE_1 | |
set CURRENT_DEVICE=%1 | |
goto end | |
:SET_TO_DEVICE_2 | |
set CURRENT_DEVICE=%2 | |
goto end | |
:end | |
setx AUDIO_SWITCH_DEVICE %CURRENT_DEVICE% | |
nircmd.exe setdefaultsounddevice "%CURRENT_DEVICE%" | |
nircmd.exe trayballoon "Switched standard sound device" "Current device: %CURRENT_DEVICE%" "shell32.dll,168" 1500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set deviceNamesTextFile = objFSO.OpenTextFile("devices.txt", 1) | |
ReDim deviceNames(1) | |
Do Until deviceNamesTextFile.AtEndOfStream | |
deviceNames(UBound(deviceNames) - 1) = deviceNamesTextFile.ReadLine | |
ReDim Preserve deviceNames(UBound(deviceNames) + 1) | |
Loop | |
deviceNamesTextFile.Close | |
Set WshShell = CreateObject("WScript.Shell") | |
WshShell.Run chr(34) & "switchaudio.cmd" & Chr(34) & deviceNames(0) & " " & deviceNames(1), 0 | |
Set WshShell = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preparations:
x86:
http://www.nirsoft.net/utils/nircmd.zip
x64:
http://www.nirsoft.net/utils/nircmd-x64.zip
Usage:
Just run SwitchAudio.vbs by double clicking it or bind a hotkey of your keyboard to it