How to control Windows system volume using JScript or VBScript?

前端 未结 7 609
说谎
说谎 2020-12-03 06:10

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?

相关标签:
7条回答
  • 2020-12-03 06:28

    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
    
    0 讨论(0)
  • 2020-12-03 06:33

    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))
    
    0 讨论(0)
  • 2020-12-03 06:40

    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.

    0 讨论(0)
  • 2020-12-03 06:41

    I used nircmd.exe along with vbscript to control system volume.

    Set objShell = CreateObject("WScript.Shell") 
    If Not WScript.Arguments.Named.Exists("elevate") Then
      CreateObject("Shell.Application").ShellExecute WScript.FullName _
        , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
      WScript.Quit
    End If
    objShell.Run("nircmd.exe setvolume 0 0 0") 
    
    'Set WshShell = WScript.CreateObject("WScript.Shell")
    'WshShell.Popup "Success", "5", "MuteSpeaker"
    
    ' to successfully run this vbscript during log-off, nircmd.exe during should be present in "C:\Windows\System32"
    ' [cscript //X scriptfile.vbs MyArg1 MyArg2]
    
    0 讨论(0)
  • 2020-12-03 06:49

    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
    
    0 讨论(0)
  • 2020-12-03 06:50

    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.

    0 讨论(0)
提交回复
热议问题