Laurens Ruben
Laurens Ruben

Reputation: 109

How to stop script as soon as active window is no longer the desired window

I'm scripting some pre-determined Clicks and Sends at a specific window.
Sometimes a click will not end up at the desired location, it may click on a different window and now all subsequent inputs from my script will be sent to the wrong window/location.

Here is the executing part of the code which I can't seem to make 100% consistent.

    CoordMode, Mouse, Window
    SetTitleMatchMode, 2

    <!p::
    Click, 60, 270
    Send, %dp%{Tab}{Tab}{Tab}{Tab}%e1%{Tab}^a           ; Plate thickness and layout
    Send, %PLh%                                         ; Plate Height
    Click, 60, 130
    Sleep 100
    Click, 263, 45
    Sleep 20
    Click, 263, 45
    Sleep 20
    Click, 370, 48
    Sleep 100
    Click, %Mx%, %My%                                   ; Bolt Diameters (M16: 40, 135) (M20: 40, 150) (M24: 40, 170) (M27: 40, 190) (M30: 40, 210)
    Sleep 50
    Click, 60, 160
    Send, {Tab}{Tab}{Tab}%w1%{Tab}{Tab}^a               ; Hor Bolt center distance
    Sleep 50
    Send, 0{Tab}^a                                      ; Edge distance
    Send, 0                                             ; Edge distance
    Sleep 100
    Click, 60, 190
    Sleep 100
    Send, {Tab}{Tab}%e3%                                ; Vert interm distance
    Send, {Tab}{Tab}{Tab}{Tab}{Tab}%e2%                 ; vert outside bolt dist
    Click, 60, 295
    Sleep 100
    Send, {Tab}%Stiff%{Tab}{Tab}{Tab}%Stiff%            ; Stiffener thickness
    Sleep 100
    Click, 60, 320
    Sleep 100
    Send, %Weld%{Tab}{Tab}{Tab}%Weld%{Tab}{Tab}%Weld%{Tab}{Tab}%Weld%{Tab}{Tab}%Weld%               ; Weld thickness
    Return

If any of the clicks in this section of code result in activating an undesired window then I would like my script to stop and give a warning.
I could insert an If Winactive statement for every click, but that feels very inelegant to me, perhaps there is a better solution?

(I could also ask how to make this script more consistent which I've already tried to do by inserting Sleep commands, but that's a different question. The main culprit seems to be clicking drop-down boxes, but it's tough to determine that since I can't debug AHK code by running line-by-line)

Thanks in advance

Upvotes: 0

Views: 107

Answers (1)

Laurens Ruben
Laurens Ruben

Reputation: 109

So kindly enough Yane pointed me in the direction of "Controlsend" and "Controlclick" which covers the problem I was having.

Instead of using, for example:

Click, 60, 300  

One can force a click in a specific window like this:

ControlClick, x60 y300, MyDesiredWindow'sTitle

Furthermore, to fix my specific case of a Click on a dropbox throwing off the rest of the script, the following can be used:

ControlClick, ComboBox1, MyDesiredWindow'sTitle

This will Click a specific control in a specific window. (use Window Spy to identify windows and controls)

I guess my question is largely irrelevant when properly making use of ControlSend and other AHK commands and practices.

It seems to me that it's possible to directly answer the question in the title and, although my answer doesn't really do that, I will accept my own answer when it becomes possible for me to do so because it did solve my problem.

Upvotes: 1

Related Questions