Reputation: 1257
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.
Upvotes: 1
Views: 2484
Reputation: 1257
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?.
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,
}
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}
}
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.
Upvotes: 2