ControlClickByControlText()

Post your working scripts, libraries and tools
CyL0N
Posts: 106
Joined: 27 Sep 2018, 09:58

ControlClickByControlText()

16 Oct 2018, 01:01

EDIT: I Had posted an older version...

Function searches & clicks on any existing control given ControlText AND/OR Title AND/OR ControlName, i use it to automate installations...

Code: Select all

;Example 1:
;ControlClickByControlText("&Kill",,"CodeQuickTester", 3)	;if running in codequicktester it'll press the kill button & so ends here,3 passes as it sometimes doesn't find the button in one pass...


;Example 2:
SetTimer, autoOk, 500
MsgBox
MsgBox, 0x2, %A_ScriptName%, Text		;this message box contains Abort,Retry,Cancel but since the buttons are clicked in the order the autoOk label won't click on Retry...
IfMsgBox, Abort
	MsgBox Aborted
MsgBox, 0x5, %A_ScriptName%, Text
IfMsgBox, Retry
	MsgBox Retry
ExitApp

autoOk:
;if all three buttons were present on the same gui,the button that is pressed is the button that is specified topmost below...
ControlClickByControlText("OK")
ControlClickByControlText("&Abort")
ControlClickByControlText("&Retry")
Return

;NOTE: if any text in control is underlined that letter must be prefixed by '&' as in the first example above...
ControlClickByControlText(searchStr, controlName:="", winTitle:="",passes:=1){	;loops through all visible windows and clicks on any window with a control that contains a matching string.}
	SetBatchLines("fastest")
	Loop % passes{	;the more passes the more reliable it is,though rarely necessary to go beyond 2
		WinGet, id, list
		Loop, %id%{
			this_id := id%A_Index%
			WinGet, thisControlList, ControlList, ahk_id %this_id%
			WinGetTitle, thisTitle, ahk_id %this_id%
			If thisControlList{
				Loop, Parse, thisControlList, `n
				{
					ControlGetText, thisControlText, %A_LoopField%, ahk_id %this_id%
					If !winTitle
						(thisControlText = searchStr AND !controlName AND WinExist("ahk_id " . this_id) ? ControlClick(A_LoopField, "ahk_id " this_id) : (thisControlText = searchStr AND controlName = A_LoopField AND WinExist("ahk_id " . this_id) ? ControlClick(A_LoopField, "ahk_id " this_id) : "" ))
					Else
						( thisControlText = searchStr AND !controlName AND winTitle = thisTitle AND WinExist("ahk_id " . this_id) ? ControlClick(A_LoopField, "ahk_id " this_id) : (thisControlText = searchStr AND controlName = A_LoopField AND winTitle = thisTitle AND WinExist("ahk_id " . this_id) ? ControlClick(A_LoopField, "ahk_id " this_id) : "" ) )
				}
			}
		}
	}
	SetBatchLines("restore")
}

ControlClick(ControlOrPos:="", WinTitle:="", WinText:="", WhichButton:="", ClickCount:="", Options:="", ExcludeTitle:="", ExcludeText:=""){
	ControlClick, % ControlOrPos, % WinTitle, % WinText, % WhichButton, % ClickCount, % Options, % ExcludeTitle, % ExcludeText
}

ControlGetText(Control:="", WinTitle:="", WinText:="", ExcludeTitle:="", ExcludeText:=""){
	ControlGetText, controlText, % Control, % WinTitle, % WinText, % ExcludeTitle, % ExcludeText
	Return controlText
}

SetBatchLines(doWhat){
	Static
	If (doWhat = "restore"){
		SetBatchLines, % OB
		OB := ""
	}Else If (doWhat = "fastest"){
		OB := !OB || A_BatchLines <> -1 ? A_BatchLines : OB
		SetBatchLines, -1
	}
}
burque505
Posts: 626
Joined: 22 Jan 2017, 19:37

Re: ControlClickByControlText()

18 Oct 2018, 13:27

Thanks, CyLON, that's a nice script. It'll come in handy.
Regards,
burque505

Return to “Scripts and Functions”

Who is online

Users browsing this forum: cocobanana and 44 guests