Enable active sound device using VBScript?

I want to switch between two audio devices connected to my computer (Windows 7 32 bit). I looked through the question and found nircmd .

Now I can create two VBS files for switching between two devices. I was wondering if I can find out what the current active / standard sound device is, then I could put everything in one file.

My current code is

Set WshShell = CreateObject("WScript.Shell") cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 0, True) Set WshShell = Nothing 

In another file, instead of “Speakers” there are “Headphones”.

0
source share
1 answer

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 
+1
source

Source: https://habr.com/ru/post/1236623/


All Articles