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")
}