Define variables to be used for multiple threads in the same script Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
WeThotUWasAToad
Posts: 302
Joined: 19 Nov 2013, 08:44

Define variables to be used for multiple threads in the same script

10 Nov 2017, 20:45

Hello,

Is there a way to define variables so they can be used over multiple threads in the same script?

For example, suppose you define a rectangular area on your screen with the following dimensions:

xPos:=400, yPos:=300, Width:=3000, Height:=1500

Each of the following hotstring threads will move the active window to one of the four quadrants of that defined area.

Code: Select all

:*:mwlu::				;	Move Window Left-Upper
	xPos:=400, yPos:=300, Width:=3000, Height:=1500
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos, yPos, Width/2, Height/2				
	Return				
:*:mwll::				;	Move Window Left-Lower
	xPos:=400, yPos:=300, Width:=3000, Height:=1500
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos, yPos+Height/2, Width/2, Height/2				
	Return				
:*:mwru::			;	Move Window Right-Upper
	xPos:=400, yPos:=300, Width:=3000, Height:=1500
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos+Width/2, yPos, Width/2, Height/2				
	Return				
:*:mwrl::				;	Move Window Right-Lower
	xPos:=400, yPos:=300, Width:=3000, Height:=1500
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos+Width/2, yPos+Height/2, Width/2, Height/2
	Return

However, rather than having to include the variable values in each thread, is there a way to define them once and have each thread recognize them (ie something like this):

Code: Select all

xPos:=400, yPos:=300, Width:=3000, Height:=1500

:*:mwlu::				;	Move Window Left-Upper
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos, yPos, Width/2, Height/2				
	Return				
:*:mwll::				;	Move Window Left-Lower
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos, yPos+Height/2, Width/2, Height/2				
	Return				
:*:mwru::			;	Move Window Right-Upper
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos+Width/2, yPos, Width/2, Height/2				
	Return				
:*:mwrl::				;	Move Window Right-Lower
	WinGetActiveTitle, title				
	WinMove, %Title%,, xPos+Width/2, yPos+Height/2, Width/2, Height/2
	Return
Is that doable?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: Define variables to be used for multiple threads in the same script  Topic is solved

10 Nov 2017, 23:43

I think you've answered your own question (assuming you tried the script you showed and it worked). I.e. yes it's doable.
WeThotUWasAToad
Posts: 302
Joined: 19 Nov 2013, 08:44

Re: Define variables to be used for multiple threads in the same script

11 Nov 2017, 14:22

Oops. That's embarrassing. :oops:

I did try it earlier but could not get it to work without having the variables defined within each thread. However, I just tried it again and you are right, it works great so there was obviously something amiss with my first try. :roll:

So it seems the takeaway message here is that once a value is assigned to a variable, the variable will continue to be recognized as that value (unless a different value is assigned at some point) throughout the remainder of the script and regardless of the number of threads. Is that correct?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: Define variables to be used for multiple threads in the same script

12 Nov 2017, 06:02

WeThotUWasAToad wrote:Oops. That's embarrassing. :oops:
So it seems the takeaway message here is that once a value is assigned to a variable, the variable will continue to be recognized as that value (unless a different value is assigned at some point) throughout the remainder of the script and regardless of the number of threads. Is that correct?
Generally yes but it can depend on the "scope" of the variable too. In basic terms the scope of a variable is usually either local or global depending on where it is first defined. In your above code you are using global variables. If you created a function, any variables defined in that function would be local to that function, but this does depend on how you code the function. If you want to learn more about it I suggest you read up on it in the docs for a proper explanation. (Look for things like variables scope, local, global etc).

Return to “Ask For Help”

Who is online

Users browsing this forum: Clemens375 and 32 guests