Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active October 13, 2023 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davebrny/c186fb1e1a31e40cbe7c0fa47b865d22 to your computer and use it in GitHub Desktop.
Save davebrny/c186fb1e1a31e40cbe7c0fa47b865d22 to your computer and use it in GitHub Desktop.
(autohotkey) - close a window then try more forceful methods if the window still exists
#noEnv
#persistent
#singleInstance, force
close_types := "winClose | postMessage | winKill | process"
hotkey, !F4, close_window_f
; hotkey, #q, close_window_f
return ; end of auto-execute ---------------------------------------------------
close_window_f:
winGet, active_window_ID, ID, a
a_id := "ahk_id " active_window_ID
loop, parse, % close_types, |, % a_space
{
close_type := a_loopField
if winExist(a_id)
goSub, % close_type
else
break
sleep 500
}
return
winClose:
winClose, % a_id ; sends WM_Close message
return
postMessage:
postMessage, 0x112, 0xF060, , , % a_id ; sends SC_Close message
return
winKill:
winKill, % a_id ; attempts WM_Close first, then attempts to kill the process
return
process:
winGet, active_PID, PID, % a_id
process, close, % active_PID ; calls TerminateProcess function
return
/*
[script info]
version = 1
description = close a window then try more forceful methods if the window still exists
author = davebrny
source = https://gist.github.com/davebrny/c186fb1e1a31e40cbe7c0fa47b865d22
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment