This is how I solved it now. I know this is a sloppy time. It does not find the currently active device, instead it saves the information in a text file (which must already exist! With the current device name in it!). Yes, that’s terrible. But, given my knowledge of VBScript and current knowledge of the Windows registry, both of which are very close to zero, this is the best I could come up with!
I post this here, due to the lack of a simple answer elsewhere. If anyone has the best solutions, without any other programs, I will be very grateful for that.
In addition, if someone wants to use this code, please change the paths and file names to suit yours.
Dim objFso, objFileHandle, strDisplayString Set objFso = WScript.CreateObject("Scripting.FileSystemObject") Set readObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 1) strDisplayString = readObjFileHandle.ReadLine() readObjFileHandle.Close Set writeObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 2, "True") If StrComp(strDisplayString, "Headphones", vbTextCompare) = 0 Then 'MsgBox "Headphones - switching to Speakers" Set WshShell = CreateObject("WScript.Shell") cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 2, True) Set WshShell = Nothing writeObjFileHandle.Write("Speakers") Else 'MsgBox "Speakers - switching to Headphones" Set WshShell = CreateObject("WScript.Shell") cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Headphones""", 2, True) Set WshShell = Nothing writeObjFileHandle.Write("Headphones") End If writeObjFileHandle.Close
source share