Script to close all open windows
Script to close all open windows
Can someone code a small script to close all open windows like the program CloseAll does but without a GUI.
Re: Script to close all open windows
Code: Select all
F1::
DetectHiddenWindows, Off
WinGet, mylist, list
Loop % mylist
WinClose % "ahk_id" mylist%A_Index%
return
Re: Script to close all open windows
Unfortunately not working.
Re: Script to close all open windows
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?
So the above code does not close any windows? It closes some, but not all? The code would not run at all?
Re: Script to close all open windows
You're right, I'm sorry. It closes nothing, there is just a tray icon of AutoHotKey...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?
Re: Script to close all open windows
Try running the script as admin?
Re: Script to close all open windows
Tried running as admin but didn't work...
Re: Script to close all open windows
The script works fine for me
Have you tried debugging it?
eg with this variant, do you get any dialogs pop up?
Maybe this line WinClose % "ahk_id" hwnd should be WinClose % "ahk_id " hwnd?
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
Re: Script to close all open windows
It doesn't work. Tried it with admin rights as well.
Re: Script to close all open windows
If I press "F1" while the try icon is running I get multiple of those:
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.
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.
Re: Script to close all open windows
"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.
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.
Re: Script to close all open windows
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
Re: Script to close all open windows
@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.
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
Re: Script to close all open windows
It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above.
Forget it, I don't want to waste more of your time for something I already have an application for.
Forget it, I don't want to waste more of your time for something I already have an application for.
Re: Script to close all open windows
Thouldre wrote:It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above.
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%
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.
Re: Script to close all open windows
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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to close all open windows
Here we are again... Thank you very much, qwerty12!! You just made my life easier... again!qwerty12 wrote:Thouldre wrote:It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above.
Forget it, I don't want to waste more of your time for something I already have an application for.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.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%
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.
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.
Re: Script to close all open windows
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:
(But it might have the desktop icon-hiding behavior, not sure.)
But if qwerty12 came in with a good code for ya, no worries.
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 if qwerty12 came in with a good code for ya, no worries.
Re: Script to close all open windows
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.
Re: Script to close all open windows
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?qwerty12 wrote:Thouldre wrote:It somehow closes even the desktop shortcuts but first there are a lot of random windows like those posted above.
Forget it, I don't want to waste more of your time for something I already have an application for.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.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%
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