AutoHotkey – How to Restore or Activate a Minimized Window

autohotkeywindows 10

I have created the following script in an attempt to have one hotkey that launches the calculator, or, if it is already open, 'activates' the window, or, if it is already activated, closes the calculator. It all works fine accept that if the calculator is minimized it fails to restore it. As far as I can tell I have done everything right. I don't get it what the problem is. The AHK documentation claims that you if you call WinActivate on a minimized window it will first restore that window, but that is a lie. If I uncomment the MsgBox line I still get a message when the window is minimized, but it is powerless to do anything more.

If WinExist("Calculator") {
;MsgBox Calculator Exists.
IfWinActive
    WinKill
Else
    WinGet, winState, MinMax
    If (winState = -1)
        WinRestore, Calculator
    WinActivate, Calculator
}
Else {
    run calc
    WinActivate, Calculator
}

Best Answer

Which OS are you on? Your code works for me on Win10 if I add the ahk_class to the title:

If WinExist("Calculator ahk_class ApplicationFrameWindow") 
{
    ;MsgBox Calculator Exists.
    IfWinActive
        WinClose
    Else
    {
        WinGet, winState, MinMax
        If (winState = -1)
        {
            WinRestore
            WinActivate
        }
    }
}
Else 
{
    run calc
    WinWait, Calculator
    WinActivate
}
Related Question