Close all scripts except two?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Close all scripts except two?

16 Sep 2020, 23:38

Greetings all

Using a seperate script (I will call this the "Master" script), I want to be able to shut down all running scripts except two of them. These two are:

1. The "Master" script itself
2. "MyScript.ahk"

Maybe it would look something like this:

Code: Select all

;Master.ahk

z::
Keep "Master.ahk" alive ;which is in essence this script
Keep "MyScript.ahk" alive
Close all other running scripts
Return
I have some code which is able to keep "Master.ahk" alive but closes all other scripts. I'm unable to find a way to keep one additional script alive as well before closing the rest.
gregster
Posts: 9003
Joined: 30 Sep 2013, 06:48

Re: Close all scripts except two?

17 Sep 2020, 00:02

I imagine that showing said code could help potential helpers to help you (faster)...
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Close all scripts except two?

17 Sep 2020, 00:12

whoops, sorry!! Geez I totally meant to follow up with the code but got distracted..

Code: Select all

keepIT := "Master.ahk" ;keep this script alive but close all others
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where name = 'Autohotkey.exe' and not CommandLine like '%" keepIT "%' ")
process, close, % process.ProcessId
I didn't write this, in fact I can't remember where it came from but it closes all other scripts except Master.ahk
gregster
Posts: 9003
Joined: 30 Sep 2013, 06:48

Re: Close all scripts except two?

17 Sep 2020, 00:31

No problem - but that should help. Try

Code: Select all

keepIT := "Master.ahk" ;keep this script alive but close all others
keepIT2 := "MyScript.ahk"

for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where name = 'Autohotkey.exe' 
and not (CommandLine like '%" keepIT "%' or Commandline like '%" keepIT2 "%')")
	process, close, % process.ProcessId			; or for testing: msgbox % process.ProcessId " " process.CommandLine

Edit:
script names fixed.
teadrinker
Posts: 4327
Joined: 29 Mar 2015, 09:41
Contact:

Re: Close all scripts except two?

17 Sep 2020, 01:08

Another approach:

Code: Select all

DetectHiddenWindows, On
SetTitleMatchMode, RegEx
WinGet, ahkWindows, List, ahk_class ^AutoHotkey$,, i)Master.ahk|MyScript.ahk
Loop % ahkWindows
   WinKill, % "ahk_id " . ahkWindows%A_Index%
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Close all scripts except two?

17 Sep 2020, 12:33

Thanks guys!

I tested both approaches and they both work perfectly.

:) :) :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 237 guests