Need help understanding the script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LG Zana
Posts: 33
Joined: 06 Dec 2021, 17:50

Need help understanding the script

Post by LG Zana » 01 Dec 2022, 12:38

I want to make a button that increases the volume of my apps, and I want to make the code behind it as small as possible so that I can use it in other buttons and hotkeys.

So I turned this

Code: Select all

!F9::
Send, {m Up}
Sleep, 15
Send, {k Up}
Sleep, 15
Run, "nircmd.exe" setappvolume "C:\Program Files\Google\Chrome\Application\chrome.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "Discord.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "csgo.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "foobar2000.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "hl.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "hl2.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "iw3mp.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "left4dead2.exe" 1
Sleep, 15
Run, "nircmd.exe" setappvolume "reactivedrop.exe" 1
Sleep, 15
WinClose, %Script1% ahk_class AutoHotkey
WinClose, %Script2% ahk_class AutoHotkey
WinClose, %Script3% ahk_class AutoHotkey
WinClose, %Script4% ahk_class AutoHotkey
WinClose, %Script5% ahk_class AutoHotkey
Return
Into this

Code: Select all

myAppsNormalVolumes := {"C:\Program Files\Google\Chrome\Application\chrome.exe": 1, "Discord.exe": 1, "csgo.exe": 1, "foobar2000.exe": 1, "hl.exe": 1, "hl2.exe": 1, "iw3mp.exe": 1, "left4dead2.exe": 1, "reactivedrop.exe": 1}
SetAppVolume(app, volume, wait:=5) {
	Run, "nircmd.exe" setappvolume "%app%" %volume%
	Sleep, %wait%
}
QuietApps(appsNormal) {
	Send, {m Up}
	Send, {k Up}
	for app, volume in appsNormal
		SetAppVolume(app, volume)
}
;------------------------------------------------------------------;
^!F9::
QuietApps(appsNormal)
WinClose, %Script1% ahk_class AutoHotkey
WinClose, %Script2% ahk_class AutoHotkey
WinClose, %Script3% ahk_class AutoHotkey
WinClose, %Script4% ahk_class AutoHotkey
WinClose, %Script5% ahk_class AutoHotkey
Return
But it doesn't increase the volume of the apps; what am I doing wrong?

One more question
Should i use

Code: Select all

QuietApps(appsNormal) {
..
	for app, volume in appsNormal
		SetAppVolume(app, volume)
Or

Code: Select all

QuietApps(myAppsNormalVolumes) {
..
	for app, volume in myAppsNormalVolumes
		SetAppVolume(app, volume)

User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: Need help understanding the script

Post by mikeyww » 01 Dec 2022, 14:55

1. Defining the function

Your choice of function parameter name makes no difference, as long as you use that name in the function. It is essentially a placeholder. It is any variable name, simply so that you have a way to refer to that same variable inside the function definition.

2. Calling the function
^!F9::
QuietApps(appsNormal)
The problem is that you passed a null array to your function. Use a real array name. It does not need to match the variable name that the function definition used. It does need to match in position: the first parameter in the call will be matched to the first parameter in the function definition.

This may seem counterintuitive at first, but it is how functions work. The idea of a function is that it is taking various kinds of input and then generating actions or output. The "placeholder" is simply saying, "Here is some input that we will use in the function". The variable name in the function definition exists only so that you can refer to it there.

LG Zana
Posts: 33
Joined: 06 Dec 2021, 17:50

Re: Need help understanding the script

Post by LG Zana » 01 Dec 2022, 15:24

@mikeyww Thank you for clarifying. I definitely need to do more research on functions to fully understand them.

User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: Need help understanding the script

Post by mikeyww » 01 Dec 2022, 16:29

Examples help, too.

Code: Select all

MsgBox, % sum(1, 2)

sum(a, b) {
 Return a + b
}

Code: Select all

c = 1
d = 2
MsgBox, % sum(c, d)

sum(a, b) {
 Return a + b
}

Code: Select all

MsgBox, % notsum(1, 3)
MsgBox, % notsum(3, 1)

notsum(a, b) {
 Return 2 * a + b
}

Post Reply

Return to “Ask for Help (v1)”