Script to close all open windows

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Script to close all open windows

29 May 2017, 10:56

Can someone code a small script to close all open windows like the program CloseAll does but without a GUI. :)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Script to close all open windows

29 May 2017, 11:12

Code: Select all

F1::
DetectHiddenWindows, Off
WinGet, mylist, list
Loop % mylist
WinClose % "ahk_id" mylist%A_Index%
return
Untested but should be what you need. Follow any links in the box above for documentation, and additionally look at expression to explain how I used WinClose.
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

31 May 2017, 08:39

Unfortunately not working. :(
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Script to close all open windows

31 May 2017, 08:51

Ah, "Not Working" - the most descriptive and useful piece of feedback ever </sarcasm>
So the above code does not close any windows? It closes some, but not all? The code would not run at all?
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

31 May 2017, 09:08

evilC wrote:Ah, "Not Working" - the most descriptive and useful piece of feedback ever </sarcasm>
So the above code does not close any windows? It closes some, but not all? The code would not run at all?
You're right, I'm sorry. It closes nothing, there is just a tray icon of AutoHotKey...
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

31 May 2017, 09:17

Tried running as admin but didn't work...
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Script to close all open windows

31 May 2017, 09:49

The script works fine for me
Have you tried debugging it?

eg with this variant, do you get any dialogs pop up?

Code: Select all

F1::
DetectHiddenWindows, Off
WinGet, mylist, list
Loop % mylist {
   hwnd := mylist%A_Index%
   msgbox % "Found window with HWND " hwnd
   WinClose % "ahk_id" hwnd
}
return
Maybe this line WinClose % "ahk_id" hwnd should be WinClose % "ahk_id " hwnd?
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

31 May 2017, 09:59

It doesn't work. Tried it with admin rights as well.
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

31 May 2017, 10:50

If I press "F1" while the try icon is running I get multiple of those:
11.png
11.png (35.4 KiB) Viewed 10636 times
22.png
22.png (14.59 KiB) Viewed 10636 times
If I click OK OK OK all windows are closing but I was hoping it would be even faster than CloseAll. Just one double klick.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Script to close all open windows

31 May 2017, 10:52

"If I click OK OK OK all windows are closing"
Which windows? The dialog window or your open windows?

I see zero reason why the code would work with a msgbox in, but will not work without.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Script to close all open windows

31 May 2017, 10:54

Code: Select all

F1::
DetectHiddenWindows, Off
WinGet, mylist, list
str := ""
Loop % mylist {
   hwnd := mylist%A_Index%
   WinGetTitle, title, % "ahk_id " hwnd
   if (title == "")
      continue
   str .= "HWND: " hwnd ", Title: " title "`n"
   WinClose % "ahk_id " hwnd
}
msgbox % str
return
Does this list the titles of all your open windows?
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Script to close all open windows

31 May 2017, 11:14

@Thouldre: This code doesn't mess with "Start" or "Program Manager", so it doesn't ask if you want to shut down Windows. You get a count of the windows closed at the end. You can just hit Escape to bail out. I'm running Win7 Pro, X64, works okay. Everything is running as Admin.

Edit: I'll mess with it a little. The Shutdown dialog appears briefly, which is disconcerting. Does work, though.
Re-edit: My If statement doesn't seem to do very much. Commented out.

Code: Select all

WindowCount := ""
F1::
DetectHiddenWindows, Off
WinGet, mylist, list
Loop % mylist {
   hwnd := mylist%A_Index%
   WindowCount = %A_Index%
   WinGetTitle, title, % "ahk_id " hwnd
   ;~ msgbox % "Found window with HWND " hwnd ", title " title
   ;If (title <> "Start" && title <> "Program Manager")
      WinClose % "ahk_id " hwnd
}
msgbox Closed %WindowCount% windows.
WindowCount := ""
return

Escape::ExitApp
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

01 Jun 2017, 09:09

It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above. :eh:

Forget it, I don't want to waste more of your time for something I already have an application for.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Script to close all open windows

02 Jun 2017, 12:51

Thouldre wrote:It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above. :eh:

Forget it, I don't want to waste more of your time for something I already have an application for.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

; probably doesn't work with windows added explicitly through ITaskbarList
GetTaskbarVisibleWindows(limit:=0, checkDisabled:=True, checkEmptyTitle:=False
					   ,compareLastActivePopupToOwner:=False, checkNoActivate:=True
					   ,checkImmediateOwnerVisibility:=True, checkITaskListDeleted:=True)
{
	static sevenOrBelow := A_OSVersion ~= "WIN_(7|XP|VISTA)", rect, PropEnumProcEx := 0, cleanup := {base: {__Delete: "GetTaskbarVisibleWindows"}}
	static WS_DISABLED := 0x08000000, WS_EX_TOOLWINDOW := 0x00000080, WS_EX_APPWINDOW := 0x00040000, WS_EX_CONTROLPARENT := 0x00010000, WS_EX_NOREDIRECTIONBITMAP := 0x00200000, WS_EX_NOACTIVATE := 0x08000000
	static GA_ROOTOWNER := 3, GW_OWNER := 4, DWMWA_CLOAKED := 14

	if (!cleanup) {
		if (PropEnumProcEx)
			DllCall("GlobalFree", "Ptr", PropEnumProcEx, "Ptr"), PropEnumProcEx := 0
		return
	}

	if (PropEnumProcEx && A_EventInfo == PropEnumProcEx && compareLastActivePopupToOwner >= 4096 && DllCall("IsWindow", "Ptr", limit)) {
		if (checkDisabled && StrGet(checkDisabled) == "ApplicationViewCloakType") {
			NumPut(checkEmptyTitle != 1, compareLastActivePopupToOwner+0, "Int")
			return False
		}
		return True
	}

	if (!VarSetCapacity(rect)) {
		VarSetCapacity(rect, 16)
		if (!sevenOrBelow)
			PropEnumProcEx := RegisterCallback(A_ThisFunc, "Fast", 4)
	}

	shell := 0 ; DllCall("GetShellWindow", "Ptr")

	ret := []
	prevDetectHiddenWindows := A_DetectHiddenWindows

	DetectHiddenWindows Off

	WinGet id, list,,, Program Manager
	Loop %id% {
		hwnd := id%A_Index%

		if (limit && limit == ret.MaxIndex())
			break

		if (checkEmptyTitle) {
			WinGetTitle wndTitle, ahk_id %hwnd%
			if (!wndTitle)
				continue
		}

		if (checkDisabled) {
			WinGet dwStyle, Style, ahk_id %hwnd%
			if (dwStyle & WS_DISABLED)
				continue
		}

		if (checkITaskListDeleted && DllCall("GetProp", "Ptr", hwnd, "Str", "ITaskList_Deleted", "Ptr"))
			continue 

		if (DllCall("GetWindowRect", "Ptr", hwnd, "Ptr", &rect) && !DllCall("IsRectEmpty", "Ptr", &rect)) {
			if (!shell) {
				hwndRootOwner := DllCall("GetAncestor", "Ptr", hwnd, "UInt", GA_ROOTOWNER, "Ptr")
			} else {
				hwndTmp := hwnd
				Loop {
					hwndRootOwner := hwndTmp
					hwndTmp := DllCall("GetWindow", "Ptr", hwndTmp, "UInt", GW_OWNER, "Ptr")
				} until (!hwndTmp || hwndTmp == shell)
			}

			if (compareLastActivePopupToOwner)
				if (DllCall("GetLastActivePopup", "Ptr", hwndRootOwner, "Ptr") != hwnd) ; https://autohotkey.com/boards/viewtopic.php?t=13288
					continue

			WinGet dwStyleEx, ExStyle, ahk_id %hwndRootOwner%
			if (hwnd != hwndRootOwner)
				WinGet dwStyleEx2, ExStyle, ahk_id %hwnd%
			else
				dwStyleEx2 := dwStyleEx

			hasAppWindow := dwStyleEx2 & WS_EX_APPWINDOW
			if ((checkNoActivate) && ((dwStyleEx2 & WS_EX_NOACTIVATE) && !hasAppWindow))
				continue

			if (checkImmediateOwnerVisibility) {
				hwndOwner := DllCall("GetWindow", "Ptr", hwnd, "UInt", GW_OWNER, "Ptr")
				if (!(!hwndOwner || !DllCall("IsWindowVisible", "Ptr", hwndRootOwner)))
					continue
			}				

			if (!(dwStyleEx & WS_EX_TOOLWINDOW) || hasAppWindow || (!(dwStyleEx2 & WS_EX_TOOLWINDOW) && dwStyleEx2 & WS_EX_CONTROLPARENT)) {
				if (!sevenOrBelow) {
					WinGetClass wndClass, ahk_id %hwnd%
					if (wndClass == "Windows.UI.Core.CoreWindow")
						continue
					if (wndClass == "ApplicationFrameWindow") {
						hasAppropriateApplicationViewCloakType := !PropEnumProcEx
						if (PropEnumProcEx)
							DllCall("EnumPropsEx", "Ptr", hwnd, "Ptr", PropEnumProcEx, "Ptr", &hasAppropriateApplicationViewCloakType)
						if (!hasAppropriateApplicationViewCloakType)
							continue
					} else {
						if (dwStyleEx & WS_EX_NOREDIRECTIONBITMAP) 
							continue
						if (!DllCall("dwmapi\DwmGetWindowAttribute", "Ptr", hwndRootOwner, "UInt", DWMWA_CLOAKED, "UInt*", isCloaked, "Ptr", 4) && isCloaked)
							continue
					}
				}
				ret.push(hwnd)
			}
		}
	}
	
	DetectHiddenWindows %prevDetectHiddenWindows%
	return ret
}

for _, hwnd in GetTaskbarVisibleWindows()
	PostMessage, 0x112, 0xF060,,, ahk_id %hwnd%
The author of the CloseAll application you linked wrote an open source Alt+Tab replacement for XP, TaskSwitchXP. The code for determining whether a window for switching to/closing should be displayed is mostly the same in both applications, so I took it and added the additional Windows 8+ specific checks CloseAll does. Thanks also to the authors of https://github.com/kvakulo/Switcheroo - better checks were able to be added.

EDIT: Also note CloseAll runs with UI Access privileges. For this script to have the same effectiveness (assuming this function itself is as effective as CloseAll's code), you must either run this script as administrator or enable the UI Access option in the AutoHotkey installer and run this script with UI Access.

Sorry, I was being too vague here - because CloseAll is signed and has UI Access capability, it can close programs started as admin or other UI Access programs (like the on screen keyboard) without needing to be admin itself. For AutoHotkey to be able to do the same, the script must be run as admin or with UI Access.

EDIT 2: You're welcome, Thouldre :-)
Last edited by qwerty12 on 04 Jun 2017, 22:10, edited 6 times in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to close all open windows

02 Jun 2017, 23:29

Re. identifying windows with a taskbar button:
Recover accidentally closed window/program - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 28#p147928

Does anyone have any ideas on the differences between the taskbar button list and the alt-tab menu list. And also, how to remove Desktop or another window from the alt-tab menu (other than the method that makes the alt-tab menu look old school).

Related question:
How can i remove a window from taskbar and Alt+Tab window but still have access to it through an hotkey? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=21152
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

03 Jun 2017, 06:10

qwerty12 wrote:
Thouldre wrote:It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above. :eh:

Forget it, I don't want to waste more of your time for something I already have an application for.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

GetTaskbarVisibleWindows() ; probably doesn't work with windows added explicitly through ITaskbarList
{
	static WS_EX_TOOLWINDOW := 0x00000080, WS_EX_APPWINDOW := 0x00040000, WS_EX_CONTROLPARENT := 0x00010000, sevenOrBelow := A_OSVersion ~= "WIN_(7|XP|VISTA)", rect
	if (!VarSetCapacity(rect))
		VarSetCapacity(rect, 16)
	shell := DllCall("GetShellWindow", "Ptr")

	ret := []
	prevDetectHiddenWindows := A_DetectHiddenWindows

	DetectHiddenWindows Off

	WinGet id, list,,, Program Manager
	Loop %id% {
		hwnd := id%A_Index%

		hwndTmp := hwnd
		Loop {
			hwndOwner := hwndTmp
			hwndTmp := DllCall("GetWindow", "Ptr", hwndTmp, "UInt", 4, "Ptr")
		} until (!hwndTmp || hwndTmp == shell)

		WinGet dwStyleEx, ExStyle, ahk_id %hwndOwner%
		if (hwnd != hwndOwner)
			WinGet dwStyleEx2, ExStyle, ahk_id %hwnd%
		else
			dwStyleEx2 := dwStyleEx

		if (!(dwStyleEx & WS_EX_TOOLWINDOW) || dwStyleEx2 & WS_EX_APPWINDOW || (!(dwStyleEx2 & WS_EX_TOOLWINDOW) && dwStyleEx2 & WS_EX_CONTROLPARENT)) {
			if ((sevenOrBelow) || (!DllCall("dwmapi\DwmGetWindowAttribute", "Ptr", hwndOwner, "UInt", 14, "UInt*", isCloaked, "Ptr", 4) && !isCloaked)) {
				if (DllCall("GetWindowRect", "Ptr", hwnd, "Ptr", &rect) && !DllCall("IsRectEmpty", "Ptr", &rect)) {
					ret.push(hwnd)
				}
			}
		}
	}
	
	DetectHiddenWindows %prevDetectHiddenWindows%
	return ret
}

for _, hwnd in GetTaskbarVisibleWindows()
	PostMessage, 0x112, 0xF060,,, ahk_id %hwnd%
The author of the CloseAll application you linked wrote an open source Alt+Tab replacement for XP, TaskSwitchXP. The code for determining whether a window for switching to/closing should be displayed is mostly the same in both applications, so I took it and added the additional Windows 8+ specific checks CloseAll does.

EDIT: Also note CloseAll runs with UI Access privileges. For this script to have the same effectiveness (assuming this function itself is as effective as CloseAll's code), you must either run this script as administrator or enable the UI Access option in the AutoHotkey installer and run this script with UI Access.
Here we are again... Thank you very much, qwerty12!! :dance: You just made my life easier... again! :D
It works perfectly smooth, even without admin privileges! I think you need them because your Windows UAC control is set to 4/4 instead of the default 3/4.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Script to close all open windows

03 Jun 2017, 08:35

Oi, I'm sorry I hadn't noticed this earlier. @evilC was right in a post above that in my code, I omitted a space.

This should work all the same:

Code: Select all

F1::
DetectHiddenWindows, Off
WinGet, mylist, list
Loop % mylist
WinClose % "ahk_id " mylist%A_Index% ; space after ahk_id
return
(But it might have the desktop icon-hiding behavior, not sure.)

But if qwerty12 came in with a good code for ya, no worries.
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

03 Jun 2017, 09:04

Just tried it out of curiosity: not closing any windows, not even with admin privileges. Thanks for your time but no need to fix it. qwerty12's script works like charm. :thumbup:
Thouldre
Posts: 42
Joined: 25 May 2017, 14:29

Re: Script to close all open windows

09 Jun 2017, 04:19

qwerty12 wrote:
Thouldre wrote:It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above. :eh:

Forget it, I don't want to waste more of your time for something I already have an application for.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

; probably doesn't work with windows added explicitly through ITaskbarList
GetTaskbarVisibleWindows(limit:=0, checkDisabled:=True, checkEmptyTitle:=False
					   ,compareLastActivePopupToOwner:=False, checkNoActivate:=True
					   ,checkImmediateOwnerVisibility:=True, checkITaskListDeleted:=True)
{
	static sevenOrBelow := A_OSVersion ~= "WIN_(7|XP|VISTA)", rect, PropEnumProcEx := 0, cleanup := {base: {__Delete: "GetTaskbarVisibleWindows"}}
	static WS_DISABLED := 0x08000000, WS_EX_TOOLWINDOW := 0x00000080, WS_EX_APPWINDOW := 0x00040000, WS_EX_CONTROLPARENT := 0x00010000, WS_EX_NOREDIRECTIONBITMAP := 0x00200000, WS_EX_NOACTIVATE := 0x08000000
	static GA_ROOTOWNER := 3, GW_OWNER := 4, DWMWA_CLOAKED := 14

	if (!cleanup) {
		if (PropEnumProcEx)
			DllCall("GlobalFree", "Ptr", PropEnumProcEx, "Ptr"), PropEnumProcEx := 0
		return
	}

	if (PropEnumProcEx && A_EventInfo == PropEnumProcEx && compareLastActivePopupToOwner >= 4096 && DllCall("IsWindow", "Ptr", limit)) {
		if (checkDisabled && StrGet(checkDisabled) == "ApplicationViewCloakType") {
			NumPut(checkEmptyTitle != 1, compareLastActivePopupToOwner+0, "Int")
			return False
		}
		return True
	}

	if (!VarSetCapacity(rect)) {
		VarSetCapacity(rect, 16)
		if (!sevenOrBelow)
			PropEnumProcEx := RegisterCallback(A_ThisFunc, "Fast", 4)
	}

	shell := 0 ; DllCall("GetShellWindow", "Ptr")

	ret := []
	prevDetectHiddenWindows := A_DetectHiddenWindows

	DetectHiddenWindows Off

	WinGet id, list,,, Program Manager
	Loop %id% {
		hwnd := id%A_Index%

		if (limit && limit == ret.MaxIndex())
			break

		if (checkEmptyTitle) {
			WinGetTitle wndTitle, ahk_id %hwnd%
			if (!wndTitle)
				continue
		}

		if (checkDisabled) {
			WinGet dwStyle, Style, ahk_id %hwnd%
			if (dwStyle & WS_DISABLED)
				continue
		}

		if (checkITaskListDeleted && DllCall("GetProp", "Ptr", hwnd, "Str", "ITaskList_Deleted", "Ptr"))
			continue 

		if (DllCall("GetWindowRect", "Ptr", hwnd, "Ptr", &rect) && !DllCall("IsRectEmpty", "Ptr", &rect)) {
			if (!shell) {
				hwndRootOwner := DllCall("GetAncestor", "Ptr", hwnd, "UInt", GA_ROOTOWNER, "Ptr")
			} else {
				hwndTmp := hwnd
				Loop {
					hwndRootOwner := hwndTmp
					hwndTmp := DllCall("GetWindow", "Ptr", hwndTmp, "UInt", GW_OWNER, "Ptr")
				} until (!hwndTmp || hwndTmp == shell)
			}

			if (compareLastActivePopupToOwner)
				if (DllCall("GetLastActivePopup", "Ptr", hwndRootOwner, "Ptr") != hwnd) ; https://autohotkey.com/boards/viewtopic.php?t=13288
					continue

			WinGet dwStyleEx, ExStyle, ahk_id %hwndRootOwner%
			if (hwnd != hwndRootOwner)
				WinGet dwStyleEx2, ExStyle, ahk_id %hwnd%
			else
				dwStyleEx2 := dwStyleEx

			hasAppWindow := dwStyleEx2 & WS_EX_APPWINDOW
			if ((checkNoActivate) && ((dwStyleEx2 & WS_EX_NOACTIVATE) && !hasAppWindow))
				continue

			if (checkImmediateOwnerVisibility) {
				hwndOwner := DllCall("GetWindow", "Ptr", hwnd, "UInt", GW_OWNER, "Ptr")
				if (!(!hwndOwner || !DllCall("IsWindowVisible", "Ptr", hwndRootOwner)))
					continue
			}				

			if (!(dwStyleEx & WS_EX_TOOLWINDOW) || hasAppWindow || (!(dwStyleEx2 & WS_EX_TOOLWINDOW) && dwStyleEx2 & WS_EX_CONTROLPARENT)) {
				if (!sevenOrBelow) {
					WinGetClass wndClass, ahk_id %hwnd%
					if (wndClass == "Windows.UI.Core.CoreWindow")
						continue
					if (wndClass == "ApplicationFrameWindow") {
						hasAppropriateApplicationViewCloakType := !PropEnumProcEx
						if (PropEnumProcEx)
							DllCall("EnumPropsEx", "Ptr", hwnd, "Ptr", PropEnumProcEx, "Ptr", &hasAppropriateApplicationViewCloakType)
						if (!hasAppropriateApplicationViewCloakType)
							continue
					} else {
						if (dwStyleEx & WS_EX_NOREDIRECTIONBITMAP) 
							continue
						if (!DllCall("dwmapi\DwmGetWindowAttribute", "Ptr", hwndRootOwner, "UInt", DWMWA_CLOAKED, "UInt*", isCloaked, "Ptr", 4) && isCloaked)
							continue
					}
				}
				ret.push(hwnd)
			}
		}
	}
	
	DetectHiddenWindows %prevDetectHiddenWindows%
	return ret
}

for _, hwnd in GetTaskbarVisibleWindows()
	PostMessage, 0x112, 0xF060,,, ahk_id %hwnd%
The author of the CloseAll application you linked wrote an open source Alt+Tab replacement for XP, TaskSwitchXP. The code for determining whether a window for switching to/closing should be displayed is mostly the same in both applications, so I took it and added the additional Windows 8+ specific checks CloseAll does. Thanks also to the authors of https://github.com/kvakulo/Switcheroo - better checks were able to be added.

EDIT: Also note CloseAll runs with UI Access privileges. For this script to have the same effectiveness (assuming this function itself is as effective as CloseAll's code), you must either run this script as administrator or enable the UI Access option in the AutoHotkey installer and run this script with UI Access.

Sorry, I was being too vague here - because CloseAll is signed and has UI Access capability, it can close programs started as admin or other UI Access programs (like the on screen keyboard) without needing to be admin itself. For AutoHotkey to be able to do the same, the script must be run as admin or with UI Access.

EDIT 2: You're welcome, Thouldre :-)
There is one little thing that would make the script perfect: 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?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ntepa and 246 guests