Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autohotkey - How to adjust volume only of a specific program?

Tags:

autohotkey

I want to control volume of a specific program and not the whole master volume.

I found THIS thread that has code for controlling only the volume of Windows Media Player from Volume Mixer .

This is the whole script:

SetTitleMatchMode, 3
SndVolWasStarted = 0

;Turn off SndVol after 1 second 
Loop {
  Sleep, 10
  If SndVolWasStarted = 1
  {
    GetKeyState, StateF1, F1
    GetKeyState, StateF2, F2
    If (StateF1 = "D" or StateF2 = "D")
    SndVolStartTime = %A_Now%
      Else {
        If ((A_Now - SndVolStartTime > 1) and WinExist("ahk_class #32770"))
        WinClose, ahk_class #32770
      }
    IfWinNotExist, ahk_class #32770
    SndVolWasStarted = 0
  }
}

;Hotkey to decrease volume
F1::
IfWinExist, Windows Media Player 
{
  IfWinNotExist, ahk_class #32770
  {
    Run, "%A_WinDir%\System32\SndVol.exe" -r 88888888
    WinWait, ahk_class #32770
    SndVolWasStarted = 1
  }
  ToolbarWindowNumber = 322
  msctls_trackbarNumber = 321
  Loop {
    ControlGetText, ControlName, ToolbarWindow%ToolbarWindowNumber%, ahk_class #32770
    If ControlName = Mute for Windows Media Player
    {
      ControlSend, msctls_trackbar%msctls_trackbarNumber%, {Down}, ahk_class #32770 ; Use {Down 2} to change sound level faster
      Break
    } Else {
      If ToolbarWindowNumber < 328
      {
        ToolbarWindowNumber := ToolbarWindowNumber + 2
        msctls_trackbarNumber := msctls_trackbarNumber + 1
      } Else {
        If ToolbarWindowNumber = 328
        {
          ToolbarWindowNumber = 3210
          msctls_trackbarNumber := msctls_trackbarNumber + 1
        } Else {
          If ToolbarWindowNumber < 3242
          {
            ToolbarWindowNumber := ToolbarWindowNumber + 2
            msctls_trackbarNumber := msctls_trackbarNumber + 1
          } Else {
            MsgBox, 16, AutoHotkey, ERROR: Application's volume control was not found!`nThis could occur if the Volume Mixer has more than 20 opened applications
            Break
          }
        }
      }
    }
  }
}
Return

;Hotkey to increase volume
F2::
IfWinExist, Windows Media Player
{
  IfWinNotExist, ahk_class #32770
  {
    Run, "%A_WinDir%\System32\SndVol.exe" -r 88888888
    WinWait, ahk_class #32770
    SndVolWasStarted = 1
  }
  ToolbarWindowNumber = 322
  msctls_trackbarNumber = 321
  Loop {
    ControlGetText, ControlName, ToolbarWindow%ToolbarWindowNumber%, ahk_class #32770
    If ControlName = Mute for Windows Media Player
    {
      ControlSend, msctls_trackbar%msctls_trackbarNumber%, {Up}, ahk_class #32770 ; Use {Up 2} to change sound level faster
      Break
    } Else {
      If ToolbarWindowNumber < 328
      {
        ToolbarWindowNumber := ToolbarWindowNumber + 2
        msctls_trackbarNumber := msctls_trackbarNumber + 1
      } Else {
        If ToolbarWindowNumber = 328
        {
          ToolbarWindowNumber = 3210
          msctls_trackbarNumber := msctls_trackbarNumber + 1
        } Else {
          If ToolbarWindowNumber < 3242
          {
            ToolbarWindowNumber := ToolbarWindowNumber + 2
            msctls_trackbarNumber := msctls_trackbarNumber + 1
          } Else {
            MsgBox, 16, AutoHotkey, ERROR: Application's volume control was not found!`nThis could occur if the Volume Mixer has more than 20 opened applications
            Break
          }
        }
      }
    }
  }
}
Return

I tried it and it works with Windows Media Player but I can't make it to work on Google Chrome.

I changed some piece of code on IfWinExist, Windows Media Player and changed it to IfWinExist, Google Chrome but didn't adjust the volume of Chrome.

I'm running windows 10 64-bit

like image 282
chico Avatar asked Jul 18 '16 09:07

chico


1 Answers

  1. Replace all IfWinExist, Windows Media Player
    with IfWinExist ahk_class Chrome_WidgetWin_1
  2. Replace all If ControlName = Mute for Windows Media Player
    with if (ControlName ~= "Mute for.*- Google Chrome")

You can use Autohotkey Window Spy utility to find the values. Rightclick any AHK icon in the taskbar tray notification area and select "Window Spy".


Or use the awesome nircmd utility:

^#Up::run nircmd changeappvolume focused +0.1
^#Down::run nircmd changeappvolume focused -0.1
^#Numpad0::run nircmd muteappvolume focused 2

Ctrl+Win+Up = volume up active app
Ctrl+Win+Down = volume down active app
Ctrl+Win+Numpad 0 = mute/unmute active app

Replace focused with app exe name to control a specific app, e.g. chrome.exe, vlc.exe

like image 159
wOxxOm Avatar answered Oct 16 '22 09:10

wOxxOm