How to change tray icon to a custom one when the script is paused?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

How to change tray icon to a custom one when the script is paused?

05 Jun 2022, 14:56

So I have this code

Code: Select all

I_Icon = C:\AutoHotkey.ico
IfExist, %I_Icon%
    Menu, Tray, Icon, %I_Icon%
;return
and it works when it is put at the top of AHK file. But I would like to add to it a caveat, that when the script is paused, then the tray icon would switch itself to that at

Code: Select all

C:\AutoHotkey OFF.ico
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

05 Jun 2022, 15:53

Well this

Code: Select all

Menu, Tray, Icon, C:\AutoHotkey.ico

Menu, Tray, Icon, C:\AutoHotkey OFF.ico
Pause, On

Menu, Tray, Icon, C:\AutoHotkey.ico
Pause, Off
and this

Code: Select all

Menu, Tray, Icon, C:\AutoHotkey.ico
Menu, tray, NoStandard ; removes edit, pause, reload etc
; other menu options
Menu, tray, Add, &Suspend Hotkeys     , MenuHandler
Menu, tray, Icon,&Suspend Hotkeys     , %A_AhkPath%, 3 ; default suspend     icon
Menu, tray, Add, &Pause Script        , MenuHandler
Menu, tray, Icon,&Pause Script        , %A_AhkPath%, 4 ; default pause icon

MenuHandler:
;
If (A_ThisMenuItem = "&Pause Script")
{
 Menu, Tray, Icon, C:\AutoHotkey OFF.ico
 Pause
 Return
}
does no work properly
User avatar
boiler
Posts: 17403
Joined: 21 Dec 2014, 02:44

Re: How to change tray icon to a custom one when the script is paused?

05 Jun 2022, 16:01

As it says at that link:
very basic code you need to expand to turn it on/off

But actually, I don't think that script can work anyway. It's just changing the main icon before it pauses, then it changes to the standard Pause icon.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

06 Jun 2022, 06:19

Code: Select all

#NoTrayIcon
hIconNormal := LoadPicture(A_AHKPath, "Icon1", IMAGE_ICON := 1)
hIconPaused := LoadPicture("Shell32", "Icon145", IMAGE_ICON := 1)
TrayIconActions("add", hIconNormal)
OnMessage(0x111, Func("WM_COMMAND").Bind(hIconNormal, hIconPaused))
Return

WM_COMMAND(hIconNormal, hIconPaused, wp, lp) {
   static trayMenuPauseID := 65306, windowMenuPauseID := 65403
   menuID := wp & 0xFFFF
   if (menuID = trayMenuPauseID || menuID = windowMenuPauseID)
      TrayIconActions("modify", A_IsPaused ? hIconNormal : hIconPaused)
}

TrayIconActions(command := "delete", hIcon := "", hWnd := "", uID := 0x404, nMsg := 0x404)
{
   static NIF_MESSAGE := 1, NIF_ICON := 2, NIF_TIP := 4
        , NIM_ADD := 0, NIM_MODIFY := 1, NIM_DELETE := 2
        , _ := OnExit("TrayIconActions")
   (hWnd = "" && hWnd := A_ScriptHwnd)
   uFlags := (command = "add" ? NIF_MESSAGE|NIF_TIP : 0) | (command != "delete" ? NIF_ICON : 0)
   action :=  command = "add" ? NIM_ADD : command = "modify" ? NIM_MODIFY : NIM_DELETE
   
   VarSetCapacity(NOTIFYICONDATA, A_PtrSize*4 + 136, 0)
   NumPut(size, NOTIFYICONDATA, "UInt")
   NumPut(hWnd, NOTIFYICONDATA, A_PtrSize)
   NumPut(uID,  NOTIFYICONDATA, A_PtrSize*2)
   if (command = "add") {
      NumPut(nMsg, NOTIFYICONDATA, A_PtrSize*2 + 8, "UInt")
      StrPut(A_ScriptName, &NOTIFYICONDATA + A_PtrSize*4 + 8, "CP0")
   }
   if (command != "delete") {
      NumPut(uFlags, NOTIFYICONDATA, A_PtrSize*2 + 4, "UInt")
      NumPut(hIcon,  NOTIFYICONDATA, A_PtrSize*3 + 8)
   }
   DllCall("shell32\Shell_NotifyIcon", "UInt", action, "Ptr", &NOTIFYICONDATA)
}
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

06 Jun 2022, 07:11

Or like this:

Code: Select all

OnMessage(0x111, "WM_COMMAND")

WM_COMMAND(wp) {
   static trayMenuPauseID := 65306, windowMenuPauseID := 65403
   menuID := wp & 0xFFFF
   if (wp >> 16 = 0) && (menuID = trayMenuPauseID || menuID = windowMenuPauseID)
      Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "Shell32", % A_IsPaused ? 1 : 145, 1
}
Last edited by teadrinker on 07 Jun 2022, 06:50, edited 1 time in total.
User avatar
submeg
Posts: 335
Joined: 14 Apr 2017, 20:39
Contact:

Re: How to change tray icon to a custom one when the script is paused?

06 Jun 2022, 07:26

I have quite a few icons that I switch between; I have different "profiles" for my code, as I have a single main script that I use, but depending on which machine I am working on, file paths can be different and so I need to update them without having to change my code, so I just switch profile instead.

I add all the paths for my icons like this:

Code: Select all


;Home Icons
global HEnabledIcon = ParentPath . "HomeIcon.ico"
global HDisabledIcon = ParentPath . "HomeIconD.ico"
global UnknownIcon = ParentPath . "Unknown.ico"
global UnknownDisabledIcon := ParentPath . "UnknownDisabled.ico"
global GamingEnabledIcon :=  ParentPath . "GamingOn.ico"
global GamingDisabledIcon := ParentPath . "GamingOff.ico"
global MusicEnabledIcon := ParentPath . "MusicIcon.ico"
global MusicDisabledIcon := ParentPath . "MusicIconD.ico"
global MinimalistEnabledIcon := ParentPath . "MinimalistIcon.png"
global MinimalistDisabledIcon := ParentPath . "MinimalistDisabledIcon.png"

...

;Set the icon image
Menu,Tray,Icon, %UnknownIcon%

I then choose which icon I use based on the below. Note: I also change the icon if my CapsLock, NumLock (or both) are on:

Code: Select all


;------------------------CHECK STATUS-------------------------
CheckStates()
{
	
	CapsOn := GetKeyState("CapsLock", "T")
	NumOn := GetKeyState("NumLock", "T")
	
	If PauseFlag = 1					;If the script is disabled
	{
		
		IconChoice = 1
;Menu,Tray,Icon, %DisabledIcon%
		
	}
	
	
	Else If PauseFlag = 0				;If the script is active: 
	{
		
;Then check if the keyboard or mouse is disabled
		
;Is the keyboard disabled?
		If DisableKeyboardFlag = 0
		{
			IconChoice = 3
;Menu,Tray,Icon, %KeyboardDisabledIcon%
			
		}
		
;Is the mouse disabled?
		Else If TouchToggle = 0
		{
			
			IconChoice = 5
;Menu,Tray,Icon, %MouseDisabledIcon%
			
		}
		
;If the keyboard and mouse are active, check for all the different key combos
		Else
		{	
			
			If (CapsOn = 1 AND NumOn = 1)  
			{
				
				IconChoice = 6
;Menu,Tray,Icon, %CapsNumLockWarningIcon%
				
			}
			
			Else If (CapsOn = 1 AND NumOn = 0)
			{
				IconChoice = 7
;Menu,Tray,Icon, %CapsWarningIcon%
				
			}
			
			Else If (CapsOn = 0 AND NumOn = 1) 
			{
				IconChoice = 8
;Menu,Tray,Icon, %NumLockWarningIcon%
				
			}
			
			Else							;If all the keys are off, it's the standard icon
			{
				
				IconChoice = 2
				
			}
		}
		
	}	
	
;msgbox IconChoice = %IconChoice%
	
;Once you know the state, pick the icon.
	Switch IconChoice
	{
		Case 0:
		msgbox Test
		
		Case 1:
		Menu,Tray,Icon, %DisabledIcon%
		
		Case 2:							;to do more than one case at a time use Case 2, 3:
		Menu,Tray,Icon, %EnabledIcon%
		
		Case 3:
		Menu,Tray,Icon, %KeyboardDisabledIcon%
		
		Case 4:				
		Menu,Tray,Icon, %EnabledIcon%
		
		Case 5:
		Menu,Tray,Icon, %MouseDisabledIcon%
		
		Case 6:
		Menu,Tray,Icon, %CapsNumLockWarningIcon%
		
		Case 7:
		Menu,Tray,Icon, %CapsWarningIcon%
		Case 8:
		Menu,Tray,Icon, %NumLockWarningIcon%
		
		Default:
		msgbox IconChoice doesn't match icons!
	}
	
	
}
Return
;-------------------------------------------------------------

____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 04:30

submeg wrote:
06 Jun 2022, 07:26
I have quite a few icons that I switch between; I have different "profiles" for my code, as I have a single main script that I use, but depending on which machine I am working on, file paths can be different and so I need to update them without having to change my code, so I just switch profile instead.
[...]
I have a different modus operandi for that

I created special folder on drive C with all of the scripts and pieces, icons, various small pieces of software that do not install themselves, etc. and put in sub-folders for maintaining order. And I have a drive S which holds things like copies of that mentioned folder, my e-mails from Thunderbird, personal files, install version of my filemanager. This gives me the ability to physically take my S drive, connected it to a machine and with few simple tasks have a system [not operating system in a strict sense] similar to mine
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 04:32

teadrinker wrote:
06 Jun 2022, 07:11
Or like this:

Code: Select all

OnMessage(0x111, "WM_COMMAND")

WM_COMMAND(wp) {
   static trayMenuPauseID := 65306, windowMenuPauseID := 65403
   menuID := wp & 0xFFFF
   if (menuID = trayMenuPauseID || menuID = windowMenuPauseID)
      Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "Shell32", % A_IsPaused ? 1 : 145, 1
}
I am having trouble adapting this to my needs, as with this change it does not workt:

Code: Select all

Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "C:\AutoHotkey.ico", % A_IsPaused ? "C:\AutoHotkey OFF.ico"
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 06:39

I adjusted the function a little, should be

Code: Select all

WM_COMMAND(wp) {
   static trayMenuPauseID := 65306, windowMenuPauseID := 65403
   menuID := wp & 0xFFFF
   if (wp >> 16 = 0) && (menuID = trayMenuPauseID || menuID = windowMenuPauseID)
      Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "C:\AutoHotkey.ico",, 1
}
Have you tried reading the docs to understand what each parameter means?
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 08:50

teadrinker wrote:
07 Jun 2022, 06:39
I adjusted the function a little, should be[...]
This does not work
teadrinker wrote:
07 Jun 2022, 06:39
Have you tried reading the docs to understand what each parameter means?
Yes I did; just like in case of my other code where I apparently had success in understanding the help files [viewtopic.php?f=76&t=104768&p=466452#p466452]. So that mine above was what I came up in this case

Unfortunately AHK scripts are more complicated than HTML and CSS code and how Mp3tag's actions work - with which I have some [limited] experience. And that is also why I have not tried out the two other suggested approaches in this thread, as there look far more complicated for my abilities
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 09:46

Keymaker wrote: This does not work
My example must work. Show your complete code. But try this as a separate script first:

Code: Select all

OnMessage(0x111, "WM_COMMAND")

WM_COMMAND(wp) {
   static trayMenuPauseID := 65306, windowMenuPauseID := 65403
   menuID := wp & 0xFFFF
   if (wp >> 16 = 0) && (menuID = trayMenuPauseID || menuID = windowMenuPauseID)
      Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "C:\AutoHotkey.ico",, 1
}
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 15:23

teadrinker wrote:
07 Jun 2022, 09:46
[...]
My example must work
[...]
Sorry, I am on medication that simply makes me dumb - and I have too many threads on this form trying to resolve many AHK issues, thus sitting on them at night. So yes- it works. And the title of this topic should have been "How to change tray icons to custom ones when the script is if for example paused?"


So now my question is: where to you get all these names and values from?

I want to make similar entries for suspended and for [normal] running. In the help files I found

Code: Select all

A_IsSuspendedmenuID
but did not find something like

Code: Select all

A_IsNormalmenuID
And the help files, at least through search, do not return records for "trayMenuPauseID"; nor even for "trayMenu", "MenuPause", "PauseID". And where this 65306 ID number comes from - the AutoHotkey itself or the operating system?
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 16:09

Launch this script:

Code: Select all

OnMessage(0x111, "WM_COMMAND")

WM_COMMAND(wp) {
   if (wp >> 16 = 0) {
      MsgBox % wp & 0xFFFF
      Return false
   }
}

Esc:: ExitApp
then open the tray menu of the script and click a menu item.
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 18:53

teadrinker wrote:
07 Jun 2022, 16:09
Launch this script:
[...]
then open the tray menu of the script and click a menu item.
I got meself

65305 for Suspend Hotkeys
65303 for Reload This Script

from it. But I can not get value for Exit because it just closes the script. And also the value for un-Pause [AKA the normal state] in unobtainable, as the script does not really pause [and if it would then it would not show that value, right?]
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

07 Jun 2022, 21:29

Keymaker wrote: But I can not get value for Exit because it just closes the script
I've edited the script, now it shouldn't close.
Keymaker wrote:the value for un-Pause [AKA the normal state] in unobtainable, as the script does not really pause
There is no separate value for un-Pause since it's the same menu item.
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

08 Jun 2022, 12:33

So I got

65300 for Open
65301 for Help
65302 for Window Spy
65303 for Reload This Script
65304 for Edit This Script
65305 for Suspend Hotkeys
65306 for Pause Script
65307 for Exit


although that last value it an educated guess, as clicking >>Exit<< closes the script. But what d I do with them?

Because this

Code: Select all

OnMessage(0x111, "WM_COMMAND")

WM_COMMAND(wp) {
   static trayMenuPauseID := 65306, windowMenuPauseID := 65403
   menuID := wp & 0xFFFF
   if (wp >> 16 = 0) && (menuID = trayMenuPauseID || menuID = windowMenuPauseID)
      Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "C:\AutoHotkey PAUSE.ico",, 1
}

OnMessage(0x111, "WM_COMMAND")

WM_COMMAND(wp) {
   static trayMenuSuspendHotkeysID := 65305, windowMenuSuspendHotkeysID := 65403
   menuID := wp & 0xFFFF
   if (wp >> 16 = 0) && (menuID = trayMenuSuspendHotkeysID || menuID = windowMenuSuspendHotkeysID)
      Menu, Tray, Icon, % A_IsPaused ? A_AHKPath : "C:\AutoHotkey SUSPEND.ico",, 1
}
is not even able to start
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

08 Jun 2022, 12:44

Keymaker wrote: But what d I do with them?
It depends on what you want to achieve. What icon should be if the script is paused and suspended at the same time?
User avatar
A Keymaker
Posts: 457
Joined: 31 May 2022, 13:46
Location: Donjon du Château de Mérovingien

Re: How to change tray icon to a custom one when the script is paused?

08 Jun 2022, 13:42

teadrinker wrote:
08 Jun 2022, 12:44
[...]
What icon should be if the script is paused and suspended at the same time?
I did not even imagined that this could be possible. So I will ask right away: does this also have a double state - one when first the script was suspended and then paused and other when first the script was paused and only then suspended?

All in all the ultimate goal would be to have an individual icon for every possible state; in a given script, with information about paths leading to those icons stored about that within AHK file hosting that script. And also to see them in the menu available after right clicking of the Tray icon of that very script

To make it easier we can store them as
C:\AutoHotkey Icons\AH icon #1.ico
C:\AutoHotkey Icons\AH icon #2.ico
C:\AutoHotkey Icons\AH icon #3.ico


etc.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change tray icon to a custom one when the script is paused?

08 Jun 2022, 16:08

Keymaker wrote: I did not even imagined that this could be possible.
Why not? You can set the check mark on both items in the tray menu, one by one. And you will see, how the script icon changes (I mean an unmodified script).
Keymaker wrote: does this also have a double state - one when first the script was suspended and then paused and other when first the script was paused and only then suspended?
No, there is no difference. If the script is paused but unsuspended you can still use its hotkeys:

Code: Select all

i := 0
Pause

$F1::
   Loop {
      ToolTip % ++i
      Sleep, 100
   }
   Return
So you need only three icons for all possible statuses, if for the normal status the usual AHK icon is used.
I'd suggest code like this:

Code: Select all

Icons := { 0: {path: A_AHKPath, iconNumber: 1, status: "normal"}
         , 1: {path: "C:\AutoHotkey Icons\AH icon #1.ico", iconNumber: "", status: "paused"}
         , 2: {path: "C:\AutoHotkey Icons\AH icon #2.ico", iconNumber: "", status: "suspended"}
         , 3: {path: "C:\AutoHotkey Icons\AH icon #3.ico", iconNumber: "", status: "paususp"} }
         
OnMessage(0x111, Func("WM_COMMAND").Bind(Icons))

WM_COMMAND(Icons, wp) {
   static menuIDs := { 65305: {action: "suspend", value: 2}
                     , 65306: {action: "pause"  , value: 1}
                     , 65403: {action: "pause"  , value: 1}
                     , 65404: {action: "suspend", value: 2} }
   menuID := wp & 0xFFFF
   if (wp >> 16 = 0 && inputStatus := menuIDs[menuID, "value"]) {
      prevStatus := A_IsSuspended << 1|A_IsPaused
      newSataus := prevStatus ^ inputStatus
      Menu, Tray, Icon, % Icons[newSataus, "path"], % Icons[newSataus, "iconNumber"], 1
   }
}
Keymaker wrote: And also to see them in the menu available after right clicking of the Tray icon of that very script
I'm not quite get what you mean by that. What the menu should look like?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 284 guests