AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

a Library for Beginners : Do (Something) On (Element) Change

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
heresy



Joined: 11 Mar 2008
Posts: 237

PostPosted: Tue Apr 29, 2008 8:09 pm    Post subject: a Library for Beginners : Do (Something) On (Element) Change Reply with quote

This idea originally began from my post at wishlist

I don't know if i can call it as a library. if so it's my first library after learning autohotkey for 2 months.
however it's just an abstraction of typical autohotkey actions for beginners including me.
what i felt for past 2 months as a beginner is need more intuitive way on various loop task.
take a look on Ask for Help forum.
there are lots of same questions asking 'how can i make it (do something) on (something changed)?'
so here it is.

these functions are ideal for Beginners who are trying to make typical automation
These functions are working in same way.
1) watch for something until it's changed
2) if changed. go to specified label

check the source there's more explanations
since it's supposed to help beginners i tried to comment it detailedly as Chris did.
but feel free to ask if you have additional question.
hope this helps

as it's based on small idea. Supplements can be done easily.
so Improvements, Supplements are welcome.
please feel free to add more.


Download On.ahk



Function list
Code:

  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)


Examples
Code:
Example #1: Basic Usage (Automation of Application Installation)
////////////////////////////////Example begin///////////////////////////////////
  WinTitle = Some Application Install Window
  On_ControlList(WinTitle, "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
  }
  On_ControlList(Title, "AutoClick")
  IfWinNotExist, %WinTitle%
    SetTimer, OnControlList, Off
  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////////////////////////////////////



Entire Source
Code:

/* 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
  }
  On_ControlList(Title, "AutoClick")
  IfWinNotExist, %WinTitle%
    SetTimer, OnControlList, Off
  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

_________________
Easy WinAPI - Dive into Windows API World
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group