-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathOn.ahk
375 lines (340 loc) · 10.9 KB
/
On.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Execute (What you want) On (Element) changes.
Introduction:___________________________________________________________________
These functions are supposed to monitoring specific element until it's changed
then will go to specified label. advantages of using these functions are vario
us things can be done in easier way and shorter such as automation for Applica
tion installation and auto-healing or any other similar automation for games.
Also killing infinite monitoring loop can be done in one line. especially as i
t's abstraction for beginners it's intuitive and handy.
The biggest benefit is these functions are running asynchronously which means
it will not freeze the caller script's work line while monitoring.
Moreover plural monitoring processes can be running at the same time.
Installation:___________________________________________________________________
Put ON.ahk in your Standard Library (AhkPath\lib) folder. then you can call th
ese functions without #include
Example #1: Basic Usage (Automation of Application Installation)
////////////////////////////////Example begin///////////////////////////////////
WinTitle = Some Application Install Window
On_ControlList(Title, "AutoClick") ;if controllist changes go to Label
Return
AutoClick:
Loop, Parse, OnControlList, `n
{
ControlGetText, Output, %A_LoopField%, %WinTitle%
If Output in Next,Install,Confirm ;If Control(button) text is match.
ControlClick, %A_LoopField%, %WinTitle% ;auto click it
}
IfWinNotExist, %WinTitle%
SetTimer, OnControlList, Off
On_ControlList(Title, "AutoClick")
Return
;Note that %OnControlList% variable is passed from the function. some functions
;will have variable with same name (exclude _) so you can call it
/////////////////////////////////Example end////////////////////////////////////
Example #2: If you want to watch infinitely. make self recurrence.
////////////////////////////////Example begin///////////////////////////////////
On_ActiveWindow("Watch")
Return
Watch:
ToolTip, ActiveWindow has been changed.
On_ActiveWindow("Watch") ;recurrence
Return
/////////////////////////////////Example end////////////////////////////////////
Example #3: Killing an infinite monitoring function
////////////////////////////////Example begin///////////////////////////////////
On_ActiveWindow("Watch")
Return
Watch:
ToolTip, ActiveWindow has been changed.
On_ActiveWindow("Watch") ;recurrence
Return
ESC::SetTimer, OnActiveWindow, Off
;Every Functions have their own Timer with same name (exclude _)
/////////////////////////////////Example end////////////////////////////////////
Example #4: Simple Popup killer using On_WinOpen() with filter list
////////////////////////////////Example begin///////////////////////////////////
FilterList = /popup,/ads/,/sponsor/,/pagead/,/banner/,etc
On_WinOpen("ahk_class IEFrame", "Filter")
Return
Filter:
ControlGetText, PopupUrl, Edit1, ahk_class IEFrame ;check url
if PopupUrl Contains %Filterlist%
SendMessage,0x111,165,0,,ahk_class IEFrame ;close
Return
/////////////////////////////////Example end////////////////////////////////////
Example #5: Two liner Auto-Healing script for online games using On_Pixel()
////////////////////////////////Example begin///////////////////////////////////
On_Pixel("40", "40", "Heal") ;assuming lowest point of health-bar is x40 y40
Return
Heal:
Send {1} ;ingame quickbar for self-heal
On_Pixel("40", "40", "Heal")
Return
/////////////////////////////////Example end////////////////////////////////////
Usage:__________________________________________________________________________
On_ActiveWindow(Label, Interval=200)
On_ControlList(WinTitle, Label, TitleMatchMode=3, Interval=200)
On_File(Filename, Label, Interval=1000)
On_Pixel(X, Y, Label, Method="", Interval=200) ;Method = RGB|Slow|Alt
On_StatusBar(WinTitle, Label, Part=1, TitleMatchMode=3, Interval=200)
On_WinTitle(WinTitle, Label, TitleMatchMode=3, Interval=200)
On_WinPos(WinTitle, Label, TitleMatchMode=3, Interval=200)
On_WinSize(WinTitle, Label, TitleMatchMode=3, Interval=200)
On_WinOpen(WinTitle, Label, TitleMatchMode=3, DetectHidden=0, Interval=200)
On_WinClose(WinTitle, Label, TitleMatchMode=3, DetectHidden=0, Interval=200)
Return:_________________________________________________________________________
Return of Functions will be empty with successful execution. else if necessary
parameters is wrong (eg: non exist label) will return 1 rather than interrupt
the whole runtime
Remarks:________________________________________________________________________
Since these functions are all SetTimer based. when you meet unexpected circums
tances such as thread interruption, Check help pages below.
http://www.autohotkey.com/docs/commands/SetTimer.htm
http://www.autohotkey.com/docs/commands/Critical.htm
http://www.autohotkey.com/docs/commands/Thread.htm
eg) Thread, NoTimers
Pending:
On_Var
On_Reg
On_Memory
On_Process
On_Tab
On_CheckBox
- heresy
*/
Return
;-------------------------------------------------------------------------------
;On Active Window Changes
On_ActiveWindow(Label, Interval=200)
{
If isLabel(Label)
{
WinGetActiveTitle, ActiveWindow
Global OnActiveWindowSub := Label, OActiveWindow := ActiveWindow
SetTimer, OnActiveWindow, %Interval%
}
Else
Return 1
}
OnActiveWindow:
WinGetActiveTitle, OnActiveWindow
If OActiveWindow != %OnActiveWindow%
{
SetTimer, OnActiveWindow, Off
Gosub, %OnActiveWindowSub%
}
Return
;-------------------------------------------------------------------------------
;On ControlList of an window changes
On_ControlList(WinTitle, Label, TitleMatchMode=3, Interval=200)
{
SetTitleMatchMode, %TitleMatchMode%
If WinExist(WinTitle) && IsLabel(Label)
{
WinGet, O, ControlList, %WinTitle%
Global OControlListT := WinTitle, OnControlListSub := Label, OControlList := O
SetTimer, OnControlList, %Interval%
}
Else
Return 1
}
OnControlList:
WinGet, OnControlList, ControlList, %OControlListT%
If OControlList != %OnControlList%
{
SetTimer, OnControlList, Off
Gosub, %OnControlListSub%
}
Return
;-------------------------------------------------------------------------------
;On File Changes (File Delete/Move/Name/Size/Attrib/Time Changes)
On_File(Filename, Label, Interval=1000)
{
If FileExist(FileName) && IsLabel(Label)
{
FileGetSize, Size, %FileName%
FileGetAttrib, Attrib, %FileName%
FileGetTime, Time, %Filename%
Global OFile := FileName, OnFileSub := Label, OFileState:=Size Attrib Time
SetTimer, OnFile, %Interval%
}
Else
Return 1
}
OnFile:
If OFile
{
FileGetSize, NFileSize, %OFile%
FileGetAttrib, NFileAttrib, %OFile%
FileGetTime, NFileTime, %OFile%
OnFile := NFileSize NFileAttrib NFileTime
IF OFileState = %OnFile%
Return
Else
{
SetTimer, OnFile, Off
Gosub, %OnFileSub%
Return
}
}
Return
;-------------------------------------------------------------------------------
;On Pixel Color Changes
On_Pixel(X, Y, Label, Method="", Interval=200)
{
If (X>=0 && Y>=0 && IsLabel(Label))
{
Method := Method="" ? "RGB" : Method
CoordMode, Pixel, Screen
PixelGetColor, Color, %X%, %Y%, %Method%
Global OnPixelSub := Label, OPixel := Color, PixelX := X, PixelY := Y
SetTimer, OnPixel, %Interval%
}
Else
Return 1
}
OnPixel:
PixelGetColor, OnPixel, PixelX, PixelY
If OPixel != %OnPixel%
{
SetTimer, OnPixel, Off
Gosub, %OnPixelSub%
}
Return
;-------------------------------------------------------------------------------
;On Statusbar Text Changes
On_StatusBar(WinTitle, Label, Part=1, TitleMatchMode=3, Interval=200)
{
SetTitleMatchMode, %TitleMatchMode%
If WinExist(WinTitle) && IsLabel(Label)
{
StatusBarGetText, Output, %Part%, %WinTitle%
Global OStatusBarT := WinTitle, OnStatusBarSub := Label
,OStatusBarText := Output, OStatusBarPart := Part
SetTimer, OnStatusBar, %Interval%
}
Else
Return 1
}
OnStatusBar:
StatusBarGetText, OnStatusBar, %OStatusBarPart%, %OStatusBarT%
If OnStatusBar != %OStatusBarText%
{
SetTimer, OnStatusBar, Off
Gosub, %OnStatusBarSub%
}
Return
;-------------------------------------------------------------------------------
;On Window Title Changes
On_WinTitle(WinTitle, Label, TitleMatchMode=3, Interval=200)
{
SetTitleMatchMode, %TitleMatchMode%
If WinExist(WinTitle) && IsLabel(Label)
{
WinGetTitle, WinTitle, %WinTitle%
Global OWinTitleT := WinTitle, OnWinTitleSub := Label
SetTimer, OnWinTitle, %Interval%
}
Else
Return 1
}
OnWinTitle:
WinGetTitle, OnWinTitle, %OWinTitleT%
If OnWinTitle != %OWinTitleT%
{
SetTimer, OnWinTitle, Off
GoSub, %OnWinTitleSub%
}
Return
;-------------------------------------------------------------------------------
;On Window Position Changes
On_WinPos(WinTitle, Label, TitleMatchMode=3, Interval=200)
{
SetTitleMatchMode, %TitleMatchMode%
If WinExist(WinTitle) && IsLabel(Label)
{
WinGetPos,X,Y,,,%WinTitle%
Pos := X Y
Global OWinPosT := WinTitle, OnWinPosSub := Label, OPos := Pos
SetTimer, OnWinPos, %Interval%
}
Else
Return 1
}
OnWinPos:
WinGetPos,NX,NY,,,%OWinPosT%
OnWinPos := NX NY
If OPos != %OnWinPos%
{
SetTimer, OnWinPos, Off
Gosub, %OnWinPosSub%
}
Return
;-------------------------------------------------------------------------------
;On Window Size Changes
On_WinSize(WinTitle, Label, TitleMatchMode=3, Interval=200)
{
SetTitleMatchMode, %TitleMatchMode%
If WinExist(WinTitle) && IsLabel(Label)
{
WinGetPos,,,Width,Height,%WinTitle%
Size := Width Height
Global OWinSizeT := WinTitle, OnWinSizeSub := Label, OSize := Size
SetTimer, OnWinSize, %Interval%
}
Else
Return 1
}
OnWinSize:
WinGetPos,,,NWidth,NHeight,%OWinSizeT%
OnWinSize := NWidth NHeight
If OSize != %OnWinSize%
{
SetTimer, OnWinSize, Off
Gosub, %OnWinSizeSub%
}
Return
;-------------------------------------------------------------------------------
;On Window Open (actually it's WinExist but still applicable. see Example #4)
On_WinOpen(WinTitle, Label, TitleMatchMode=3, DetectHidden=0, Interval=200)
{
Switch := DetectHidden=0 ? "Off" : "On"
DetectHiddenWindows, %Switch%
SetTitleMatchMode, %TitleMatchMode%
If IsLabel(Label)
{
Global OWinOpenT := WinTitle, OnWinOpenSub := Label
SetTimer, OnWinOpen, %Interval%
}
Else
Return 1
}
OnWinOpen:
IfWinExist, %OWinOpenT%
{
SetTimer, OnWinOpen, Off
Gosub, %OnWinOpenSub%
}
Return
;-------------------------------------------------------------------------------
;On Window Close
On_WinClose(WinTitle, Label, TitleMatchMode=3, DetectHidden=0, Interval=200)
{
Switch := DetectHidden=0 ? "Off" : "On"
DetectHiddenWindows, %Switch%
SetTitleMatchMode, %TitleMatchMode%
If WinExist(WinTitle) && IsLabel(Label)
{
Global OWinClose := WinTitle, OnWinCloseSub := Label
SetTimer, OnWinClose, %Interval%
}
Else
Return 1
}
OnWinClose:
IfWinNotExist, %OWinClose%
{
SetTimer, OnWinClose, Off
GoSub, %OnWinCloseSub%
}
Return