Kill the active window Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Kill the active window

07 Apr 2021, 05:35

I open a lot of programs using hotkeys and hotstrings and I use them all the time, all very efficient for me (I use "xx" as a prefix so that I would never accidentally hit these). However, once they are open, I do something in them and I would like to close that app. For things like cmd or powershell, that's easy, I type 'exit', but for some, it can be odd key combo's etc, so, is there a way in AutoHotkey that I could say "kill the ACTIVE window"? Obviously, I don't want to hard kill it if there is work open, so, first it should try "Alt => F => C" (works for Explorer), or "Alt => F => X" (works for other apps like Notepad), and only if these fail should it a) get the process of the active window, then kill the active process (probably I'll prompt for confirmation for that to avoid losing work). This would be really useful for me in managing apps opening and closing, would appreciate any ideas on how to do this. :)

Code: Select all

IfExistThenRun(myPath) {
    IfExist, %myPath%
        Run, %myPath%
}
; This is not good enough, expand this to look in multiple locations, maybe up to 4 arguments, test each in order until get a hit then use that to open.
; Also, possibly should open existing app if already open? e.g. if existing app is found to be open, just ask "Open new instance, or use existing instance?" if so.

:x*:xxpowershell::
:x*:xxps::Run, powershell.exe
:x*:xxpw::   ; "pw" short for "pwsh"
:x*:xxp7::IfExistThenRun("C:\Program Files\PowerShell\7\pwsh.exe")
:x*:xxp6::IfExistThenRun("C:\Program Files\PowerShell\6\pwsh.exe")
:x*:xxp8::IfExistThenRun("C:\Program Files\PowerShell\8\pwsh.exe")   ; pw should open latest available!
:x*:xxcmd::Run, cmd.exe
:x*:xxcalc::Run, calc.exe
:x*:xxsp::IfExistThenRun("C:\Program Files (x86)\SpeedCrunch\speedcrunch.exe")
:x*:xxword::Run, winword.exe
:x*:xxexcel::Run, excel.exe
:x*:xxoutlook::Run, outlook.exe
:x*:xxmspaint::
:x*:xxpaint::Run, mspaint.exe
:x*:xxedge::IfExistThenRun("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
:x*:xxie::IfExistThenRun("C:\Program Files (x86)\Internet Explorer\iexplore.exe")
:x*:xxmpc::IfExistThenRun("C:\Program Files\MPC-HC\mpc-hc64.exe")
:x*:xxbc::
:x*:xxbeyondcompare::IfExistThenRun("C:\Program Files\Beyond Compare 4\bcompare.exe")
:x*:xxchrome::("C:\Program Files\Google\Chrome\Application\chrome.exe")
:x*:xxchium::("C:\Program Files\Chromium\Application\chromium.exe")
:x*:xxsteam::IfExistThenRun("C:\Program Files (x86)\Steam\steam.exe")
:x*:xxgpuz::IfExistThenRun("C:\Program Files (x86)\GPU-Z\GPU-Z.exe")
:x*:xxtreesize::IfExistThenRun("C:\Program Files (x86)\JAM Software\TreeSize Free\TreeSizeFree.exe")
:x*:xxanydesk::IfExistThenRun("C:\Program Files (x86)\AnyDesk\AnyDesk.exe")
:x*:xxzenmap::IfExistThenRun("C:\Program Files (x86)\Nmap\zenmap.exe")
:x*:xxscp:: 
:x*:xxwinscp::IfExistThenRun("C:\Program Files (x86)\WinSCP")
:x*:xxputty::IfExistThenRun("C:\ProgramData\chocolatey\bin\putty.exe")
:x*:xxcode::Run, code.exe
:x*:xxnpp:: 
:x*:xxnotepad::IfExistThenRun("C:\Program Files\Notepad++\notepad++.exe")
:x*:xx7z::IfExistThenRun("C:\Program Files\7-Zip\7zFM.exe")
:x*:xxpython::IfExistThenRun("C:\Python39\python.exe")
:x*:xxgit::IfExistThenRun("C:\Program Files\Git\git-bash.exe")
:x*:xxbash::Run, bash.exe

; System Apps
:x*:xxdiskman::Run, diskmgmt.msc   ; Disk Management
:x*:xxcontrol::Run, control.exe    ; Control Panel
; https://www.howtogeek.com/717570/how-to-use-microsoft-defender-antivirus-from-command-prompt-on-windows-10/
; MpCmdRun -Scan -ScanType 1   ; Quick
; MpCmdRun -Scan -ScanType 2   ; Full
; MpCmdRun -Scan -ScanType 3 -File PATH   ; Custom, only scan a specific path
; MpCmdRun -Scan -ScanType -BootSectorScan
; MpCmdRun -Restore -ListAll          ; List all quarantined files
; MpCmdRun -Restore -Name MyApp.exe   ; Restore a specific file
; MpCmdRun -Restore -Name MyApp.exe -FilePath PATH   ; Restore to a different location
; MpCmdRun -SignatureUpdate    ; Update Signatures now
; MpCmdRun -h
; Latest version might also be here: C:\Program Files\Windows Defender
:x*:xxdefender1::
; try and convert the following from PowerShell over to AutoHotkey ...
; ("ls C:\ProgramData\Microsoft\Windows Defender\Platform\ |sort CreationTime -Descending|select -Expand FullName -First 1")
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Kill the active window

07 Apr 2021, 09:58

@roysubs
maybe for your question , if program already run , not run again , just activate minimized program
splitpath maybe not works for all programs , file-name must be same as process-name

Code: Select all

;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=89035
;F3::WinClose, A

;- alt+F4 closes also the active window  ( !F4 )

IfExistThenRun(myPath) 
 {
 SplitPath, mypath, name, dir, ext, name_no_ext, drive
 Process, Exist,%name%      
 cmdPid := ErrorLevel
 if (cmdpid=0)
   {
   try
     {
     run,%mypath%      ; run program if not exist
     return
     }
   }
 else
   {
   ;- show/activate minimized program
   winactivate,ahk_pid %cmdPid%
   WinWaitactive, ahk_pid %cmdPid%
   }
 }
;---------------------------------------------------------------------------
:x*:fox::IfExistThenRun("C:\Program Files\Mozilla Firefox\firefox.exe")
:x*:char::IfExistThenRun("charmap.exe")
:x*:xxps::IfExistThenRun("powershell.exe")
roysubs
Posts: 426
Joined: 29 Sep 2018, 16:37

Re: Kill the active window

11 Apr 2021, 01:52

Thanks Garry, that's great, I'm going to use this also! :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AHK_user, Google [Bot], sharonhuston and 200 guests