0
votes

I write a script to test selecting pop-up windows.

SetTitleMatchMode, 2

winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"
IfWinExist,%winTitle%
{
    WinActivate
    send !{F4}
}

IfWinExist,%popWin%
{
    WinActivate
    WinWaitActive, %popWin%
    WinGetClass, outputvar, %popWin%
    MsgBox %outputvar%
}

This script is intended to send ALT-F4 to close an opened R window, and when the confirmation pop-up window occurs, display the pop-up window's class name.

The first if block works fine. However, the send if block sometimes works, sometimes not. Active window info shows the pop-up windows' class info is:

Window Title, Class and Process

Question
ahk_class #32770
ahk_exe Rgui.exe

snapshot of the above info

I don't know why IfWinExist,%popWin% does not work. I tried changing popWin:="ahk_class #32770 ahk_exe Rgui.exe" to popWin:="ahk_class #32770", still it sometimes works, and sometimes not. So what should I do to select the pop-up windows correctly?

1

1 Answers

2
votes

I have changed your AutoHotkey code, such that it should give you the functionality you require.

SetTitleMatchMode, 2

winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"

if (hWnd := WinExist(winTitle)) ;this assigns hWnd, it does not compare hWnd with WinExist(winTitle)
{
    ;WinActivate, ahk_id %hWnd%
    ;send !{F4}
    WinClose, ahk_id %hWnd% ;WinClose is more direct than Alt+F4 if it works (Send can potentially send key presses to the wrong window if a new window suddenly appears)
}

WinWait, %popWin%, , 5 ;wait for window to exist, give up after 5 seconds
if !ErrorLevel ;if window found within 5 seconds
{
    WinGet, hWnd, ID, %popWin%
    WinActivate, ahk_id %hWnd%
    WinGetClass, outputvar, ahk_id %hWnd%
    MsgBox %outputvar%
}

Note: In most cases WinActivate requires a window title/hWnd be specified.

The second part of your code works sometimes but not other times, probably because if the popup appears very quickly, then IfWinExist will find the window, but if the popup appears slowly, then the IfWinExist check will occur before the window exists, and will thus not find the window.