AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post your working scripts, libraries and tools for AHK v1.1 and older
likethevegetable
Posts: 81
Joined: 05 May 2021, 08:54

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by likethevegetable » 16 Aug 2022, 12:03

This is a great script that I find useful. I tried to add the Reload option by adding `Menu, SubMenu_%PID%, Add, &Reload, ScriptCommand` and `Cmd_Reload = 65400` in the appropriate spots, but it seems to only reload the script the first time I click it.. I'm guessing this is becuase the main script is not catching the relaoded script's PID and indexing it? Any pointers?

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 16 Aug 2022, 14:04

You pretty accurately descripted the problem which is the reason I did not include Reload in the menu.

When you reload a script, it is basically just stopping the current script and running the new script. The new script run is almost like any other script you might start externally to AHK Startup. There is no real connection between the old and new.

As I think about it now though, a Reload could probably be simulated by stopping the old script yourself and starting the new script with Run using the OutputVarPID. Then you would have to substitute that new PID into the index. It actually does not seem that hard, but I have not looked at this code in years so there might be other complications.

You can also click on 'Reload This Script' from the menu which will reload AHK Startup which will effectively reload all of its list of scripts.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

likethevegetable
Posts: 81
Joined: 05 May 2021, 08:54

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by likethevegetable » 31 Aug 2022, 11:17

Thank you for the reply--my work around has been to reload all scripts. Cheers!

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 26 Sep 2022, 17:35

Updated in First Post

Change Log: Update 2022 09 26
  • Added ability to Reload, Load, and Exit scripts
The Reload is not a true reload. In order to track the PID of the reloaded script, the original script is closed and then Run again with the OutputVarPID option. This is effectively a reload but not exactly the same.

If a script is closed with the "Exit" submenu item for that script, then the script is moved to the "Load" menu. Then "Load" can be used to start the script again. Effectively allowing a script to be loaded and unloaded over and over as needed.

In the list of scripts in the code, the flag "/noload" can be used after a script name to not run the script but include it in the "Load" submenu. I use "/noload" on scripts that I want easy access to but do not actually want to have running all the time. A script is only moved to the "Load" submenu if exited through the menu. If a script is closed directly or closes itself, then it is not added to the "Load" submenu. When a script is closed through the menu, "AHK Startup" knows it is being closed. If closed other ways, then there would have to be some type of monitoring that is constantly watching for certain scripts to close. This is very possible, especially with my other script WinHook but it would complicate this code significantly and I am not sure it is worth it.

There does appear to sometimes be a timing issue when a script you Load not having its icon removed. It is an issue with the new icon being in limbo for a bit when it appears in the "outside" visible tray and then gets moved to the "inside" hidden tray. Maybe I can do something to resolve this issue in the future.

I was kind of hesitant to post this update as it required making fairly major changes to the code as I changed the structure of the main array that stored everything about the scripts being managed. This required changes throughout the code in numerous places. I have been running the code for several weeks but have not really tested it out much and tried to break it. Hopefully it will hold up to other peoples use.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

jamez
Posts: 28
Joined: 18 Nov 2016, 04:29

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by jamez » 18 Oct 2022, 08:35

Super useful script! Works like a charm!

The only thing: if you reload a script via this script's tray menu, then it adds one separator line to every other script's menu (see picture)
image.png
image.png (13.75 KiB) Viewed 2404 times
It does not add that line to that script's menu that was reloaded. If you reload a different script then that script's menu's extra separator lines are removed (every other menu gets one more separator line added).

Reloading the main script removes all the extra separator lines.

JohnCamara
Posts: 8
Joined: 04 Nov 2022, 10:31

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by JohnCamara » 04 Nov 2022, 10:37

Unfortunately, this latest version doesn't work for me. I'm getting this error

"Error: Submenu does not exist.

Specifically: SubMenu_Load

Line#
...
...
...
--->163: Menu,Tray,Add,Load,:SubMenu_Load
...
...

The current thread will exit."

jamez
Posts: 28
Joined: 18 Nov 2016, 04:29

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by jamez » 04 Nov 2022, 11:25

The easiest fix is to add at least one script with /noload

Also, I added Pause and Suspend Hotkeys to the menu:

Code: Select all

		
			...
			Menu, SubMenu_%PID%, Add, &Edit, ScriptCommand
			Menu, SubMenu_%PID%, Add, &Pause, ScriptCommand
			Menu, SubMenu_%PID%, Add, &Suspend Hotkeys, ScriptCommand
			Menu, SubMenu_%PID%, Add, &Reload, ScriptCommand
			...
And in ScriptCommand I changed else to:

Code: Select all

	...
	else
	{
	    if (cmd = "Pause") or if (cmd = "SuspendHotkeys")
			Menu, SubMenu_%Pid%, ToggleCheck, %A_ThisMenuItem%
		cmd := Cmd_%cmd%
		PostMessage, 0x111, %cmd%,,,ahk_pid %Pid%
	}
	...

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 04 Nov 2022, 19:20

Updated in First Post

Change Log: Update 2022 11 04
  • Fixed fatal bug if no files had the "/noload" flag that resulted in a crash when attempting to add a null Load submenu
  • Fixed blank lines accumulating at the bottom of tray submenu as scripts were loaded and unloaded
The fatal "/noload" bug was just total lack of decent testing. I was so concentrated on getting "/noload" working that once I did, I never tried not using it.

I tried adding in "Suspend" and "Pause". At first, I thought this is simple and works great. Wondered why I did not include these in the first place. Then after some testing this time, I realized the problem. When something needs to be removed from the tray menu, the entire menu and submenus are deleted and then rebuilt. This causes the status of the checkmark for "Suspend" and "Pause" to be lost as the status is not actually stored, just toggled back and forth in the menu. At some point I will probably see about working around this issue so that "Suspend" and "Pause" can be added without the checkmarks getting out of sync.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

JohnCamara
Posts: 8
Joined: 04 Nov 2022, 10:31

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by JohnCamara » 05 Nov 2022, 00:26

Not getting the error anymore. Although I still get multiple script icons on my taskbar. Exiting the main StartUp script also does not close the other scripts I asked it to load. I'm not sure if it's a problem with the scripts I'm loading or if it's the main startup script itself.

jamez
Posts: 28
Joined: 18 Nov 2016, 04:29

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by jamez » 07 Nov 2022, 04:20

FanaticGuru wrote:
04 Nov 2022, 19:20
I tried adding in "Suspend" and "Pause". At first, I thought this is simple and works great. Wondered why I did not include these in the first place. Then after some testing this time, I realized the problem. When something needs to be removed from the tray menu, the entire menu and submenus are deleted and then rebuilt. This causes the status of the checkmark for "Suspend" and "Pause" to be lost as the status is not actually stored, just toggled back and forth in the menu. At some point I will probably see about working around this issue so that "Suspend" and "Pause" can be added without the checkmarks getting out of sync.
Yes, true! Didn't really test that much. So I thought adding Pause and Suspend statuses. Then I realized that you have another great script - Hotkey Help - that can read other script's pause and suspend status. That seemed more interesting. So I made a function that uses a part of that script.
I made 3 changes:
1) added call to Function PausedOrSuspended to MenuBuild

Code: Select all

	for Script_Name, Script in Scripts
		if Script.Status
		{
			PID := Script.PID
			try Menu, SubMenu_%PID%, DeleteAll
			Menu, SubMenu_%PID%, Add, View Lines, ScriptCommand
			Menu, SubMenu_%PID%, Add, View Variables, ScriptCommand
			Menu, SubMenu_%PID%, Add, View Hotkeys, ScriptCommand
			Menu, SubMenu_%PID%, Add, View Key History, ScriptCommand
			Menu, SubMenu_%PID%, Add
			Menu, SubMenu_%PID%, Add, &Open, ScriptCommand
			Menu, SubMenu_%PID%, Add, &Edit, ScriptCommand
			Menu, SubMenu_%PID%, Add, &Pause, ScriptCommand ;added command
			Menu, SubMenu_%PID%, Add, &Suspend Hotkeys, ScriptCommand ;added command
			Menu, SubMenu_%PID%, Add, &Reload, ScriptCommand
			Menu, SubMenu_%PID%, Add, &Exit, ScriptCommand
			Menu, Tray, Add, %Script_Name%, :SubMenu_%PID%
			PausedOrSuspended(Script_Name, PID) ;added function
		}
2) added MenuBuild to else in scriptcommand:

Code: Select all

else
	{
		cmd := Cmd_%cmd%
		PostMessage, 0x111, %cmd%,,,ahk_pid %Pid%
		if (cmd = "65403") or if (cmd = "65404") ;Pause or Suspend
			gosub MenuBuild
	}
3) added function PausedOrSuspended:

Code: Select all

PausedOrSuspended(Script_Name, PID) {
SetTitleMatchMode, 2
WinGet, script_id, ID, \%Script_Name%.ahk - ahk_class AutoHotkey

		SendMessage, 0x211,,,, ahk_id %script_id%  ; WM_ENTERMENULOOP
		SendMessage, 0x212,,,, ahk_id %script_id%  ; WM_EXITMENULOOP

		; Get script status from its main menu.
		mainMenu := DllCall("GetMenu", "uint", script_id)
		fileMenu := DllCall("GetSubMenu", "uint", mainMenu, "int", 0)
		isPaused := DllCall("GetMenuState", "uint", fileMenu, "uint", 4, "uint", 0x400) >> 3 & 1
		isSuspended := DllCall("GetMenuState", "uint", fileMenu, "uint", 5, "uint", 0x400) >> 3 & 1
		DllCall("CloseHandle", "uint", fileMenu)
		DllCall("CloseHandle", "uint", mainMenu)

if isPaused
	Menu, SubMenu_%PID%, Check, &Pause
if isSuspended
	Menu, SubMenu_%PID%, Check, &Suspend Hotkeys		
;3 - default suspend icon, 4 - default pause icon, 5 - default suspend and pause icon
if isPaused && isSuspended
	ikoon := 5
else if isPaused
	ikoon := 4
else if isSuspended
	ikoon := 3

if ikoon
	Menu, Tray, Icon, %Script_Name%, %A_AhkPath%, %ikoon%

sleep 1
}
Function PausedOrSuspended also adds respective icon to the menu before script's name.

Everything seems to be working so far. The line sleep 1 is required. Without it the last script always gets marked as paused and suspended. Other commands also work inconsistently: sometimes refreshing a script gets marked as paused and suspended, sometimes not. Sometimes launching a script from Load menu gets marked as paused and suspended. Cant figure out why. Works fine with sleep.

I have to admit I don't know what >> 3 & 1 means at the end of DllCall. I have seen this line written like this also:
(DllCall("GetMenuState", "uint", hMenu, "uint", 4, "uint", 0x400) & 0x8)!= 0

I know that isPaused and IsSuspended will aways be 1 if the script_id is missing (if the script was closed not using the main script).
I tried to check if scripts PID is the same as stored in the array scripts using:

Code: Select all

WinGet, checkPID, PID, \%ScriptName%.ahk - ahk_class AutoHotkey
or

Code: Select all

WinGet, checkPID, PID, ahk_id %ID%
But for some reason checkPID is always blank. Don't know why.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 07 Nov 2022, 12:10

Updated in First Post

Change Log: Update 2022 11 07
  • Fixed bug introduced by the new 'Launcher' that is installed with AutoHotkey v2
AHK Startup uses the Run command to start scripts and get the PID of the newly run script back from that command. AutoHotkey v2 introduced a 'Launcher' which with the default settings is run anytime an attempt is made to run a script. This 'Launcher' then figures out which version of AutoHotkey to use based on analyzing the scripts code to determine if it is v1 or v2 code. Then the Launcher runs the script with the correct version of AutoHotkey. This causes AHK Startup to only get the PID of the Launcher and not of the actual scripts. AHK Startup resolves this issue by specifying which version of AutoHotkey to use to Run a script there by circumventing the Launcher.

The version of AutoHotkey used to Run all the scripts is the same version of AutoHotkey used to run AHK Startup.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

JohnCamara
Posts: 8
Joined: 04 Nov 2022, 10:31

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by JohnCamara » 09 Nov 2022, 08:52

Small bug with the latest one, changing the display resolution will cause multiple AHK icons to appear. Have to reload the master script in order for the others to hide again.

MonkCanatella
Posts: 3
Joined: 22 Feb 2023, 16:10

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by MonkCanatella » 22 Feb 2023, 16:26

Is there any version of this that launches both v1 and v2 scripts? I have a combination of both and this is causing an infinite loop of ahk errors.

MonkCanatella
Posts: 3
Joined: 22 Feb 2023, 16:10

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by MonkCanatella » 23 Feb 2023, 19:46

I've figured out why I was having an infinite loop: I had the script in the same folder as the other scripts I wanted to run!

I am having an issue though because I have v1 and v2 scripts that I'd like consolidated, so it's throwing errors for the v2 scripts. Is there any way around this?

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by LAPIII » 24 Feb 2023, 14:12

Could someone please help me set this up? I use AHK v2 and I've got several folders that I keep scripts in, so if it's possible here then I'll need a variable. In my scripts I use #NoTrayIcon.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 28 Feb 2023, 17:03

Updated in First Post

Change Log: Update 2023 02 28
  • Added flags for running scripts with v1 or v2 versions of AutoHotkey.exe
  • Added additional cleanup of tray icons on screen resolution change and anytime the AHK Startup tray icon is moused over

AHK Startup is a v1 script but it can start v2 scripts by adding the appropriate "/v2" flag after the filename in the initialization section where the list of scripts to run is defined. The script assumes the AutoHotkey.exe is in the standard folder location of A_ProgramFiles "\AutoHotkey\v2\AutoHotkey.exe" but this can be changed if needed.

Also included some additional tray icon cleanup. For various reasons Windows does not always communicate promptly how to find new tray icons. First, they can take a moment to appear, then when they do, they can be in limbo for a bit as Windows displays them more obviously before moving them to their final destination. When AHK expects new tray icons to appear, the script tries to hide them several times over like 30 seconds. But still some icons can escape detection in the time frame. So, I added in a catch all that any time the AHK Startup tray icon is moused over it will attempt to hide all icons that it is supposed to be hiding. This uses an event hook with OnMessage so it is not constantly checking, it has just told Windows that during your normal housekeeping to let the script know when these things occur. This should at least make capturing stray icons easier when it does occur. Used a similar hook for the screen resolution change. Windows apparently rebuilds the entire desktop interface similar to bootup when the screen resolution changes. So just like at bootup, AHK Startup needs to hide all the necessary tray icons.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

goldfish1063
Posts: 4
Joined: 12 Mar 2023, 06:51

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by goldfish1063 » 12 Mar 2023, 06:58

Hi @FanaticGuru
Thank you for this amazing script. I have been using it since long on Windows 11.
However recently, I guess after a windows update, the script is not working as it should. It is able to run successfully all the autohotkey scripts placed in my desired folder. Though, its failing to exit the already running scripts after combining their icons.

I searched for this thread and saw the updated version of your script. I had to manually #Include pathto\trayicon.ahk as it threw an error without that. Also the latest version is still not able to close the script icons when they are run successfully. How can this be fixed?

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 13 Mar 2023, 12:24

goldfish1063 wrote:
12 Mar 2023, 06:58
I searched for this thread and saw the updated version of your script. I had to manually #Include pathto\trayicon.ahk as it threw an error without that. Also the latest version is still not able to close the script icons when they are run successfully. How can this be fixed?

The script is working fine for me on Windows 11. It probably has to do with the TrayIcon library. I edited the first post and included just the necessary functions from the library in the script so it does not have a library dependency. Hopefully that helps.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

goldfish1063
Posts: 4
Joined: 12 Mar 2023, 06:51

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by goldfish1063 » 14 Mar 2023, 12:44

FanaticGuru wrote:
13 Mar 2023, 12:24
The script is working fine for me on Windows 11. It probably has to do with the TrayIcon library. I edited the first post and included just the necessary functions from the library in the script so it does not have a library dependency. Hopefully that helps.
I tried the updated version and indeed it works now without the TrayIcon library. However, the script doesnot close the running scripts on combining their icons.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK Startup (Consolidate AHK Scripts' Tray Icons)

Post by FanaticGuru » 14 Mar 2023, 19:23

goldfish1063 wrote:
14 Mar 2023, 12:44
I tried the updated version and indeed it works now without the TrayIcon library. However, the script doesnot close the running scripts on combining their icons.

I am unsure of your comment, "However, the script doesnot close the running scripts on combining their icons." It is not supposed to do this.

When AHK Startup runs it starts other scripts then hides their tray icons and provides access to many of those individual scripts icon functions into its own tray icon menu. There by uncluttering the tray icons. I typically have about 15 scripts running and do not want 15 individual tray icons for my AHK scripts.

It does not close the other scripts when it combines their tray icons. The scripts are still running and functioning like normal. Only when AHK Startup stops running does it then stop all the scripts it started. So, in effect it makes a list of scripts start and stop like one script but there are still multiple scripts and multiple processes running.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Post Reply

Return to “Scripts and Functions (v1)”