Script freaks out when it runs

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

Script freaks out when it runs

21 Aug 2019, 18:13

Hello,

Can someone help me understand why this script goes crazy when I run it?

It is a looped script which includes a Left mouse click at one of 12 locations depending on the circumstances.

Fortunately, the click locations are evenly spaced vertically so I initially calculated the 12 vertical locations in a spreadsheet then pasted the exact cords for the 12 possible locations in the script. That way, I could simply remove a semicolon from one of the lines, to get the click location I wanted.

For example, the following script (which works great) executes the left-click at the 5th location (4610, 1171) each time it loops:

Code: Select all

F1::
;Sleep lengths:
    short := 99
    medium := 999
    long := 2299
Loop, 68
{
	CoordMode, Mouse, Screen
	GoSub, CaptureNotepadLine				; includes activating notepad

	;Click, 4610, 635						; 1st field
	;Click, 4610, 769						; 2nd field
	;Click, 4610, 903						; 3rd field
	;Click, 4610, 1037						; 4th field
	Click, 4610, 1171						; 5th field
	;Click, 4610, 1305						; 6th field
	;Click, 4610, 1439						; 7th field
	;Click, 4610, 1573						; 8th field
	;Click, 4610, 1707						; 9th field
	;Click, 4610, 1841						; 10th field
	;Click, 4610, 1975						; 11th field
	;Click, 4610, 2109						; 12th field
		Sleep, short
	Send ^v
		Sleep, short
	;Click, 4565, 505						; next entry
	Click, 4385, 1895						; create new entry
		Sleep, short
}
Return
The above approach worked well until I discovered that the coords and spacing for all 12 locations can be different in certain situations. As a result, I decided to use variables and expressions (in hopes of having far fewer coords to enter manually) and have the script calculate the click location each time it runs. I did this by defining position variables then attempted to specify the 12 possible click locations as shown
here:

Code: Select all

F1::
;Sleep lengths:
    short := 99
    medium := 999
    long := 2299
;Position variables:
	lower := 1915							; vertical location of the bottom field
	upper := 615							; vertical location of the top field
	difference := lower - upper				; pixel distance between top and bottom fields
	denominator := 10						; number of fields
	increment := difference / denominator	; vertical distance between any two fields
	xBase := 4460							; horizontal position for each click
	yBase := 590							; vertical position for the middle of the top field

Loop, 68
{
	CoordMode, Mouse, Screen
	GoSub, CaptureNotepadLine				; includes activating notepad

	;Click, %xBase%, %yBase%+0*%increment%					; click 1st field
	;Click, %xBase%, %yBase%+1*%increment%					; click 2nd field
	;Click, %xBase%, %yBase%+2*%increment%					; click 3rd field
	;Click, %xBase%, %yBase%+3*%increment%					; click 4th field
	Click, %xBase%, %yBase%+4*%increment%					; click 5th field
	;Click, %xBase%, %yBase%+5*%increment%					; click 6th field
	;Click, %xBase%, %yBase%+6*%increment%					; click 7th field
	;Click, %xBase%, %yBase%+7*%increment%					; click 8th field
	;Click, %xBase%, %yBase%+8*%increment%					; click 9th field
	;Click, %xBase%, %yBase%+9*%increment%					; click 10th field
	;Click, %xBase%, %yBase%+10*%increment%					; click 11th field
	;Click, %xBase%, %yBase%+11*%increment%					; click 12th field

		Sleep, short
	Send ^v
		Sleep, short
	;Click, 4565, 505						; next entry
	Click, 4385, 1895						; create new entry form
		Sleep, short
}
Return
However, I suspect I have done something wrong in the way I have written the click expressions because my system freaks out each time I run the script and, despite having included a ^Esc::ExitApp line, I have a hard time getting it stopped.

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.
ilhom
Posts: 52
Joined: 19 Aug 2019, 17:58

Re: Script freaks out when it runs

21 Aug 2019, 19:41

https://www.autohotkey.com/docs/commands/Click.htm
Since click does not support expressions, variables should be enclosed in percent signs.
https://www.autohotkey.com/docs/Variables.htm#Expressions
an expression takes one or more values as input, performs one or more operations, and produces a value as the result.

You would need to do something like this

Code: Select all

	yBase0 := ybase + 0 * increment
	yBase1 := ybase + 1 * increment
	yBase2 := ybase + 2 * increment
	.
	.
	.
	Click, %xBase%, %yBase0%
	Click, %xBase%, %yBase1%
	Click, %xBase%, %yBase2%
User avatar
Xtra
Posts: 1522
Joined: 02 Oct 2015, 12:15

Re: Script freaks out when it runs

21 Aug 2019, 22:08

You could use: MouseClick if you wanted to use expressions for the X / Y coords.
X, Y
The x/y coordinates to which the mouse cursor is moved prior to clicking, which can be expressions.

Code: Select all

MouseClick, Left, xBase, yBase + 4 * increment
swagfag
Posts: 3079
Joined: 11 Jan 2017, 17:59

Re: Script freaks out when it runs

21 Aug 2019, 22:27

u can use Click just as well
Click % x+123 "," y+123
WeThotUWasAToad
Posts: 260
Joined: 19 Nov 2013, 08:44

Re: Script freaks out when it runs

21 Aug 2019, 22:42

Thanks!

That is just what I need. It works great now.
click does not support expressions
The key was discovering that Click parameters do not support expressions.
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.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, blad4, ktack05, synnapt, xhoffzz and 265 guests