tray.ahk 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. ; #Warn ; Enable warnings to assist with detecting common errors.
  3. SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
  4. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
  5. #NoTrayIcon
  6. #Persistent
  7. #SingleInstance force
  8. global script
  9. /* Setup Tray icon and add item that will handle
  10. * double click events
  11. */
  12. Menu Tray, NoStandard
  13. Menu Tray, Icon
  14. Menu Tray, Icon, icon.ico
  15. Menu Tray, Add, Show / Hide, TrayClick
  16. Menu Tray, Add, Close, CloseItem
  17. Menu Tray, Default, Show / Hide
  18. ;// Run program or batch file hidden
  19. DetectHiddenWindows On
  20. Run script.bat,, Hide, PID
  21. WinWait ahk_pid %PID%
  22. script := WinExist()
  23. DetectHiddenWindows Off
  24. return
  25. TrayClick:
  26. OnTrayClick()
  27. return
  28. ;// Show / hide program or batch file on double click
  29. OnTrayClick() {
  30. if DllCall("IsWindowVisible", "Ptr", script) {
  31. WinHide ahk_id %script%
  32. } else {
  33. WinShow ahk_id %script%
  34. WinActivate ahk_id %script%
  35. }
  36. }
  37. CloseItem() {
  38. DetectHiddenWindows On
  39. WinWait ahk_class ConsoleWindowClass
  40. Process, Close, cmd.exe
  41. DetectHiddenWindows Off
  42. ExitApp
  43. }
  44. SetTimer, Closer, 1000
  45. Closer:
  46. Process, Exist, cmd.exe
  47. If !ErrorLevel=0
  48. ExitApp
  49. Return