Break chained Scripts' tray icon - UngruppingTrayIcon doesn't work consistently.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
haomingchen1998
Posts: 176
Joined: 20 Feb 2023, 16:37

Break chained Scripts' tray icon - UngruppingTrayIcon doesn't work consistently.

Post by haomingchen1998 » 09 May 2024, 11:15

Hi, I want to ungroup or break the chain of 2 scripts in windows 11 tray icon. I have 2 scripts, one scripts is located at "C:\Ahk" and another script is located at "C:\Ahk\Completed". So far, I saw below scripts from @teadrinker which doesn't work under certain situations, and I'm not sure why. I'm aware of solutions such as run a renamed copy of AutoHotkey.exe suggested by @mikeyww, and maybe converting the script itself into .exe file, but I still prefer @teadrinker 's solution since it's more dynamic.

Here is when the scripts works and when it doesn't:
1. Doesn't work when I just run the script from its current location. So I tried to create a new script file for each original script file on desktop, rename the file to the original file name, and then paste the exact same code into each script. Still doesn't work.
2. Works when I create a new script file for each on desktop, keep the default name given (Ex: New AutoHotkey Script.ahk), and then paste the exact same code from original script into each new script file. If I place those 2 new files to the path of the original script, still works. The moment I rename the 2 files to its original name, it's doesn't work anymore (2 script icons chained together again). These 2 scripts are independent of each other, meaning one doesn't rely on the other to function, nor do they #include anything from each other. Any ideas on what I can do to make it work? Thank you!

Below is the script for each file, the only difference between the 2 scripts are the name of the image, both located in the same folder ("C:\Ahk\Icons\"), just different image name. So I only posted 1 script.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Enable warnings to assist with detecting common errors.
#SingleInstance Force
#Persistent

; Run as Admin
If not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}

hIcon := HIconFromPng("C:\Ahk\Icons\Recycle Bin.png", icoSize := 16)
MyUngruppingIcon := new UngruppingTrayIcon(hIcon)

; this function was from https://www.autohotkey.com/boards/viewtopic.php?p=474323#p474323
class UngruppingTrayIcon
{
   #NoTrayIcon
   __New(hIcon, trayTip := "") {
      static msg := 0x404, hwnd := 0
           , NIM_ADD := 0x00000000
           , flags := ( NIF_MESSAGE := 0x00000001 )
                    | ( NIF_ICON    := 0x00000002 )
                    | ( NIF_TIP     := 0x00000004 )
                    | ( NIF_GUID    := 0x00000020 )
                    
      (trayTip = "" && trayTip := A_ScriptName)
      hwnd := !hwnd ? A_ScriptHwnd : DllCall("CreateWindowEx", "UInt", 0, "Str", "AutoHotkey", "Str", "", "UInt", 0
                                                             , "Int", 0, "Int", 0, "Int", 0, "Int", 0
                                                             , "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr")
      this.hwnd := hwnd
      VarSetCapacity(NOTIFYICONDATA, size := 64 + StrPut(trayTip, "CP0"), 0)
      NumPut(size   , &NOTIFYICONDATA)
      NumPut(hwnd   , &NOTIFYICONDATA + A_PtrSize)
      NumPut(flags  , &NOTIFYICONDATA + A_PtrSize*2 + 4)
      NumPut(msg    , &NOTIFYICONDATA + A_PtrSize*2 + 8)
      NumPut(hIcon  , &NOTIFYICONDATA + A_PtrSize*3 + 8)
      StrPut(trayTip, &NOTIFYICONDATA + A_PtrSize*4 + 8, "CP0")
      this.GUID := this._CreateGuid(&NOTIFYICONDATA + size - A_PtrSize - 16)
      if !DllCall("Shell32\Shell_NotifyIcon", "UInt", NIM_ADD, "Ptr", &NOTIFYICONDATA)
         throw "Shell_NotifyIcon failed, error " . A_LastError
   }
   __Delete() {
      static NIM_DELETE := 0x00000002
      VarSetCapacity(NOTIFYICONDATA, size := 16, 0)
      NumPut(size     , NOTIFYICONDATA)
      NumPut(this.hwnd, NOTIFYICONDATA, A_PtrSize)
      DllCall("Shell32\Shell_NotifyIcon", "UInt", NIM_DELETE, "Ptr", &NOTIFYICONDATA)
      (this.hwnd = A_ScriptHwnd || DllCall("DestroyWindow", "Ptr", this.hwnd))
   }
   _CreateGuid(pGUID) {
      VarSetCapacity(CLSID, 78) ; (38 + 1) * 2
      DllCall("Ole32\CoCreateGuid", "Ptr", pGUID)
      DllCall("Ole32\StringFromGUID2", "Ptr", pGUID, "WStr", CLSID, "Int", 39)
      Return CLSID
   }
}

; this function is from https://www.autohotkey.com/boards/viewtopic.php?t=112980
HIconFromPng(pngFilePath, icoSize := 16) {
   File := FileOpen(pngFilePath, "r")
   File.RawRead(buf, len := File.Length)
   File.Close()
   Return DllCall("CreateIconFromResourceEx", "Ptr", &buf, "UInt", len, "UInt", true, "UInt", 0x30000
                                            , "Int", icoSize, "Int", icoSize, "UInt", 0, "Ptr")
}

teadrinker
Posts: 4364
Joined: 29 Mar 2015, 09:41
Contact:

Re: Break chained Scripts' tray icon - UngruppingTrayIcon doesn't work consistently.

Post by teadrinker » 09 May 2024, 19:30

This approach only separates the icons of such scripts from the rest of the scripts where this method is not applied. However, the icons of scripts where this method is used remain linked. There is currently no known way to separate them other than renaming the exe file.

haomingchen1998
Posts: 176
Joined: 20 Feb 2023, 16:37

Re: Break chained Scripts' tray icon - UngruppingTrayIcon doesn't work consistently.

Post by haomingchen1998 » 09 May 2024, 20:18

teadrinker wrote:
09 May 2024, 19:30
This approach only separates the icons of such scripts from the rest of the scripts where this method is not applied. However, the icons of scripts where this method is used remain linked. There is currently no known way to separate them other than renaming the exe file.
Thanks for explaining!

Post Reply

Return to “Ask for Help (v1)”