-
Notifications
You must be signed in to change notification settings - Fork 71
/
VariemClick.ahk
148 lines (145 loc) · 6.03 KB
/
VariemClick.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;================================================================================================
;
; Function: VariemClick
; Description: Finds and Clicks multiple instances of a single image on screen and
; if no image is found it increases the allowed variation for the next search
;
; The function Also returns a Sting with ether a error message or
; a list of X,Y coord clicked and the variation used
;
; Online Ref.: http://www.autohotkey.com/forum
;
; Last Update: 19/4/2012 12:00
;
; Created by: Blackholyman
; http://www.autohotkey.com/community/memberlist.php?mode=viewprofile&u=55126
;
; Thanks to: All the Ahk forum members that post code examples for me to learn from
;
;================================================================================================
;
; VariemClick(ImageFile [, NumberOfTries, Start_Vari, Max_Vari, Click_X_Offset, Click_Y_Offset, WaitBetweenClicks]
;
;================================================================================================
;
; * The parameters for this function are:
;
; + Required parameters:
;
; - ImageFile The full file path must be enclosed in "" double quotes for ImageSearch to use
;
; + Optional parameters:
;
; - NumberOfTries Number Of times to find `n click or increse the variation (default is 10)
; - Start_Vari Allowed shades of variation to start with (default is 0)
; - Max_Vari Maximum shades of variation to be used (default is 80)
; - Click_X_Offset Offset from the X coordinate to use (default is 0)
; - Click_Y_Offset Offset from the Y coordinate to use (default is 0)
; - WaitBetweenClicks Time to Wait Between Clicks (default is 1/10 of a second)
;
;================================================================================================
VariemClick(ImageFile, NumberOfTries=10, Start_Vari=0, Max_Vari=80, Click_X_Offset=0, Click_Y_Offset=0, WaitBetweenClicks=100) {
StartTime := A_TickCount ;Store the time of start
BatchSave := A_BatchLines ;Store BatchLines current setting
MouseDelay := A_MouseDelay ;Store MouseDelays current setting
SetBatchLines -1 ;Set the between lines sleep to -1
SetMouseDelay -1 ;set MouseDelay to -1
CoordMode, ToolTip ;Set the ToolTips to use screen coordinates
Loop % NumberOfTries
{
ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *%Start_Vari% %ImageFile%
if ErrorLevel = 2
{
Clicks := "ErrorLevel = 2 ""Could not conduct the search."""
SetBatchLines %BatchSave% ;Set between lines sleep back to the call scripts setting
SetMouseDelay %MouseDelay% ;set MouseDelay back to the call scripts setting
return %Clicks%
}
if ErrorLevel = 1
{
ToolTip, Image with variation %Start_Vari% on try %A_index% was not found., 10, 10, 2
Sleep, 10
Start_Vari += 5 ;Increse the variation
if (Start_Vari >= Max_Vari)
{
ToolTip,,,,2 ;Have ToolTip 2 disappear
Clicks := "The image could not be found with under the max(" Max_Vari ") shades of variation."
SetBatchLines %BatchSave% ;Set between lines sleep back to the call scripts setting
SetMouseDelay %MouseDelay% ;set MouseDelay back to the call scripts setting
Return %Clicks%
}
else
continue
}
else
{
Vari_inc := Max_Vari - Start_Vari
Vari_inc //= NumberOfTries
Vari := Start_Vari ;Store the variation
MouseMove, %X%,%Y% ;Move the mouse cursor to the found image
MouseGetPos, , , id ;Get the Window id under the cursor
WinGetClass, class, ahk_id %id% ;Get the Window class
WinActivate,ahk_class %class% ;Activate the Window with that class
break
}
}
while (Vari_Tries <> NumberOfTries) and (ClickNumb <> NumberOfTries)
{
StartX := NewX ;Set the Searchs Starting X coordinat
StartY := NewY ;Set the Searchs Starting Y coordinat
ImageSearch, FoundX, FoundY, %StartX%, %StartY%, A_ScreenWidth, A_ScreenHeight, *%Vari% %ImageFile%
if ErrorLevel = 1
{
ToolTip, No more Images was found with a variation of %Vari% so variation increased., 10, 10, 2
NewX := 0 ;Set the next Searchs x coord to 0
NewY := ClickedY+1 ;Set the next Searchs Y coord to the last clicked Y coord
Error++ ;Increse the internal Error setting
If (Error = 2)
{
NewX := 0 ;Set the next Searchs x coord to 0
NewY := 0 ;Set the next Searchs Y coord to 0
If (Vari > Max_Vari) ;If at max variation
{
VariUpOrDown = Down ;Set value to down
Max_Vari_toggle = 1 ;Toggle that max variation has been reached
}
If (Vari < Start_Vari) ;If at under start variation
VariUpOrDown = Up ;Set value to up
If (VariUpOrDown = Up)
Vari+=Vari_inc
Else
Vari-=Vari_inc
Vari_Tries++ ;Increse the count of variation tried
Error-- ;Decrese the internal Error setting
}
Sleep, 10
continue
}
else
{
Off_X := FoundX+Click_X_Offset ;Add the X Offset to the found X coordinate
Off_Y := FoundY+Click_Y_Offset ;Add the Y Offset to the found Y coordinate
NewX := Foundx+1 ;Set and Increse the X coordinate
Clickedx := FoundX ;Store the last Found X coordinate
ClickedY := FoundY ;Store the last Found Y coordinate
WinActivate,ahk_id %id% ;Activate the Window with that id
Mousemove, %Off_X%, %Off_Y% ;Move the mouse to the Offset X,y coordinates
Sleep, 10
SendInput {Click}
Sleep, 10
Mousemove, 0, 0 ;Move the mouse away
ClickNumb++ ;Increse the click count
Error := 0 ;The internal Error setting is set to 0
Clicks .= "The image was found and tried clicked at " . FoundX . "x" . FoundY . " with a variation of " . Vari . ".`n"
Sleep, %WaitBetweenClicks%
continue
}
}
ToolTip ;Have ToolTip 1 disappear
ToolTip,,,,2 ;Have ToolTip 2 disappear
SetBatchLines %BatchSave% ;Set between lines sleep back to the call scripts setting
SetMouseDelay %MouseDelay% ;set MouseDelay back to the call scripts setting
ElapsedTime := A_TickCount - StartTime ;Calculate and store the elapsed time
Clicks .= "The Number of clicks was (" . ClickNumb . ") and it took (" . (ElapsedTime //= 1000) . ") seconds."
Return %Clicks% ;Return the list of coordinates clicked and variation used
}