views:

787

answers:

2

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?

A: 

From within a browser window/web page - definitely no way at all. Browser sandbox security would prohibit it even if it were technically possible.

From within Windows Script Host (standalone .vbs or .js file) - no way that I am aware of. WMI does not provide control over this either, according to this question.

Tomalak
+1  A: 

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(String.fromCharCode(0xAD));

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
Helen
This works perfectly, even for Windows 7 (32-bit)! I have one correction, however, in that "mutesysvolume 0" is unmute and "mutesysvolume 1" is mute.
Sibo Lin
Oh, right. Fixed that.
Helen