Amati
Amati

Reputation: 1532

WinActivate not working in autohotkey script

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:

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.

Upvotes: 1

Views: 8010

Answers (2)

Stevoisiak
Stevoisiak

Reputation: 26885

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

Upvotes: 1

fxam
fxam

Reputation: 3982

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.

Upvotes: 2

Related Questions