Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control Windows system volume using JScript or VBScript?

I want to control the volume of my Windows system from a JScript or VBScript script. Any ideas?

Also, can I unmute the system volume if it is muted?

like image 881
Sibo Lin Avatar asked Feb 07 '10 07:02

Sibo Lin


5 Answers

The best way I can see for manipulating the system volume level on Windows without the need for installing additional software is to use VBScript in one of the following ways:

Toggle muted: (already mentioned in a previous answer)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))

Increase volume level:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAF))

Decrease volume level:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAE))

This should work for most modern Windows machines. I've tested this on Windows 7 and 8.1 and it works fine even when run with "do as" in LC, so it is possible to embed these scripts within an executable and run them without the need of saving them as individual files.

like image 141
Ryan Avatar answered Oct 01 '22 07:10

Ryan


To mute or unmute the system volume, you can simulate the Mute key press using the WshShell.SendKeys method:

var oShell = new ActiveXObject("WScript.Shell");
oShell.SendKeys(Chr(&HAD));

As for changing the volume level from a script, there's a solution that involves some Windows automation, such as launching the System Volume applet and simulating the appropriate keyboard shortcuts in it, but I don't think it's reliable. Therefore I recommend that you use some external utility capable of changing the volume level, and call it from your script. For example, you could use the free NirCmd tool:

var oShell = new ActiveXObject("WScript.Shell");

// Increase the system volume by 20000 units (out of 65535)
oShell.Run("nircmd.exe changesysvolume 20000");

// Decrease the system volume by 5000 units
oShell.Run("nircmd.exe changesysvolume -5000");

NirCmd can also mute or unmute the system volume:

var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("nircmd.exe mutesysvolume 0");  // unmute
oShell.Run("nircmd.exe mutesysvolume 1");  // mute
oShell.Run("nircmd.exe mutesysvolume 2");  // switch between mute and unmute
like image 42
Helen Avatar answered Oct 01 '22 08:10

Helen


On Windows 7 I could do this by controlling the keystrokes using WScript.

set oShell = CreateObject("WScript.Shell") 
oShell.run"%SystemRoot%\System32\SndVol.exe" 
WScript.Sleep 1500 
oShell.SendKeys"{TAB} " ' Tab to the mute and press space
oShell.SendKeys"%{F4}"  ' ALT F4 to exit the app.

I saved it in to a file called Mute-Sound.vbs and created a shortcut on the desktop to assign a shortcut key. CTRL+ALT+F12. For some reason the keyboard shortcuts only work if they are on the desktop so I am not sure how reliable keyboard shortcuts are!

like image 20
MistyManor Avatar answered Oct 01 '22 06:10

MistyManor


Misty Manor's Answer Works on Windows Vita as well. This, i literally just made, does the same in a sense.

set oShell = CreateObject("WScript.Shell") 
oShell.run"%SystemRoot%\System32\SndVol.exe" 'Runs The Master Volume App.
WScript.Sleep 1500 'Waits For The Program To Open
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20, If It Is Muted Then It Will Unmute It
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys"%{F4}"  ' ALT F4 To Exit The App.

If you want to decrease the volume you would do

oShell.SendKeys("{PGDN}") 'It Will Decrease The Volume By 20
like image 44
Jonco98 Avatar answered Oct 01 '22 06:10

Jonco98


http://www.nilpo.com/2008/11/windows-xp/mute-sound-volume-in-wsh/ He uses a keystroke of a Multimedia Keyboard mute key. Clever ;-)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))
like image 28
macxcool Avatar answered Oct 01 '22 06:10

macxcool