Close open windows and shutdown the computer

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SimPlayer
Posts: 128
Joined: 01 Apr 2016, 05:57

Close open windows and shutdown the computer

Post by SimPlayer » 09 Nov 2021, 14:51

Hi all.

I would like to close all open Windows (programs) and shut down the computer with a script.

I found a script that shuts down Windows and also a script that shuts down the computer...but how do I combine these 2 so it works at all times?

Here is the first part:

Code: Select all

WinGet, id, list, , , Program Manager
Loop, %id%
{
 StringTrimRight, this_id, id%a_index%, 0
 WinGetTitle, this_title, ahk_id %this_id%
 winclose,%this_title%
}
Return
[Mod edit: [code][/code] tags added.]


Here the second part:

Code: Select all

Sleep 1000
Shutdown, 1
Sleep 1000
exit app

Thanks,

SimPlayer.

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

Re: Close open windows and shutdown the computer

Post by mikeyww » 09 Nov 2021, 15:26

This script could lead to loss of data, so use at your own risk.

Code: Select all

WinGet, wins, List
Loop, %wins% {
 WinGet, style, Style, % wTitle := "ahk_id " wins%A_Index%
 If !(style ~= "0x(9|16)")
  WinClose, %wTitle%
}
; Shutdown, POWERDOWN := 8

DaveF
Posts: 21
Joined: 14 Nov 2016, 17:01

Re: Close open windows and shutdown the computer

Post by DaveF » 09 Nov 2021, 16:11

I use:

Code: Select all

run C:\Windows\System32\shutdown.exe -s -t 00
Full routine which closes my regular programs & saves a few settings before calling the above.

Code: Select all

PostMessage,0x0111,975,,,ahk_class MediaPlayerClassicW ; Add Favourite
PostMessage,0x0111,816,,,ahk_class MediaPlayerClassicW ; exits MPC

FileCopy, C:\dwgs\Programs\Notepad++\config.xml, C:\dwgs\Programs\Notepad++\config store.xml, 1 


IfWinExist, ahk_exe firefox.exe 
Winclose, ahk_exe firefox.exe 

IfWinExist, ahk_exe thunderbird.exe 
Winclose, ahk_exe thunderbird.exe 

IfWinExist, ahk_exe FreeCommander.exe
Winclose, ahk_exe FreeCommander.exe

Process, Close, Notepad++.exe
;Process, Close, firefox.exe
Process, Close, i_view32.exe

run C:\Windows\System32\shutdown.exe -s -t 00
AHK 1.1.34.03, Win 10 Pro. Latest Firefox

SimPlayer
Posts: 128
Joined: 01 Apr 2016, 05:57

Re: Close open windows and shutdown the computer

Post by SimPlayer » 10 Nov 2021, 09:47

Hi.

Thanks for your answers.

I have exactly 3 softvares that run and that need to be shut down before the computer in order to work properly, non of them ever needs to save anything at all, they just run. DaveF sent a script that shuts down a specific program based on the exe file. Now, would it work ok if I just would put the program that needs to be shut down like this:

Code: Select all

IfWinExist, ahk_exe firstprogram.exe 
Winclose, ahk_exe firstprogram.exe 

IfWinExist, ahk_exe secondprogram.exe 
Winclose, ahk_exe secondprogram.exe 

IfWinExist, ahk_exe thirdprogram.exe 
Winclose, ahk_exe thirdprogram.exe 

run C:\Windows\System32\shutdown.exe -s -t 00
[Mod edit: [code][/code] tags added.]

This doesn´t shut down the AHK scripts, I will just have to shut them down with a "stop" code, or is there a way the exit all AHK scrips at the same time somehow?

SimPlayer

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Close open windows and shutdown the computer

Post by GEV » 10 Nov 2021, 10:52

Try

Code: Select all

; Use Widow Spy to find the ahk_exe of your programs

programs := "notepad,WINWORD,msedge"

Loop, parse, programs, `,
{
	WinClose, ahk_exe %A_LoopField%.exe
	WinWaitClose, ahk_exe %A_LoopField%.exe, , 2 ; seconds to wait
	if ErrorLevel
	{
		MsgBox, Cannot close %A_LoopField%.exe.`nYou have to close it manually.
			ExitApp
	}
}

; If all the above programs are closed
run shutdown.exe -s -t 00
You don't have to close any AHK scripts before Shutdown
Last edited by GEV on 10 Nov 2021, 14:01, edited 1 time in total.

Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Close open windows and shutdown the computer

Post by Lepes » 10 Nov 2021, 13:15

https://www.autohotkey.com/docs/commands/WinKill.htm

WinKill will try to close a window and if it fail, it force to close. Maybe you need this.

Shutdown also have a "force" parameter, if you launch with it, it will kill all applications.
https://www.autohotkey.com/docs/commands/Shutdown.htm

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Close open windows and shutdown the computer

Post by GEV » 10 Nov 2021, 13:37

Brute force to close any open programs (WinKill, forced Shutdown) should be used only in extreme cases. They can damage program files or corrupt data.

SimPlayer
Posts: 128
Joined: 01 Apr 2016, 05:57

Re: Close open windows and shutdown the computer

Post by SimPlayer » 18 Nov 2021, 14:07

Hi all.

Yes, there´s several ways to do this. The important thing is that the computer needs to be shut down (no power) without any type of windows popping up, windows that ask you to close a specific softvare. There´s never anything to save in the softvares, and it might be that they would close nicely when windows is closed...but you never know. Therefore I would like to close all programs before closing windows.

SimPlayer

Zubarm
Posts: 18
Joined: 11 May 2022, 01:05

Re: Close open windows and shutdown the computer

Post by Zubarm » 01 Jun 2023, 17:10

I have the same script on my laptop does anyone know if the system is getting overloadednow when the laptop is not shut down correctly but by a script ?

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

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 06:33

mikeyww wrote:
09 Nov 2021, 15:26
This script could lead to loss of data, so use at your own risk.

Code: Select all

WinGet, wins, List
Loop, %wins% {
 WinGet, style, Style, % wTitle := "ahk_id " wins%A_Index%
 If !(style ~= "0x(9|16)")
  WinClose, %wTitle%
}
; Shutdown, POWERDOWN := 8
What does the line with the If !(style ~= "0x(9|16)") mean?

I have the code below and it's working, the only issue I have is that it removes my desktop icons as well. Is there a way for it 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: 26885
Joined: 09 Sep 2014, 18:38

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 07:35

Hi,

My earlier script is a feeble attempt to target only windows that are visible. It is probably faulty. There are other posts on the forum that describe how to use window styles to distinguish visible from non-visible windows. I have never found them to be completely reliable for all windows. You can craft your own rules to indicate what windows to include or exclude. I typically have some windows that I exclude by window title; specifying them is generally straightforward. There could be other approaches, too, including possibly checking the coordinates of the window. I have not tested that.

Displaying a list of all window titles may be informative as you refine your script. You will find Program Manager, windows with null titles, etc.

Code: Select all

#Requires AutoHotkey v1.1.35
WinGet win, List
str := ""
Loop % win {
 WinGetTitle title, % "ahk_id" win%A_Index%
 str .= title "`n"
}
MsgBox 64, Window titles, % A_Clipboard := str
Return
I wrote a script that I have called "CloseAll". It works reasonably well. It checks for this window style value before proceeding: winStyle & 0x10000000. That is the style from WinGet, Style. It then also handles my exclusions, and excludes windows with null titles. My exclusions include Citrix Workspace, MainWindow, Program Manager, Start, Windows Task Manager, ApplicationFrameHost, ctfmon, but this list can be customized as needed.

The following post shows an approach to using both the style and the extended style. I have used this with some success.

viewtopic.php?p=505727#p505727

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

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 08:24

The blank spaces mean those with null titles? My only problem is I don't know which title removes the desktop icons. And if your code is returning blank, how do I exclude them then?

Thank you
Attachments
image.png
image.png (12.37 KiB) Viewed 585 times

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

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 08:51

Yes, a blank title is most likely a null title.

Below is an example that you can test and adjust to meet your needs.

Find visible windows

Code: Select all

; This script displays a GUI of information about visible windows
#Requires AutoHotkey v1.1.35
exclude := "Program Manager|Start|Windows Task Manager|MainWindow"
Gui Font, s10
Gui Color, F9FF59
Gui Add, ListView, w800 r25 Grid ReadOnly Count20 BackgroundF0F0F0, Process|Window title
WinGet winArray, List                                    ; Get list of all windows
Loop % winArray {                                        ; Loop through each window
 WinGetTitle title       , % winTitle := "ahk_id" winArray%A_Index%
 WinGet proc, ProcessName, % winTitle
 If !(title ~= "i)^(" exclude ")$") && visible(winTitle) ; If title is not excluded, and window is visible,
  LV_Add(, StrReplace(proc, ".exe"), title)              ;  then add it to the ListView
}
LV_ModifyCol(), LV_ModifyCol(1, "Sort")
Gui Show,, Visible windows
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" )
}

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

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 09:07

Thanks. This looks good and all. They indeed only show the open applications. But the problem is not the visible windows.

The problem is my desktop icons. I don't know what WindowsTitle they belong to and how to exclude it.

If you have an idea, please let me know.


Thank you.

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

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 09:09

If you use the windows matching the criteria for visible windows, I don't think that it will include your desktop icons. You can confirm this simply by running the script and having a look at the GUI. An icon is a file, not a window.

WinClose would not delete any icons unless you use a program that actually populates your desktop with the icons in a simulated fashion (i.e., displaying a dedicated window that depicts icons). If you use Nimi Places or some program like that, you can exclude it by process or window title.

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

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 12:08

I don't use any apps to populate my desktop. If you use my original code, you'll see what I mean.

On the other hand, using your code, the below works. But even tho I don't have the exclude variable, everything works fine. Is there any specific reason I should keep those?

Program Manager|Start|Windows Task Manager|MainWindow

Also I should mention that the shutdown prompt is no longer showing. Previously it was there. So in my original code, whenever I press the hotkey, it automatically closes everything and then shows the shutdown prompt. then I only need to press enter and that's it.

Code: Select all

#Esc::
DetectHiddenWindows, Off
exclude := ""
WinGet, WindowList, List
Loop, %WindowList%
{
	WinGetTitle title       , % winTitle := "ahk_id" WindowList%A_Index%
	If !(title ~= "i)^(" exclude ")$") && visible(winTitle)
	WinClose, % "ahk_id " . WindowList%A_Index%
}
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: 26885
Joined: 09 Sep 2014, 18:38

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 13:40

Hello,

It is up to you which windows you want to close. You can experiment and see what works.

If your script includes your shutdown command, then the command will execute. If your script does not include your shutdown command, then the command will not execute. You have your choice.

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

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 13:47

Thanks. My original code below doesn't have the shutdown command but it still shows the prompt tho. lol

Anyway, I'll try to figure it out

Code: Select all

#Esc::
DetectHiddenWindows, Off
WinGet, WindowList, List
Loop, %WindowList%
{
	WinClose, % "ahk_id " . WindowList%A_Index%
}
return

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

Re: Close open windows and shutdown the computer

Post by mikeyww » 15 Jan 2024, 13:48

I noticed this from your original post.
Here the second part....

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

Re: Close open windows and shutdown the computer

Post by nelvin08 » 15 Jan 2024, 13:50

ah that's a different guy. That's not me

Post Reply

Return to “Ask for Help (v1)”