Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Another XKill script


  • Please log in to reply
No replies to this topic
burt777
  • Members
  • 3 posts
  • Last active: Jul 22 2015 02:44 PM
  • Joined: 26 Aug 2012

Hello everybody, i know this has been done before, but i wanted to have a go at a nice XKill script. XKill is a Linux command that allows the user to kill a window by simply clicking on it. I like combining different languages in one single file, so this file doubles as a Batch file. Simply save this as C:\Windows\System32\xkill.bat (or xkill.bat in any other directory in your %Path%), so you can invoke it with a simple one-word command from any place.
 
You can also specify a string as parameter, and it'll pop up a list of checkboxes for all programs that match your string, so you can kill any or all of them at once.
 
C:\>XKill
Kill a window by clicking on it (watch out clicking the desktop or taskbar!! ;-> )

C:\>XKill Expl
Pop up a window with all programs that contain "Expl" in either process name or window title, check boxes, click Kill to kill them all.

C:\>XKill -e
Edit source (note that my editor is Sublime, invoked with subl, so change that to your personal editor or Notepad if all else fails)
 
A few notes:

  • My AutoHotKey.exe is in D:\Portable\AutoHotKey, change your path accordingly in the script.
  • If more than +- 40 matches are found, the Gui can't properly show them. There's no nice solution i could think off...
  • Does not use the WinWaitClose, because it would add the time-outs of all applications. Instead, it tries to close all applications first, then loops again and (hard) kills each and any that weren't (softly) closed.
  • I've already accepted that i probably go to hell for the use of Comment-hacks and goto's. (-:

 
XKill.bat 

;:: Bat Start:
;@echo off
;if [%1%]==[-e] goto Edit
;if [%1%]==[-h] goto ShowHelp
;Goto RunAhk


; *********** Ahk part here: *************

#SingleInstance Off
Menu, Tray, Icon, explorer.exe, 10

If %1% {
    DetectHiddenWindows On
    ; Anywhere within the title:

    NrOfIdsFound = 0
    IdList = 

    SetTitleMatchMode, 2
    WinGet, id, list
    Loop, %id%
    {
        thisId := id%A_Index%
        WinGetTitle, this_title, ahk_id %thisId%
        WinGet, this_name, ProcessName, ahk_id %thisId%
        
        IfInString, this_title, %1% 
        {
            NrOfIdsFound := NrOfIdsFound + 1
            Process%NrOfIdsFound% := thisId
        }
        IfInString, this_name, %1% 
        {
            NrOfIdsFound := NrOfIdsFound + 1
            Process%NrOfIdsFound% := thisId
        }
    }

    If (NrOfIdsFound == 0) {
        MsgBox, No windows found matching "%1%"
    } Else {
        Gui, +LastFound
        GuiHWND := WinExist()

        Gui, Font, Bold Underline
        Gui, Add, Button, y5 x10 Default gKillAll, KILL
        Gui, Add, CheckBox, vAllNone gAllNone, %NrOfIdsFound% Found matching "%1%" (all/none):
        GUi, Font,
        Gui, Add, Button, y5 x50 Cancel gXKillEnd, Cancel

        Loop, %NrOfIdsFound% {
            thisId := process%A_Index%
            WinGetTitle, thisTitle, ahk_id %thisId%
            WinGet, thisProcess, ProcessName, ahk_id %thisId%
            
            Gui, Add, CheckBox, x40 vCheckBox%A_Index%, %thisId% - %thisTitle% (%thisProcess%)
        }
        Gui, Show, Autosize, --== X Kill ==--
    }

} Else {
    ; Go into Kill Mode
    HotKey, LButton, XKillWindow
    HotKey, RButton, XKillEnd

    Msg = Click on a window to kill it`, right-click to cancel
    Loop {
        MouseGetPos, wX, wY, killWindow
        WinGetTitle, currentTitle, ahk_id %killWindow%
        ToolTip, %Msg%`n`nCurrent: `n%currentTitle%`n%killWindow%, % wX + 10, % wY + 10
        Sleep, 30
    }
}

Return ; ****************** End Of Auto Execute!! **********************************

; Toggle all boxes on or off:
allNone:
    GuiControlGet, checkVar, , AllNone
    Action := checkVar ? "check" : "uncheck"
    Loop, % NrOfIdsFound + 1 {
        Control, %action%, , Button%A_Index%
    }
Return

; Kill all checked processes:
KillAll:
    ; try closing first:
    Loop, %NrOfIdsFound%
    {
        GuiControlGet, checkVar, , CheckBox%A_Index%
        If (checkVar == 1) {
            thisProcess := process%A_Index%
            WinClose, ahk_id %thisProcess%
        }
    }

    Sleep, 500

    ; try killing all that's still left:
    Loop, %NrOfIdsFound%
    {
        ; currentCheckBox := A_Index + 1
        GuiControlGet, checkVar, , CheckBox%A_Index%
        If (checkVar == 1) {
            thisProcess := process%A_Index%
            IfWinExist, ahk_id %thisProcess%
                WinKill, ahk_id %thisProcess%
        }
    }

    ExitApp
Return


XKillWindow:
    MouseGetPos, wX, wY, killWindow
    if killWindow
    {
        ToolTip, 
        WinClose, ahk_id %killWindow%
        Sleep, 500
        IfWinExist, ahk_id %killWindow%
            WinKill, ahk_id %KillWindow%
        ExitApp
    }
Return

; All the ways to exit:
GuiClose:
GuiEscape:
XKillEnd:
    ExitApp
Return

/*
:: *********** Dos part here: *******************

:ShowHelp
    echo Click on any window to WinClose and then WinKill it.
    echo.
    echo        -e  Edit
    echo        -h  Help
    echo [Keyword]  Popup showing all windows matching [Keyword]
Goto End

:Edit
    subl "%~f0"
    ::echo %ErrorLevel%
Goto End

:RunAhk
    Start D:\Portable\AutoHotKey\AutoHotKey "%~f0" %1%
:End

(Why's the syntax highlighting not working?)