Clicking a specific button

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gazmoz17
Posts: 72
Joined: 14 Mar 2018, 09:37

Re: Clicking a specific button

12 Nov 2018, 03:30

Any ideas guys?
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Clicking a specific button

12 Nov 2018, 04:54

One last attempt. Do you see your button in the list?

Code: Select all

#NoEnv
WinTitle := "Sage 50 Accounts Professional - GRP(UK)LTD" ; the window's title
WinGet, CtrlList, ControlListHwnd, %WinTitle%
Gui, Add, ListView, w600 r10, Class|Text
Loop, Parse, CtrlList, `n
{
   HWND := A_LoopField
   WinGetClass, WinClass, ahk_id %HWND%
   If !InStr(WinClass, "Button") ; remove this line and the following if your button is not shown in the ListView
      Continue
   WinGetText, WinText, ahk_id %HWND%
   LV_Add("", WinClass, WinText)
}
LV_ModifyCol()
FoundControls := LV_GetCount()
Gui, Show, , Found %FoundControls% Controls
Return
GuiClose:
ExitApp
gazmoz17
Posts: 72
Joined: 14 Mar 2018, 09:37

Re: Clicking a specific button

12 Nov 2018, 05:22

Hi I get a window found 184 controls?

Class & Text headings accross the top. Blank under text heading Under class:

#SG32DTBUTTON; loads of that same entry going down but no button number on the end of it.

Thanks
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Clicking a specific button

12 Nov 2018, 11:48

ControlClickByControlText(),written for such an occasion...
live ? long & prosper : regards
gazmoz17
Posts: 72
Joined: 14 Mar 2018, 09:37

Re: Clicking a specific button

13 Nov 2018, 03:47

Thanks guys.

Cylon I'll have a look at this in the afternoon if I have time at work.

Many Thanks
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Clicking a specific button

13 Nov 2018, 06:04

Sorry, I made a silly mistake using WinGetText instead of ControlGetText. :facepalm:

This might have a better chance to find your button:

Code: Select all

#NoEnv
WinTitle := "Sage 50 Accounts Professional - GRP(UK)LTD"    ; the window's title
BtnClass := "#SG32DTBUTTON"                                 ; the fix leading part (the class name) of the ClassNN
BtnText  := "EMail"                                         ; the button's caption
BtnFound := False
WinGet, CtrlList, ControlListHwnd, %WinTitle%
; MsgBox, 0, Found controls, %CtrlList% ; <<<<< added
Loop, Parse, CtrlList, `n
{
   HWND := A_LoopField
   WinGetClass, CtlClass, ahk_id %HWND%
   If (CtlClass <> BtnClass)
      Continue
   ControlGetText, CtlText, , ahk_id %HWND%
   If (CtlText <> BtnText)
      Continue
   BtnFound := True
   ControlClick, , ahk_id %HWND%
   Break
}
If (BtnFound)
   MsgBox, 0, Found, Found and tried to click the button!
Else
   MsgBox, 16, Error, Did not find the button!
ExitApp
gazmoz17
Posts: 72
Joined: 14 Mar 2018, 09:37

Re: Clicking a specific button

14 Nov 2018, 07:02

Hi justme,

Aprreciate you adjustsing that.

Get a window 'Found and tried to click the button!' ?

Whats the next step please?

Thanks
gazmoz17
Posts: 72
Joined: 14 Mar 2018, 09:37

Re: Clicking a specific button

14 Nov 2018, 07:03

Hi Cylon,

I dont pretend to understand all that ha.

But when I have more than 5 mins I'll have a look

Many thanks
garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: Clicking a specific button

14 Nov 2018, 08:15

I must learn these controls ... just a short example to auto click button 'TEST2'
( at the moment works , but Button1 must have variable ending with '1' ( or example BT01 ... ) must new rewrite .... )
take a look also to CyL0N 's function ....

Code: Select all

#warn
#NoEnv
setworkingdir,%a_scriptdir%
WinTitle := "HWND_TEST"
Gui,2:add,button, x20 y20  h30 w200 gA0 vBT01,TEST1
Gui,2:add,button, x20 y60  h30 w200 gA0 vBT02,TEST2
Gui,2:add,button, x20 y100 h30 w200 gA0 vBT03,TEST3

Gui,2:show,x10 y10 h150 w400,%wintitle%
gosub,get
return
;------------------------------------
2Guiclose:
exitapp
;----------
get:
Gui,2:submit,nohide
BtnText  := "TEST2"     ;- Click this button
BtnFound := False
WinGet, CtrlList, ControlListHwnd, %WinTitle%
Loop, Parse, CtrlList, `n
{
HWND := A_LoopField
ControlGet     ,btn%a_index%  ,Hwnd ,,Button%a_index%,%wintitle%
aa:=btn%a_index%
ControlGetText ,btx%a_index% ,,ahk_id %aa%
ac:=btx%a_index%
   if (ac<>btntext)
      continue
   BtnFound := True
   sleep,1000
   ControlClick, , ahk_id %HWND%
   Break
}
return
;----------------------------


a0:
Gui,2:submit,nohide
r:=a_guicontrol
stringlen,L1,r
L1:=(L1-1)
stringmid,p,r,L1,2
p := LTrim(p, "0")    ;- remove leading zero
ControlGet     ,btn%p%  ,Hwnd ,,Button%p%,%wintitle%
aa:=btn%p%
ControlGetText ,btx%p% ,,ahk_id %aa%
af:= btx%p%
msgbox, 262208, ,clicked to`nButton Variable= %r%`nButtonName=%af%,
return
;===================== END script =====
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Clicking a specific button

14 Nov 2018, 11:16

The topmost example is to click on button from garry's example above,where as the hotkey's are to ControlClick on your pesky button, as per your description of: ButtonText,ButtonClassNN,WinTitle.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;-->EXAMPLE To Click on button's from Garry's example above...
;BOTH ARE VALID, the one on top is however more accurate in identifying target buttons...
ControlClickByControlText("TEST1", "Button1", "HWND_TEST")	;click on button with specified name,class NN & window title of gui with button
ControlClickByControlText("TEST1")	;click on any button with button text 'TEST1'

;====================================================================================
;Usage: ControlClickByControlText(buttonText, buttonControlClassNN, winTitle)
;buttonControlClassNN & winTitle ARE OPTIONAL!
;====================================================================================

;-->One of the following below is certain to work,unless it's especially unique GUI,in which case you can't use ControlClick...
F9::ControlClickByControlText("EMail", "#SG32DTBUTTON", "Sage 50 Accounts Professional - GRP(UK)LTD")
F10::ControlClickByControlText("EMail", "#SG32DTBUTTON")
F11::ControlClickByControlText("EMail",, "Sage 50 Accounts Professional - GRP(UK)LTD")
F12::ControlClickByControlText("EMail")


;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
	}
}
live ? long & prosper : regards
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Clicking a specific button

14 Nov 2018, 11:36

gazmoz17 wrote:Get a window 'Found and tried to click the button!' ?
Whats the next step please?
Well, we can put the code into a small function which can be called whereever you need it:

Code: Select all

ClickSageMailButton() {
   WinTitle := "Sage 50 Accounts Professional - GRP(UK)LTD"    ; the window's title
   BtnClass := "#SG32DTBUTTON"                                 ; the fix leading part (the class name) of the ClassNN
   BtnText  := "EMail"                                         ; the button's caption
   WinGet, CtrlList, ControlListHwnd, %WinTitle%
   Loop, Parse, CtrlList, `n
   {
      HWND := A_LoopField
      WinGetClass, CtlClass, ahk_id %HWND%
      If (CtlClass = BtnClass) {
         ControlGetText, CtlText, , ahk_id %HWND%
         If (CtlText == BtnText) {
            ControlClick, , ahk_id %HWND%
            Return True
         }
      }
   }
   Return False
}
If the function finds the button it tries to click it and returns True (1), otherwise False (0). You might want to try whether it does what you want.
garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: Clicking a specific button

15 Nov 2018, 12:07

@CyL0N & just me , thank you for scripts / function / examples

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: robforAHK2 and 119 guests