r/AutoHotkey • • 12d ago

v2 Script Help How do I make my script kill running v1 scripts as well?

I'm currently working on translating my v1 master script to v2. In V1 I had this:

^#!e::
DetectHiddenWindows, On
SetTitleMatchMode, RegEx
WinGet, ahkWindows, List, ahk_class ^AutoHotkey$,, i)Keyboard Fixes.ahk
Loop % ahkWindows
   WinKill, % "ahk_id " . ahkWindows%A_Index%
return

which killed v1 as well as v2 scripts. For the v2 version, I frankensteined this together:

^#!e::
{
    DetectHiddenWindows(true)
    SetTitleMatchMode("RegEx")

    ahkWindows := WinGetList("ahk_class ^AutoHotkey$",, "i)Keyboard Fixes.ahk")

    for hwnd in ahkWindows
        WinKill("ahk_id " hwnd)
}

But this only kills v2 scripts and leaves the v1 ones running, and I can't figure out why. Especially considering that if I swap WinKill for MsgBox, I get the ID for the v1 scripts as well, so it can't be a problem with the list. I'm super new to v2 and I've been trying to find smth useful online and on the "handbook" on their website, but I'm at my wit's end. Please help me TwT

2 Upvotes

4 comments sorted by

1

u/Keeyra_ 12d ago

Use ProcessClose. If the given AHK is the script itself, use this:

#Requires AutoHotkey 2.0
#SingleInstance

^#!e:: {
    DetectHiddenWindows(1)
    for hwnd in WinGetList("ahk_class AutoHotkey") {
        if (hwnd == A_ScriptHwnd)
            continue
        pid := WinGetPID(hwnd)
        (pid) && ProcessClose(pid)
    }
}

1

u/AnthonyJames696 12d ago

 If the given AHK is the script itself

I don't quite get what you mean by this 🙈

1

u/genesis_tv 12d ago

WinGetList will return a list of all AHK scripts, including this one that's closing all the others. You want to skip it for an obvious reason.

1

u/AnthonyJames696 12d ago

Oh! Oh gosh, I just re-read the sentence and now idk how I didn't get it lmao. Maybe it was because of being a bit eepy and still riding out the last bits of a cold. Yes. Let's say that's the reason xD