1
votes

I am writing an AutoHotkey script which interacts with a Java-based web application. At one point, I want my script to pause and wait until it finds control SunAwtCanvas1 before continuing to run.

I know that you can use IfWinExist to check whether a window exists, but I don't know if a similar command exists for buttons or control fields.

How can I check if a control field exists in AutoHotkey?

1

1 Answers

1
votes

Option 1: WinExist title, control_name

when inspecting with AU3_Spy.exe, you may find control name in text. In that case, just

IfWinExistwintitle, SunAwtCanvas1

will do.

Though I like functional syntax more, particularly because because Egyptian brackets available with it:

If (WinExist(wintitle, "SunAwtCanvas1")) {
    // …
}

Option 2: ControlGet

If control name is not in text, or if it is in but may be invisible, and you want to know if it's visible, ControlGet can read some properties of the control.

If the control does not exist, ErrorLevel is set to 1 and OutputVar is made blank. Also, it will throw an exception inside Try {} block.

Also, some controls may be pre-created but invisible. So, to check if control is displayed:

ControlGet ctrlVisible, Visible,, SunAwtCanvas1
If (ctrlVisible) { // like IfControlVisible
    // …
}

AFAIK there's no WinWait-like operators for controls, but simple loop will do:

// add IfWinExist or WinWait somewhere before to fill in LastWindowFound

Loop
{
    ControlGet ctrlVisible, Visible,, SunAwtCanvas1 
    Sleep 100 ; to avoid high CPU load, and to allow app finish its operations in case control is found in middle of something (that's why it's after ControlGet, not before)
} Until ctrlVisible

In this example, ControlGet will wait for a control to appear in LastWindowFound. Or you can specify wintitle/wintext/excludetitle/excludetext right in ControlGet as parameters.