-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitchDevice.bat
66 lines (65 loc) · 2.47 KB
/
SwitchDevice.bat
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
60
61
62
63
64
65
66
@if (@CodeSection == @Batch) @then
@echo off
chcp 65001 > NUL
setlocal enabledelayedexpansion
set "VARS_PATH=variables.csv"
::---------------------------------------------------------------------
::1.Reads variables from %VARS_PATH%
::--Required vars: SoundVolumeView, TIMEOUT, COLUMN, DEFAULT, H_DEVICE, S_DEVICE, H_VOLUME, S_VOLUME--
::2.Uses %SoundVolumeView% to get the value of %COLUMN% for %S_DEVICE% and %H_DEVICE%
::3.Finds which device's state is set to the %DEFAULT% keyword
::4.Uses %SoundVolumeView% to switch default device to the other device
::5.Uses %SoundVolumeView% to set default volume to the other device's default volume
::6.Displays info popup for %TIMEOUT% seconds
::---------------------------------------------------------------------
::Default variables
for /f "tokens=1, 2 delims=," %%a in ('type %VARS_PATH%') do (
if %%a NEQ "" if %%b NEQ "" ( set "%%a=%%b" )
)
::---------------------------------------------------------------------
::Find default device & switch & display
for /f "delims=" %%a in ('%SoundVolumeView% /GetColumnValue %H_DEVICE% %COLUMN%') do (
REM Headset
call if "%%a" == "%DEFAULT%" (
REM Switch Headset -> Speakers
set "DEVICE=%S_DEVICE%"
set "VOLUME=%S_VOLUME%"
set "INFO=%H_DEVICE% → %S_DEVICE%"
GOTO SWITCH
)
)
for /f "delims=" %%a in ('%SoundVolumeView% /GetColumnValue %S_DEVICE% %COLUMN%') do (
REM Speakers
call if "%%a" == "%DEFAULT%" (
REM Switch Speakers -> Headset
set "DEVICE=%H_DEVICE%"
set "VOLUME=%H_VOLUME%"
set "INFO=%S_DEVICE% → %H_DEVICE%"
GOTO SWITCH
)
)
:: Handle unknown device
set "ERR=Couldn't detect current device"
echo Switch: %ERR%
CScript //nologo //E:JScript "%~F0" %TIMEOUT% "%ERR%" "err"
GOTO END
::---------------------------------------------------------------------
:SWITCH
:: Switch & display device
%SoundVolumeView% /SetDefault %DEVICE% all
%SoundVolumeView% /SetVolume %DEVICE% %VOLUME%
echo Switch: %INFO%
CScript //nologo //E:JScript "%~F0" %TIMEOUT% %DEVICE%
:END
goto :EOF
@end
//---------------------------------------------------------------------
// Start Cscript and display popup
if (WScript.Arguments.Unnamed.Count < 2) WScript.Quit(0);
var timeout = WScript.Arguments.Unnamed(0);
var text = WScript.Arguments.Unnamed(1);
var WshShell = WScript.CreateObject("WScript.Shell")
if (WScript.Arguments.Unnamed.Count > 2 && WScript.Arguments.Unnamed(2)=="err")
WScript.Quit (WshShell.Popup(text ,timeout ,"Error", 0+48))
else
WScript.Quit (WshShell.Popup(text ,timeout ,"New default device", 0+64))