volume control question

Get help with using AutoHotkey and its commands and hotkeys
Anamacha
Posts: 8
Joined: 18 Jun 2018, 14:46

volume control question

10 Oct 2018, 17:37

Hey all —

Is it possible to control volume of specific apps with AHK? Or even the system volume?

What I'd like to do is hit one key which reduces Spotify's volume by half. Later I'd hit the same key again, and it would be restored to its original level. Think of a Mute button on a TV remote — my Toshiba TV mutes by half when you hit the button, mutes completely on the second press, and restores to full on the 3rd.

I've searched the forums here and have found a couple things that are kind of what I want, but not fully.

thanks!
eqv
Posts: 66
Joined: 18 Sep 2018, 22:17

Re: volume control question

11 Oct 2018, 09:56

Quite tricky, but it's possible.
·
First run the volume mixer (right click on the volume icon), or run this command:

Code: Select all

Run, "%A_WinDir%\System32\SndVol.exe"
·
Then check the name of the desire volume. You do not need the full name, just a part.
·
In the script below, I show you using as example "System Sounds":
(I tested on Windows 10, if there is any problem; let me know.)

Code: Select all

#NoEnv		;; performance stuff, ignore this part.
#UseHook
#KeyHistory, 0
ListLines, 0
SetBatchLines, -1
SetWinDelay, -1
SetKeyDelay, -1
;; ·····························································
vol("System Sounds",5)	;; <<<< FUNCTION [Left=Name("System Sounds") | Right=Volume(5)]

;; ·····························································
vol(b,a) {
	If (WinExist("ahk_exe SndVol.exe")) {
	WinGet, e, PID, ahk_exe SndVol.exe
	} else {
	Run, "%A_WinDir%\System32\SndVol.exe",,, e
	WinWait, % "ahk_pid " . e,, 3
	} WinMove, % "ahk_pid " . e,, a_screenwidth, a_screenheight
	c:=1
	While !(inStr(d,b) || (d="" && c<>1)) {
	ControlGetText, d, % "Static" . (c*3)-1, % "ahk_pid " . e
	c++
	} Loop, 5
	ControlSend, % "msctls_trackbar32" . c, {PgDn}, % "ahk_pid " . e
	Loop, % a//20
	ControlSend, % "msctls_trackbar32" . c, {PgUp}, % "ahk_pid " . e
	Loop, % mod(a,20)
	ControlSend, % "msctls_trackbar32" . c, {Up}, % "ahk_pid " . e
	Sleep, 200
	WinClose, % "ahk_pid " . e
}
If you want to set the volume at half, I think it's better to "hardcode" it (example, gives volume 40 and then 20). I don't know how to get the current volume of individual mixer :( .
·
I think this example, should work for your case (replace "F1" with your hotkey, it will work as a toggle between 40 and 20 volume):

Code: Select all

#NoEnv		;; performance stuff, ignore this part.
#UseHook
#KeyHistory, 0
ListLines, 0
SetBatchLines, -1
SetWinDelay, -1
SetKeyDelay, -1
;; ·····························································
F1::(t ? (vol("spotify",20), t=!t) : (vol("spotify",40), t=!t))		;; I don't own spotify, just guesing.

;; ·····························································
vol(b,a) {
	If (WinExist("ahk_exe SndVol.exe")) {
	WinGet, e, PID, ahk_exe SndVol.exe
	} else {
	Run, "%A_WinDir%\System32\SndVol.exe",,, e
	WinWait, % "ahk_pid " . e,, 3
	} WinMove, % "ahk_pid " . e,, a_screenwidth, a_screenheight
	c:=1
	While !(inStr(d,b) || (d="" && c<>1)) {
	ControlGetText, d, % "Static" . (c*3)-1, % "ahk_pid " . e
	c++
	} c-=1
	Loop, 5
	ControlSend, % "msctls_trackbar32" . c, {PgDn}, % "ahk_pid " . e
	Loop, % a//20
	ControlSend, % "msctls_trackbar32" . c, {PgUp}, % "ahk_pid " . e
	Loop, % mod(a,20)
	ControlSend, % "msctls_trackbar32" . c, {Up}, % "ahk_pid " . e
	Sleep, 200
	WinClose, % "ahk_pid " . e
}
There is probably better solutions with Spotify on specific, for example:
https://www.reddit.com/r/AutoHotkey/com ... r_spotify/

Also, autohotkey have several commands for controlling the general sound and its devices:
https://autohotkey.com/docs/commands/SoundSet.htm
https://autohotkey.com/docs/commands/SoundGet.htm

Cheers,
Last edited by eqv on 13 Oct 2018, 17:12, edited 1 time in total.
Anamacha
Posts: 8
Joined: 18 Jun 2018, 14:46

Re: volume control question

12 Oct 2018, 02:00

thanks mate!

Unfortunately having to open the volume mixer beforehand is a dealbreaker for me - if I have to do that I might as well just do the volume changing myself. Besides, I wouldn't know the first thing about what I was coding — as I'm not an AHK programmer.

Thank you though <3
WingbtZ
Posts: 22
Joined: 09 Aug 2018, 04:41

Re: volume control question

12 Oct 2018, 02:21

Anamacha wrote:thanks mate!

Unfortunately having to open the volume mixer beforehand is a dealbreaker for me - if I have to do that I might as well just do the volume changing myself. Besides, I wouldn't know the first thing about what I was coding — as I'm not an AHK programmer.

Thank you though <3
You don't need to open mixer? Not manually atleast.. As already said.
eqv wrote:Quite tricky, but it's possible.
·
First run the volume mixer (right click on the volume icon), or run this command:

Code: Select all

Run, "%A_WinDir%\System32\SndVol.exe"
If the thing is that you don't want the window to pop up.. I guess you could use something like

Code: Select all

WinHide
? Implementing it shouldent be that hard.. But I am not caffeinated enough to try it atm..
eqv
Posts: 66
Joined: 18 Sep 2018, 22:17

Re: volume control question

12 Oct 2018, 03:03

Anamacha wrote: open the volume mixer beforehand is a dealbreaker for me
I am sorry, there has been a misunderstand :| , you don't have to manually open it at all.
The reason to open it is to know which name for "spotify" is in the volume mixer (only once, the name should keep the same always).
(for example, chrome always keep "Google Chrome" at the end)
See the attached image for example at the bottom.

Code: Select all

#NoEnv		;; performance stuff, ignore this part.
#UseHook
#KeyHistory, 0
ListLines, 0
SetBatchLines, -1
SetWinDelay, -1
SetKeyDelay, -1
;; ·····························································

F1::(t ? (vol("spotify",20), t=!t) : (vol("spotify",40), t=!t))

;; ·····························································
vol(b,a) {
	If (WinExist("ahk_exe SndVol.exe")) {
	WinGet, e, PID, ahk_exe SndVol.exe
	} else {
	Run, "%A_WinDir%\System32\SndVol.exe",,, e
	WinWait, % "ahk_pid " . e,, 3
	} WinMove, % "ahk_pid " . e,, a_screenwidth, a_screenheight
	c:=1
	While !(inStr(d,b) || (d="" && c<>1)) {
	ControlGetText, d, % "Static" . (c*3)-1, % "ahk_pid " . e
	c++
	} c-=1
	Loop, 5
	ControlSend, % "msctls_trackbar32" . c, {PgDn}, % "ahk_pid " . e
	Loop, % a//20
	ControlSend, % "msctls_trackbar32" . c, {PgUp}, % "ahk_pid " . e
	Loop, % mod(a,20)
	ControlSend, % "msctls_trackbar32" . c, {Up}, % "ahk_pid " . e
	Sleep, 200
	WinClose, % "ahk_pid " . e
}
Put the name in the script, and then run it. You activate it with "F1"
It should do it so fast that you barely notice the window, if you can.

-------------------------------------------------
WingbtZ wrote:If the thing is that you don't want the window to pop up.. I guess you could use something like
Only as a tip; that I thought initially. But for some reason, autohotkey did not detect when it was hidden (and yes, previously activated "DetectHiddenWindows"). So I just move the window outside the screen when appears :D .
Attachments
tutorial.jpg
(100.02 KiB) Downloaded 94 times
Last edited by eqv on 13 Oct 2018, 02:39, edited 1 time in total.
roysubs
Posts: 329
Joined: 29 Sep 2018, 16:37

Re: volume control question

12 Oct 2018, 09:36

I use this that I found somewhere because it is utterly brilliant and just does everything amazingly well.
Just move the mouse anywhere at all over the length of the taskbar and then roll the wheel. Simple and perfect.
You can change the value for Volume_Up / Volume_Down to 4 or 6 if you want huge jumps in volume.

Along with this, I would recommend installing "Magic Actions" for Chrome/Firefox which creates a similar thing for active YouTube videos.
So just move the mouse over the video window in youtube, and you can roll the mouse up and down to control the YouTube specific volume.

I guess it should be possible to extend the below to work for any app so that #If MouseIsOver("ahk_class <replace with class of the app you want to control>) and from there change the volume of that thing if it offers it's own volume as a control (though I'm not 100%, maybe one of our experts could extend the below so that if MouseIsOver on iTunes, it changes iTunes volume etc?).

Code: Select all

/*
    Adjust volume by scrolling the mouse wheel over the taskbar.
    ##########
*/
#If MouseIsOver("ahk_class Shell_TrayWnd")
WheelUp::Send {Volume_Up 2}
WheelDown::Send {Volume_Down 2}

MouseIsOver(WinTitle)
{
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}
#If
Anamacha
Posts: 8
Joined: 18 Jun 2018, 14:46

Re: volume control question

12 Oct 2018, 16:51

roysubs wrote:I use this that I found somewhere because it is utterly brilliant and just does everything amazingly well.
Just move the mouse anywhere at all over the length of the taskbar and then roll the wheel. Simple and perfect.
You can change the value for Volume_Up / Volume_Down to 4 or 6 if you want huge jumps in volume.
I dropped this in my .ahk file and it works great. Is there a way to modify this script so that it affects ONLY Spotify and not the SYSTEM volume?

Maybe there's a way to roll over the Spotify window, roll the wheel, and Spotify's volume will go down — This seems to be combining this idea with the previous ones.
roysubs
Posts: 329
Joined: 29 Sep 2018, 16:37

Re: volume control question

13 Oct 2018, 01:36

I've not worked out a way to do that, but as I say, you can get the class of the Spotify window using the Spy tool in the AHK install folder, then you just need to:
(EDIT: changed the below after looking at Spotify a bit)

a) Activate the Spotify window.
b) Best not to tie wheel roll to volume as the interface has lots of areas that wheel roll is required for(!), so would need something else, such as Ctrl + WheelRoll ... I don't know how to do that.
c) The shortcut keys for Spotify volume in windows are Ctrl+Up and Ctrl+Down, and additionally, Ctrl+Shift+Down is mute and Ctrl+Shift+Up is unmute.

So you'd have to consider what is going to be the most sensible/usable option if you really wanted to control Spotify at this level.
roysubs
Posts: 329
Joined: 29 Sep 2018, 16:37

Re: volume control question

13 Oct 2018, 11:39

Maybe this is useful from 8 months ago:
https://www.reddit.com/r/AutoHotkey/com ... r_spotify/

Also from 2013:
https://autohotkey.com/board/topic/9421 ... g-spotify/

And another one from 2009:
https://autohotkey.com/board/topic/3623 ... l-hotkeys/

I have to say, it is pretty cool how many tasks have already been done by others with AutoHotkey ...
User avatar
Flipeador
Posts: 1198
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: volume control question

13 Oct 2018, 13:34

Windws 1♂ Pro 64-Bits I make scripts for AHKv2 & WIN_7+ Argentina

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], malcev, WatsonEnterprises, wineguy and 43 guests