1
votes

I have the following AutoHotKey script to help me switch between different open apps, using a shortcut:

^!c::ToggleWindow("Chrome")
^!p::ToggleWindow("PowerShell")


ToggleWindow(TheWindowTitle)
{
    SetTitleMatchMode,2
    DetectHiddenWindows, Off
    IfWinActive, %TheWindowTitle%
    {
        WinMinimize, %TheWindowTitle%
    }
    Else
    {
        IfWinExist, %TheWindowTitle%
        {
            WinActivate
            ;;; Tried using WinMaximize/WinRestore here but same result
        }
        Else
        {
            DetectHiddenWindows, On
            IfWinExist, %TheWindowTitle%
            {
                WinShow
                WinActivate
            }
        }
    }
}

The problem is that for some apps it works only sometimes (for example, PowerShell and TortoiseHG Workbench) which is really frustrating. For other apps (Chrome, Thunderbird) it works always.

Here is what I've found so far:

  • If you explicitly minimize a "problematic" app then you can never activate/maximize the window using the AHK shortcuts. I'm not sure if there are other scenarios that prevent the shortcuts from working but this is one certain way to replicate the problem (at least for me).

  • Even in the cases where the shortcuts don't work, I can see that the target app icon in the taskbar is getting highlighted. I guessing it works somewhat halfway, activating the window but not actually showing it.

  • I think the problem is not restricted to AutoHotKey only because I can replicate this behavior just with the Task Manager. If I go to the 'Applications' tab, right click on on target app and choose 'Bring to front', the same thing happens. BUT, if I choose 'Switch to' instead, it works!

So, I guess my question is what exactly does "Task Manager->Applications->Switch to" do and is there an equivalent that I can use in AHK. My OS is Win7.

2
Your code is working fine for my PowerShell (Windows 8 64-bit), I wonder what's the problem there.fxam
Did you run your PowerShell as admin, but AutoHotkey as normal user? When I do so (in a Windows 7 64-bit machine), I cannot restore a minimized PowerShell window.fxam
Running AutoHotkey as admin solves the problem of restoring/minimizing a PowerShell window running as administrator.fxam
Yes! That's it! I was running PowerShell as admin and running AHK as admin too solves the problem. Thank you!Amati

2 Answers

1
votes

Try to put a #WinActivateForce in your script.

If you run AutoHotkey as normal user, it may be unable to manage program running as administrator (for example PowerShell). If that's the case, try to run AutoHotkey as administrator.

1
votes

If you explicitly minimize a "problematic" app then you can never activate/maximize the window using the AHK shortcuts. I'm not sure if there are other scenarios that prevent the shortcuts from working but this is one certain way to replicate the problem (at least for me).

According to AutoHotkey's release notes, this is a known issue which was fixed in AutoHotkey v1.1.20. (Released 1 month after this question was asked)

1.1.20.00 - March 8, 2015

Changes

  • Changed WinActivate to restore the window if already active but minimized.
  • Changed WinActivate to look for a visible window to activate if DetectHiddenWindows is off and the active window is hidden, instead of doing nothing