Help with simple button-click script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Autorquey
Posts: 4
Joined: 08 Feb 2023, 17:45

Help with simple button-click script

Post by Autorquey » 08 Feb 2023, 18:03

I have the scanner software "VueScan". Its main window has a button marked "Scan". With VueScan running, I want AHK to activate its window and click the Scan button.

AU3_Spy (Active Window Information) shows the correct VueScan window title and that the button has Text = Scan and ClassNN = Button2

My script activates the VueScan window but fails to click the Scan button:

Code: Select all

^!x::   ;hotkey to activate
WinActivate VueScan  ;this works to activate the window
ControlClick Button2     ;this fails to click the Scan button
ControlClick "Button2"     ;this fails to click the Scan button
ControlClick "", "Button2"     ;this fails to click the Scan button
ControlClick "Scan"     ;this fails to click the Scan button
return
I also tried many variations using "ControlSend" without success.

For info: the app AutoIt is able to activate the window and press the Scan button with these commands:

Code: Select all

WinActivate("VueScan", "")
ControlClick("VueScan", "", "[CLASS:Button; INSTANCE:2]")

But I want to use AHK. What else to try?

User avatar
boiler
Posts: 17047
Joined: 21 Dec 2014, 02:44

Re: Help with simple button-click script

Post by boiler » 08 Feb 2023, 18:19

You haven't specified a WinTitle for your ControlClick commands. If you're counting on the last found window, WinActivate isn't one of the commands or functions that captures the last found window.

Autorquey
Posts: 4
Joined: 08 Feb 2023, 17:45

Re: Help with simple button-click script

Post by Autorquey » 08 Feb 2023, 19:52

Thanks; in the Help syntax it looked like win titles was optional, and I missed that subtlety.

I finally got it working, despite my bafflement as to what should or shouldn't be quoted... So for the benefit of anyone else:

Code: Select all

ControlClick Button2, VueScan     ;this WORKS to click the Scan button
These do not work (note that pressing Enter would be almost as good as Scan):

Code: Select all

ControlClick "Scan", "VueScan"     ;this fails to click the Scan button
ControlClick "Scan", VueScan     ;this fails to click the Scan button
ControlClick Button2, "VueScan"     ;this fails to click the Scan button
ControlClick "Button2", "VueScan"     ;this fails to click the Scan button
ControlSend "{Enter}", "Button2"    ;this fails to click the Scan button
ControlSend "{Enter}", "Scan"    ;this fails to click the Scan button
ControlSend "{Enter}", Button2    ;this fails to click the Scan button
ControlSend "{Enter}", Button2, "VueScan"    ;this fails to click the Scan button

User avatar
boiler
Posts: 17047
Joined: 21 Dec 2014, 02:44

Re: Help with simple button-click script

Post by boiler » 08 Feb 2023, 20:18

Autorquey wrote: Thanks; in the Help syntax it looked like win titles was optional, and I missed that subtlety.
It is optional. You just didn’t use a command or function beforehand that would have captured the last found window. For example, this would work:

Code: Select all

WinWait, VueScan
ControlClick, Button2

Autorquey wrote: These do not work…
Quoted strings are used in expressions, not legacy (command) syntax. You can avoid having to remember when to use each by switching to AHK v2, which uses expressions throughout.

Autorquey
Posts: 4
Joined: 08 Feb 2023, 17:45

Re: Help with simple button-click script

Post by Autorquey » 09 Feb 2023, 15:04

Thanks for the actual best way to do it. In fact

Code: Select all

ControlClick Button2, VueScan
isn't actually working and maybe never did; can't think why.

But following your code works:

Code: Select all

WinActivate VueScan ;optional just to see scan results live
WinWait, VueScan
ControlClick, Button2
I only occasionally use each of a half-dozen different app scripting features... so I can't remember the quoting practice for any one of them.

User avatar
boiler
Posts: 17047
Joined: 21 Dec 2014, 02:44

Re: Help with simple button-click script

Post by boiler » 09 Feb 2023, 16:30

What you might have found out is that VueScan won't accept virtual clicks unless the window is active, which is the case with some windows. Have you gotten it to actually click that button when the window is not active?

Autorquey
Posts: 4
Joined: 08 Feb 2023, 17:45

Re: Help with simple button-click script

Post by Autorquey » 12 Feb 2023, 22:22

There were too many variables for me to be sure that ControlClick would work with no WinActivate anywhere prior.

What I thought was working (my prev post) would occasionally fail; but I knew the command syntax was basically correct. So I beefed up that syntax with the "NA" option (seemed to help) and deactivating VueScan then reactivating it (seemed to help) and some Sleep periods (seemed to help); but still not solid. Finally from some visual clues I decided to send the ControlClick command several times in a row. THAT has now been 100% solid for dozens of uses over several days, with both a hardwired keyboard and a wireless.

It looks ad-hoc and messy - for some reason - but here it is for anyone else to get hints from:

Code: Select all

!`::   ;hotkey Alt-`				;Hotkey to launch the script
WinActivate VueScan					;May not be req'd but I want to see the scan happen
sleep, 100							;Might help, not sure
WinActivate, ahk_class Shell_TrayWnd	;Step away from VueScan but it's still visible, seemed to help
sleep, 200							;Deep breath might help, not sure
WinWait, VueScan					;Get VueScan ready for control input
ControlClick, Button2, VueScan,,,, NA	;Click the Scan button (#2 per Window Spy)
ControlClick, Button2, VueScan,,,, NA	;Do it three times, dammit
ControlClick, Button2, VueScan,,,, NA
sleep, 100							;Might help, not sure
WinActivate, ahk_class Shell_TrayWnd	;Step away from VueScan, might help before next use, not sure
return

Post Reply

Return to “Ask for Help (v1)”