Changing Tray Icon when script is paused

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
IGRACH
Posts: 21
Joined: 28 Oct 2016, 17:57

Changing Tray Icon when script is paused

Post by IGRACH » 13 Aug 2020, 07:25

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.

lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: Changing Tray Icon when script is paused

Post by lexikos » 14 Aug 2020, 03:43

There are two ways to know when the script is (un)paused or (un)suspended:
  1. 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.
  2. 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.

IGRACH
Posts: 21
Joined: 28 Oct 2016, 17:57

Re: Changing Tray Icon when script is paused

Post by IGRACH » 14 Aug 2020, 08:04

lexikos wrote:
14 Aug 2020, 03:43
There are two ways to know when the script is (un)paused or (un)suspended:
  1. 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.
  2. 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.

lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: Changing Tray Icon when script is paused

Post by lexikos » 14 Aug 2020, 19:59

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.

IGRACH
Posts: 21
Joined: 28 Oct 2016, 17:57

Re: Changing Tray Icon when script is paused

Post by IGRACH » 18 Aug 2020, 16:55

I still do not understand what I need to do xD.

lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: Changing Tray Icon when script is paused

Post by lexikos » 18 Aug 2020, 22:08

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,

Code: Select all

MsgBox Hello, world!
This will show a message, right?

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.
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

IGRACH
Posts: 21
Joined: 28 Oct 2016, 17:57

Re: Changing Tray Icon when script is paused

Post by IGRACH » 19 Aug 2020, 08:23

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.

lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: Changing Tray Icon when script is paused

Post by lexikos » 19 Aug 2020, 23:13

I know, and I have explained what I think your problem is.

User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: Changing Tray Icon when script is paused

Post by Tigerlily » 20 Aug 2020, 01:36

IGRACH wrote:
19 Aug 2020, 08:23
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.
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:

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
you'll obviously need to have the icon files located within the the same directory the script is located in, or specify a full path.
-TL

IGRACH
Posts: 21
Joined: 28 Oct 2016, 17:57

Re: Changing Tray Icon when script is paused

Post by IGRACH » 20 Aug 2020, 08:13

@ Tigerlily I have copied your code to the top of the script and icon is not changing when I pause/unpause the script.

Post Reply

Return to “Ask for Help (v1)”