Script to close all open windows

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Script to close all open windows

09 Jun 2017, 07:08

Thouldre wrote:After closing all windows I first have to klick on the desktop before I can use Alt+F4 to shutdown Windows. Can you auto focus the desktop after closing all windows?
I just tried the script and pressing Alt+F4 to bring up the Shut Down Windows dialog box worked as it is for me, without needing to manually focus anything. Anyway, you can try adding one of the following at the end:
  • WinActivate % "ahk_id " . DllCall("GetShellWindow", "Ptr") - focuses the desktop window, which accepts Alt+F4
  • ComObjCreate("Shell.Application").ShutdownWindows - shows the Shut Down Windows dialog box automatically
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

09 Jun 2017, 07:55

I used the first one and it works! But unfortunately there is another problem: If documents are still open you are going to be asked if you want to save the current state. With the original script you would just hit "Enter" and it would save the document. But now you have to klick on each document to hit "Enter" probably because of the focus at the desktop. With CloseAll you could just hit "Enter" for each document to save the document e.g. 3 open documents: Enter, Enter, Enter and then press Alt+F4 to shutdown the system.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Script to close all open windows

09 Jun 2017, 08:09

Off the top of my head, one of these workarounds may work:
  • Inside the loop, use WinGet to get the PID of the process hosting the window and use Process WaitClose to wait for the window to close before having the script post SC_CLOSE to the next window
  • Outside of the loop, before activating the shell window, run GetTaskbarVisibleWindows() in another loop and have the script wait before selecting the shell window. Something like this, I think:

    Code: Select all

    while (GetTaskbarVisibleWindows().MaxIndex())
    	Sleep 100
    
nelvin08
Posts: 97
Joined: 27 Mar 2022, 23:13

Re: Script to close all open windows

15 Jan 2024, 06:27

Sorry to hijack this thread but the original code works except that it removes all the desktop icons as well. Is there a way NOT to do that?

Code: Select all

#Esc::
DetectHiddenWindows, Off
WinGet, WindowList, List
Loop, %WindowList%
{
	WinClose, % "ahk_id " . WindowList%A_Index%
}
return
User avatar
mikeyww
Posts: 27267
Joined: 09 Sep 2014, 18:38

Re: Script to close all open windows

15 Jan 2024, 13:55

It seems that the following is a working solution.

viewtopic.php?f=76&t=96486#p555158

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], ghoulishghoul and 165 guests