HowTo: Building a Master Tray Icon to Edit/Suspend/Restart/Quit a Specific Group of Scripts

Helpful script writing tricks and HowTo's
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

HowTo: Building a Master Tray Icon to Edit/Suspend/Restart/Quit a Specific Group of Scripts

11 Feb 2023, 04:01

For imkira3Keys v4 I decided to hide the tray icons for AutoHotkey64 and AutoHotkey32 and create a master icon to control everything. This is how I did it.

The goal here was to create 5 sections in a customized tray icon, (About/Edit/Suspend/Restart/Quit) you can use this to either control multiple scripts with a single tray icon like it was a single program, or simply to manage your own group of personal scripts, in which case you can ignore the About section. Aside from Suspend we are gonna do it all externally because I like the idea of having an external app that can suspend, restart or quit all the scripts, it gives us more options. So were gonna create some mini-apps, some converted from batch files and some converted from AHK files. You can use the icons I made or create your own, I'll place links for everything in this thread at the bottom of the post. Use Bat to Exe Converter and the icons provided to convert your batch files, the AHK files can be converted with icons using ahk2exe which can be found in your AutoHotkey v2 installation folder. The VBS file does not need to be converted. All files go in your working directory, including AutoHotkey64.exe and AutoHotkey32.exe (v2.0.2).

About.bat

The only point of this is to add an icon to the Start Menu which provides the user with basic information on your program, so if your not interested in creating a shortcut to this then you might as well open the VBS file directly.
      Code: Download
  1. start "" About.vbs
About.vbs

This provides the user with basic information on your program.
      Code: Download
  1. x=msgbox("PROGRAMVERSION" & vbNewLine & "Copyright © YEAR NAME" & vbNewLine & "HomePage: LINK" ,64, "About PROGRAMNAME")
Edit.bat

This will open your scripts for editing.
      Code: Download
  1. start "" notepad.exe SCRIPTPATH.ahk
  2. start "" notepad.exe SCRIPTPATH.ahk
Quit.ahk

This will close all your scripts in a manner that will allow fluent running of your exit scripts.
      Code: Download
  1. #NoTrayIcon
  2. DetectHiddenWindows True
  3. A_Command := 0x0111
  4. A_Quit := 65405
  5. PostMessage A_Command, A_Quit,,, "SCRIPTNAME.ahk ahk_class AutoHotkey"
  6. DetectHiddenWindows True
  7. A_Command := 0x0111
  8. A_Quit := 65405
  9. PostMessage A_Command, A_Quit,,, "SCRIPTNAME.ahk ahk_class AutoHotkey"
  10. DetectHiddenWindows True
  11. A_Command := 0x0111
  12. A_Quit := 65405
  13. PostMessage A_Command, A_Quit,,, "COMPILEDSCRIPTNAME.exe ahk_class AutoHotkey"
Restart.ahk

This will restart all your scripts in a manner that will allow fluent running of your exit scripts.
      Code: Download
  1. #NoTrayIcon
  2. try Run 'PATH\Quit.exe'
  3. Sleep 5000
  4. try Run 'PATH\TrayApp.exe'
Launcher.bat

This will launch multiple scripts at once and wait to launch the next commands until AutoHotkey closes. After AutoHotkey closes it can kill additional software your scripts have running.
      Code: Download
  1. start "" /w AutoHotkey64.exe SCRIPTNAME.ahk | AutoHotkey32.exe SCRIPTNAME.ahk
  2. taskkill /f /im PROGRAMNAME.exe
  3. taskkill /f /im PROGRAMNAME.exe
  4. taskkill /f /im PROGRAMNAME.exe
  5. taskkill /f /im PROGRAMNAME.exe
  6. exit
TrayApp.ahk

And now we will create the main tray app. The script for suspending your scripts must be contained here so we can change the icon to reflect your scripts being suspended even as the tray app doesn't get suspended. For this to work, place Enabled.ico and Disabled.ico in your working directory.
      Code: Download
  1. try SetWorkingDir "PATH"
  2. tray := A_TrayMenu
  3. A_Suspended := "0"
  4. A_TrayMenu.delete("&Suspend Hotkeys")
  5. A_TrayMenu.delete("&Pause Script")
  6. A_TrayMenu.delete("E&xit")
  7. A_TrayMenu.Add("About", A_MenuHandler1)
  8. Persistent
  9. A_MenuHandler1(ItemName, ItemPos, MyMenu) {
  10. try Run 'PATH\About.exe'
  11. }
  12. A_TrayMenu.Add("Edit", A_MenuHandler2)
  13. Persistent
  14. A_MenuHandler2(ItemName, ItemPos, MyMenu) {
  15. try Run 'PATH\Edit.exe'
  16. }
  17. A_TrayMenu.Add("Suspend", A_MenuHandler3)
  18. Persistent
  19. A_MenuHandler3(ItemName, ItemPos, MyMenu) {
  20. DetectHiddenWindows True
  21. A_Command := 0x0111
  22. A_Suspend := 65404
  23. PostMessage A_Command, A_Suspend,,, "SCRIPTNAME.ahk ahk_class AutoHotkey"
  24. DetectHiddenWindows True
  25. A_Command := 0x0111
  26. A_Suspend := 65404
  27. PostMessage A_Command, A_Suspend,,, "SCRIPTNAME.ahk ahk_class AutoHotkey"
  28. global A_Suspended
  29. if A_Suspended = "0"{
  30. A_Suspended := "1"
  31. try TraySetIcon "Disabled.ico"
  32. }else{
  33. A_Suspended := "0"
  34. try TraySetIcon "Enabled.ico"
  35. }
  36. }
  37. A_TrayMenu.Add("Restart", A_MenuHandler4)
  38. Persistent
  39. A_MenuHandler4(ItemName, ItemPos, MyMenu) {
  40. try Run 'PATH\Restart.exe'
  41. }
  42. A_TrayMenu.Add("Quit", A_MenuHandler5)
  43. Persistent
  44. A_MenuHandler5(ItemName, ItemPos, MyMenu) {
  45. try Run 'PATH\Quit.exe'
  46. }
  47. try Run 'PATH\Launcher.exe'

Finally, once your scripts are completed and fully tested, add this line to your scripts to hide their tray icons.

Code: Select all

#NoTrayIcon

Download Bat to Exe Converter: https://www.mediafire.com/file/5muq1k44nipshgy/Bat_to_Exe_Converter.exe
Download Icons: https://app.mediafire.com/uaw07bo8uh409
Download Icons (Zipped): https://www.mediafire.com/file/zvvakm5tmxms5jn/Icons.zip


[Mod action: Moved topic from “AutoHotkey v2 Scripts and Functions”]
MonkCanatella
Posts: 3
Joined: 22 Feb 2023, 16:10

Re: HowTo: Building a Master Tray Icon to Edit/Suspend/Restart/Quit a Specific Group of Scripts

26 Feb 2023, 13:43

I'm a little confused how this works? I ran it in the same directory as all my startup scripts. After running it, none of my startup scripts were running. Can you add some more context as to how to utilize this? As of now I'm still quite confused.

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 125 guests