I've noticed, though, that if I exit the console window directly, the AHK script just sits there in the tray, and clicking on Close in the right click menu doesn't do anything. I have to run the script again (causing it to run script.bat again), then click on Close in the context menu. The last part of the script is supposed to prevent this from happening, by checking every second if cmd is running, then exiting itself when it finds that it isn't. What am I doing wrong?
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#NoTrayIcon
#Persistent
#SingleInstance force
global script
/* Setup Tray icon and add item that will handle
* double click events
*/
Menu Tray, NoStandard
Menu Tray, Icon
Menu Tray, Icon, icon.ico
Menu Tray, Add, Show / Hide, TrayClick
Menu Tray, Add, Close, CloseItem
Menu Tray, Default, Show / Hide
;// Run program or batch file hidden
DetectHiddenWindows On
Run script.bat,, Hide, PID
WinWait ahk_pid %PID%
script := WinExist()
DetectHiddenWindows Off
return
TrayClick:
OnTrayClick()
return
;// Show / hide program or batch file on double click
OnTrayClick() {
if DllCall("IsWindowVisible", "Ptr", script) {
WinHide ahk_id %script%
} else {
WinShow ahk_id %script%
WinActivate ahk_id %script%
}
}
CloseItem() {
DetectHiddenWindows On
WinWait ahk_class ConsoleWindowClass
Process, Close, cmd.exe
DetectHiddenWindows Off
ExitApp
}
SetTimer, Closer, 1000
Closer:
Process, Exist, cmd.exe
If !ErrorLevel=0
ExitApp
Return