1

How to use Autohotkey to automatically close, minimize, maximize or send keys to a window as soon as it pops up? I can detect a dialog and close it with this:

WinWaitActive, TITLE
WinClose, TITLE

But this doesn't work if the window isn't open on script execution.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Schneyer
  • 1,197
  • 1
  • 9
  • 27

1 Answers1

2

This is a very common task AHK is used for.

First you need the title of the window you want to address. Read How to get the title of a window with AHK?.

The code

For the basic functionalitiy of closing a window we need Loop, WinWaitActive and WinClose.

Example for a Firefox window with Stack Overflow open.

Loop {
  WinWaitActive, Stack Overflow - Mozilla Firefox
  WinClose,
}

Explanation

The Loop repeats the process to close the window multiple times. WinWaitActive waits until the the window gets activated (pops up) and WinClose closes it.

Hint: If you don't specify a specifiy window title like with WindowClose the last found window, which is the one from WinWaitActive is used.

minimize/maximize

Instead of WinClose use WinMaximize or WinMinimize to perform the corresponding action.

Sending Keys

If you want to send specific keys (e.g. Enter) to the window use Send

Loop {
  WinWaitActive, Stack Overflow - Mozilla Firefox
  send {Enter}
}

Additions

If the basic version does not work or you want to create an more advanced script, here are some possible modifications.

More Force

If WinClose does not work try WinKill or Send, !{F4} to use more force.

As Admin

Admin rights might be necessary to close the window, use this code snippet on top of your script to make sure it runs with full access.

If not A_IsAdmin ;force the script to run as admin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}

Other matching methods

On default the window title has to be an exact match. To change this behavior and allow partial or start with matches use SetTitleMatchMode on top of your script, e.g. SetTitlematchMode, 2 for partial match.

Instead of title, the window class (ahk_class) or .exe (ahk_exe) from Window Spy can be used.

WinWaitActive, ahk_class MozillaWindowClass

or WinWaitActive, ahk_exe firefox.exe

Select the one which suits your needs carefully to only react to the correct window.

Community
  • 1
  • 1
Schneyer
  • 1,197
  • 1
  • 9
  • 27
  • 1
    Really good idea! I have some suggestions for improvement. Let's use SetTimer instead of Loop, because Loop blocks the script from doing anything else. There are more ways to close a window and there is better way than sending Alt+F4. To close certain windows, admin rights are required. See my answer from this question: http://stackoverflow.com/questions/34480195/using-ahk-to-close-a-pop-up-dialogue-within-visual-studio/34493278#34493278 – Forivin Jan 28 '16 at 09:48
  • @Forivin thanks, i will insert your ideas to the additions part to expand it even further. I just want to keep a very basic version for all those guys completly new to AHK. As you are an active member of the AHK tag and answered questions like this, i invite you to edit and improve my anser on your own. – Schneyer Jan 28 '16 at 11:17
  • 3 ways to open Window Spy right in the beginning seems overkill, don't you think? Also, it would be better to use WinWait instead of WinWaitActive. And `PostMessage, 0x112, 0xF060,,, %winTitle%` does the same thing as Alt+F4, but it's more reliable as it doesn't require the window to be active. – Forivin Jan 28 '16 at 15:56
  • I think it might be a better idea to create a new question that just says "How to find a Window Title or Text" for the Windo Spy tutorial and then link to it from other questions. – Forivin Jan 28 '16 at 15:58