Skip to content

Instantly share code, notes, and snippets.

@mherwig
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mherwig/8975353 to your computer and use it in GitHub Desktop.
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.
Headset
Speakers
:: 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
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
@mherwig
Copy link
Author

mherwig commented Feb 13, 2014

Preparations:

  1. Download NirCmd:
    x86:
    http://www.nirsoft.net/utils/nircmd.zip
    x64:
    http://www.nirsoft.net/utils/nircmd-x64.zip
  2. Extract NirCmd and place nircmd.exe and nircmdc.exe in the same folder as the other files
  3. In order to get the script working with the default settings you have to rename your Headset to 'Headset' and your Speakers to 'Speakers' (You can do this in Windows's audio devices manager). Or edit the lines in devices.txt to match your devices.

Usage:
Just run SwitchAudio.vbs by double clicking it or bind a hotkey of your keyboard to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment