Assigning ahk_exe with variable Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Finallf
Posts: 18
Joined: 30 Oct 2020, 13:52

Assigning ahk_exe with variable

Post by Finallf » 30 Oct 2020, 14:23

I am passing a script v1 to v2.

This way it works:

Code: Select all

SetTimer "checkwin", 1000

checkwin() {
	if !WinExist("ahk_exe snes9x-x64.exe")
		ExitApp
}
But I need it to work like this:

Code: Select all

AhkName := "ahk_exe snes9x-x64.exe"
SetTimer "checkwin", 1000

checkwin() {
	if !WinExist("%AhkName%")
		ExitApp
}
or like this:

Code: Select all

AhkName := "snes9x-x64.exe"
SetTimer "checkwin", 1000

checkwin() {
	if !WinExist("ahk_exe %AhkName%")
		ExitApp
}

I tried in many ways and nothing to accept the variable.

User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Assigning ahk_exe with variable  Topic is solved

Post by kczx3 » 30 Oct 2020, 14:41

If you need that variable's value to be bound to the function then I guess you will need to make the variable global. Or pass it via a fat arrow function.

GLOBAL

Code: Select all

global AhkName := "ahk_exe snes9x-x64.exe"
SetTimer "checkwin", 1000

checkwin() {
	if !WinExist(AhkName)
		ExitApp
}
FAT ARROW

Code: Select all

AhkName := "ahk_exe snes9x-x64.exe"
SetTimer (*) => checkwin(AhkName), 1000

checkwin(exe) {
	if !WinExist(exe)
		ExitApp
}

Finallf
Posts: 18
Joined: 30 Oct 2020, 13:52

Re: Assigning ahk_exe with variable

Post by Finallf » 30 Oct 2020, 15:04

Thanks, it worked.
I will use it global because I have other places to use the variable, so it is easier to use.

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Assigning ahk_exe with variable

Post by Helgef » 31 Oct 2020, 07:17

Instead of the fat-arrow example, it is often better to use a boundfunc. Eg, SetTimer func('checkwin').bind(AhkName), 1000. Because the fat-arrow still depends on the global variable AhkName, the boundfounc binds the value and doesn't depend on the variable after it has been created.

User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Assigning ahk_exe with variable

Post by kczx3 » 31 Oct 2020, 07:49

The OP wanted that variables value to be bound at the time the function was called. So binding it wouldn’t help because then it’s static. I suppose you could bind an object with a property.

Post Reply

Return to “Ask for Help (v2)”