-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromptSwitch.bat
82 lines (81 loc) · 2.89 KB
/
PromptSwitch.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@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.Displays prompt popup
::5.If selected "ok":
:: - Uses %SoundVolumeView% to switch default device to the other device
:: - Uses %SoundVolumeView% to set default volume to the other device's default volume
:: - Displays info popup for %TIMEOUT% seconds
::6.If selected "cancel":
:: - Exits
::---------------------------------------------------------------------
:: 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 & display
for /f "delims=" %%a in ('%SoundVolumeView% /GetColumnValue %H_DEVICE% %COLUMN%') do (
REM Headset
call if "%%a" == "%DEFAULT%" (
set "DEVICE=%S_DEVICE%"
set "VOLUME=%S_VOLUME%"
set "INFO=%H_DEVICE% → %S_DEVICE%"
GOTO DISPLAY
)
)
for /f "delims=" %%a in ('%SoundVolumeView% /GetColumnValue %S_DEVICE% %COLUMN%') do (
REM Speakers
call if "%%a" == "%DEFAULT%" (
set "DEVICE=%H_DEVICE%"
set "VOLUME=%H_VOLUME%"
set "INFO=%S_DEVICE% → %H_DEVICE%"
GOTO DISPLAY
)
)
:: Handle unknown device
set "ERR=Couldn't detect current device"
echo Current: %ERR%
CScript //nologo //E:JScript "%~F0" %TIMEOUT% "%ERR%" "err"
GOTO END
::---------------------------------------------------------------------
:DISPLAY
:: Display device
echo Prompt: %INFO%
CScript //nologo //E:JScript "%~F0" 0 "%INFO%"
::---------------------------------------------------------------------
:: Response. ok=1, cancel=2
if "%errorlevel%" == "1" (
REM Switch & display device
%SoundVolumeView% /SetDefault %DEVICE% all
%SoundVolumeView% /SetVolume %DEVICE% %VOLUME%
echo Switch: %INFO%
CScript //nologo //E:JScript "%~F0" %TIMEOUT% %DEVICE% "switch"
) else (
REM do nothing
echo Cancelled switching.
)
: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) {
if (WScript.Arguments.Unnamed(2)=="err")
WScript.Quit (WshShell.Popup(text ,timeout ,"Error", 0+48))
else if (WScript.Arguments.Unnamed(2)=="switch")
WScript.Quit (WshShell.Popup(text ,timeout ,"New default device", 0+64))
}
else
WScript.Quit (WshShell.Popup(text ,timeout ,"Switch device?", 1+32))