3

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
}
1

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
}
1
  • Thanks! I don't know what the point of that ahk_class thing is, but it seems to do the trick. Only one problem though. You put the WinRestore and WinActivate together in the same if statement in the middle of the script which made it so that the calculator would only be activated if it was minimized. I fixed that and it works perfectly! – Moss Sep 27 '17 at 23:52
1

This is what I got to work for me.

;------hotkey to open/close explorer------
^LWin:: ;control and leftwindows
if WinExist("ahk_class ActualTools_TabbedExplorerContainerWindow"){ ; if the window exists
    if WinActive("ahk_class ActualTools_TabbedExplorerContainerWindow") or WinActive("ahk_exe Explorer.EXE")
        WinMinimize, ahk_class ActualTools_TabbedExplorerContainerWindow 
    else{
        WinActivate ; otherwise make it the active window
    }
}else
    run, explorer.exe ;otherwise not open, open explorer
return

I use to use:

;------hotkey to open/close explorer------
^LWin:: ;control and leftwindows
;WinGetClass, Clipboard, A ;Use this to get the name(class?) of the window you want the script to open. https://stackoverflow.com/questions/45642727/what-is-ahk-class-how-can-i-use-it-for-window-matching
if WinExist("ahk_class ActualTools_TabbedExplorerContainerWindow"){ ; if the window exists
        WinGet, state, MinMax ;get the state of the window. is it maximized or minimized. this could be part of the issue
        If state >= 0 ; if its not minimized, minimize it
            WinMinimize
        else
            WinActivate ; otherwise make it the active window
}else
    Run, Explorer.exe ;otherwise not open, open explorer
return

But this solution required for me to often have to hit the hotkey twice. Once to make it active(if it wasnt top most, even it is still seeable) and then again to minimize it. Hope this helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.