Changing Tray Icon when script is paused
Changing Tray Icon when script is paused
I have googled a bit, but I'm not able to find how to do this. I need custom icon when script is paused/suspended. I know how to change it on key press, but I need same functionality like when you suspend it with right mouse click in System Tray. When you do that standard AHK icon will have red background, if paused and it will change letter to "S" if suspended. My script needs to change to custom icon when it's paused/suspended, when you right-click on the icon in Tray and suspend it. No key press activation needed, eventually script need to detect when I pause/resume it and change to different custom icons.
Re: Changing Tray Icon when script is paused
There are two ways to know when the script is (un)paused or (un)suspended:
To change the icon, use the Menu command. Specify 1 for the last parameter to "freeze" the icon, otherwise your icon will be ignored if the script is suspended or paused.
- Be the one who does it; i.e. if you just called Suspend, you know that A_IsSuspended just changed. You can remove the standard tray menu items and add your own which do whatever you want (including changing the icon) by using the Menu command.
- Monitor the messages which are posted when the user selects one of the menu items. These are not posted when you call Pause or Suspend directly. Menus work by posting a WM_COMMAND message (message number 0x111) with the wParam parameter containing the menu item ID, which the program uses to determine which action to take. The menu item IDs are defined in the AutoHotkey source code, in resource.h for the main window and script.h for the tray menu. For example:
Code: Select all
OnMessage(WM_COMMAND := 0x111, "OnMenuCommand")
OnMenuCommand(wParam)
{
switch wParam
{
case 65403, 65306: ; ID_FILE_PAUSE, ID_TRAY_PAUSE
if A_IsPaused
MsgBox Script is being unpaused.
else
MsgBox Script is being paused.
case 65404, 65305: ; ID_FILE_SUSPEND, ID_TRAY_SUSPEND
if A_IsSuspended
MsgBox Script is being unsuspended.
else
MsgBox Script is being suspended.
}
; The change to pause/suspend status takes effect only after this function returns.
}
To change the icon, use the Menu command. Specify 1 for the last parameter to "freeze" the icon, otherwise your icon will be ignored if the script is suspended or paused.
Re: Changing Tray Icon when script is paused
lexikos wrote: ↑14 Aug 2020, 03:43There are two ways to know when the script is (un)paused or (un)suspended:
- Be the one who does it; i.e. if you just called Suspend, you know that A_IsSuspended just changed. You can remove the standard tray menu items and add your own which do whatever you want (including changing the icon) by using the Menu command.
- Monitor the messages which are posted when the user selects one of the menu items. These are not posted when you call Pause or Suspend directly. Menus work by posting a WM_COMMAND message (message number 0x111) with the wParam parameter containing the menu item ID, which the program uses to determine which action to take. The menu item IDs are defined in the AutoHotkey source code, in resource.h for the main window and script.h for the tray menu. For example:
Code: Select all
OnMessage(WM_COMMAND := 0x111, "OnMenuCommand") OnMenuCommand(wParam) { switch wParam { case 65403, 65306: ; ID_FILE_PAUSE, ID_TRAY_PAUSE if A_IsPaused MsgBox Script is being unpaused. else MsgBox Script is being paused. case 65404, 65305: ; ID_FILE_SUSPEND, ID_TRAY_SUSPEND if A_IsSuspended MsgBox Script is being unsuspended. else MsgBox Script is being suspended. } ; The change to pause/suspend status takes effect only after this function returns. }
To change the icon, use the Menu command. Specify 1 for the last parameter to "freeze" the icon, otherwise your icon will be ignored if the script is suspended or paused.
Thanks for the response.
I have copied you code in my script, but when I click pause or suspended there is no pop-up message.
I would like to capture event when you click suspend or pause via right-click on the tray icon. Then I could change tray icon of the script.
Re: Changing Tray Icon when script is paused
I think if you add #Warn Unreachable (which requires AutoHotkey v1.1.33+) to your script, you will find that the OnMessage() line is unreachable and therefore never actually being executed.
https://www.autohotkey.com/docs/Scripts.htm#auto
There is no benefit to having a post repeated verbatim in a quote within the very next post. It is better to use the "post reply" button rather than the quote button when replying to the last post in a topic.
https://www.autohotkey.com/docs/Scripts.htm#auto
There is no benefit to having a post repeated verbatim in a quote within the very next post. It is better to use the "post reply" button rather than the quote button when replying to the last post in a topic.
Re: Changing Tray Icon when script is paused
I still do not understand what I need to do xD.
Re: Changing Tray Icon when script is paused
I tested the code before posting. It works, so I assume that you have pasted the code into an existing script, at a point where OnMessage will never be called.
For example,This will show a message, right?
It won't show a message until the command is called.
This behaviour is partially explained by the link I posted before. However, you still need a basic understanding of control flow.
https://www.autohotkey.com/docs/Concepts.htm#control-flow
For example,
Code: Select all
MsgBox Hello, world!
It won't show a message until the command is called.
Code: Select all
MsgBox This will show a message when you start the script, because that is when MsgBox is called.
if false
MsgBox This message will not show.
^1::MsgBox This will show when you press the hotkey, because that is when MsgBox is called.
MsgBox This will never show, because it will never be called.
#Warn Unreachable ; Adding this line anywhere in the script will warn you about the above line being unreachable.
https://www.autohotkey.com/docs/Concepts.htm#control-flow
Re: Changing Tray Icon when script is paused
The thing is that I don't need hotkey. I need same functionality when you pause default script(red icon) and standard icon when running(green icon). When you pause script, with right-click on taskbar; no hotkey needed. There should be one custom icon when script is running and different when script is paused nothing more.
Re: Changing Tray Icon when script is paused
I know, and I have explained what I think your problem is.
Re: Changing Tray Icon when script is paused
I haven't done GUIs in v1 much yet nor do I plan to ever, but try this (untested). Add to top of your script:IGRACH wrote: ↑19 Aug 2020, 08:23The thing is that I don't need hotkey. I need same functionality when you pause default script(red icon) and standard icon when running(green icon). When you pause script, with right-click on taskbar; no hotkey needed. There should be one custom icon when script is running and different when script is paused nothing more.
Code: Select all
Menu, Tray, Add, PauseChangeIcon
PauseChangeIcon:
Pause
if (A_IsPaused)
Menu, Tray, Icon, % A_ScriptDir "RegularAHKIcon.ico"
else
Menu, Tray, Icon, % A_ScriptDir "YourPauseIcon.ico"
return
-TL
Re: Changing Tray Icon when script is paused
@ Tigerlily I have copied your code to the top of the script and icon is not changing when I pause/unpause the script.