Close open windows and shutdown the computer

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 13:53

And so what did you mean by
the shutdown prompt is no longer showing. Previously it was there.
:?:

nelvin08
Posts: 97
Joined: 27 Mar 2022, 23:13

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 15:37

Okay, adding the code below is the final true form of the code. Instead of what others are doing which is to directly shut down the PC by using Shutdown, 1 (which I think is risky considering if you have unsaved work), this just shows the windows prompt if you want to shutdown / restart / sleep / switch user

ComObjCreate("Shell.Application").ShutdownWindows

Add this after the loop and before the return.

PS: I still don't get the difference between having the exclude variable. I tried playing with it but everything looks just the same.

nelvin08
Posts: 97
Joined: 27 Mar 2022, 23:13

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 17:45

For efficiency purposes, I will just ask this.

Is there some other way this code could be further simplified/shortened?

Code: Select all

#Esc::
exclude := "Program Manager|Start|Windows Task Manager|MainWindow"
WinGet, WindowList, List
Loop, %WindowList%
{
	WinGetTitle title, % winTitle := "ahk_id" WindowList%A_Index%
	If !(title ~= "i)^(" exclude ")$") && visible(winTitle)
	WinClose, % title
}
ComObjCreate("Shell.Application").ShutdownWindows
return

visible(winTitle) {
 WinGetTitle dwTitle   ,          % winTitle
 WinGet      dwStyle   , Style  , % winTitle
 WinGet      dwExStyle , ExStyle, % winTitle
 WinGetClass szClass   ,          % winTitle
 Return   !(      dwTitle   = ""
             ||   dwStyle   & 0x08000000
             || !(dwStyle   & 0x10000000)
             ||   dwExStyle & 0x00000080
             ||   szClass   = "TApplication" )
}

User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 18:02

I'm not sure how to answer that question. If it works, I would go with it. Others can chime in if they have other opinions about it.

nelvin08
Posts: 97
Joined: 27 Mar 2022, 23:13

Re: Close open windows and shutdown the computer

Post by nelvin08 » 16 Jan 2024, 04:11

All good. Thanks for the help. Can I pick your brain some more?

On your exclude line code, is there a different way instead of the windows title maybe exe or class? Cause for windows title, I have to be exact with the name of the window (e.g. if I have multiple notepads, I have to put the name of each specific notepad file name)

But I am not sure how that will play out with the program manager, start, windows manager, etc

Or maybe is there a wildcard in AHK where as long as the windows title contains that word, it will exclude it

User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Close open windows and shutdown the computer

Post by mikeyww » 16 Jan 2024, 06:41

Yes, that is possible. As a next step, I recommend that you read the documentation page about :arrow: WinGet. It shows how to get other information about the window. Your loop has already identified a unique WinTitle for the window, so you can now use WinGet to find anything else. You could, for example, get the process name (e.g., notepad.exe) and then add an exclusion for it.

The If... in and similar "contains" construct will let you provide a list of comma-separated terms for your matching; read there for details. This would come close to meeting your need of matching a word, though it matches text strings rather than words with word boundaries. An alternative for matching a list could be to use a regular expression like the one shown, but remove ^ and $ to yield partial text matches instead of matching the entire string. The regex notation for a word boundary is \b.

Finally, although you have a unique WinTitle, you then do not use it when you close your window. Your current script uses title instead. This may yield unwanted results, so use the WinTitle that is already defined in the loop. How could this yield unwanted results? WinClose will use the topmost match, but the specific window of this loop iteration might be a different window if multiple windows match the title. The WinTitle defined in the loop has no other matches because it is unique. My ListView indeed shows the window title for readability, but if you want to act upon the window, use the unique WinTitle provided (which includes the HWND). That is possible because WinGet with the List subcommand has already returned every HWND; that is what the command does.

nelvin08
Posts: 97
Joined: 27 Mar 2022, 23:13

Re: Close open windows and shutdown the computer

Post by nelvin08 » 16 Jan 2024, 07:32

Thanks a lot for the insights. I'll definitely explore the ProcessName subcommand later on.

About the WinClose, which is more correct then between the 2?

WinClose, % "ahk_id " . WindowList%A_Index%
WinClose, % winTitle

This is partly what I mean by making my code more efficient as well, correcting these simple mistakes so thank you for that

User avatar
mikeyww
Posts: 27198
Joined: 09 Sep 2014, 18:38

Re: Close open windows and shutdown the computer

Post by mikeyww » 16 Jan 2024, 08:23

You can display (or otherwise compare) the two values to verify that they are identical. This would mean that you can take your pick. I tend to use variables when I have them, because it can decrease the chance of certain coding errors, and sometimes improve the readability or efficiency of the script. Creating a variable presumably has some purpose to it. I will often create a variable when I plan to use it more than once, or if I want to be able to change it quickly.

In using variables, there may be some subtle gains in performance with not having to reevaluate certain expressions repeatedly. This is often not noticeable but might be an issue in long loops or for more complex expressions or operations.

In the case of your script, it is not necessarily a mistake but perhaps more of a preference or style decision with the coding.

Post Reply

Return to “Ask for Help (v1)”