Created
January 25, 2026 19:41
-
-
Save Krzysiu/c9f0f5462e555923ef9f2ba7438a0182 to your computer and use it in GitHub Desktop.
AutoHotKey v1 - sets tray icon from base64 string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| License: MIT, (C) 2026 Krzysztof Blachnicki | |
| Function for setting tray icon from base64 string for AUtoHotKey v1. | |
| Possible uses: | |
| * distribution of single-file script without separate .ico file | |
| * automation of creating icons via external tools (like ImageMagick) | |
| Syntax: | |
| SaveBase64Ico([string]base64String, [boolean=true]tray) : string | |
| So, for example if you got your base64 string in variable icon and you want function to set the tray icon, you use | |
| SaveBase64Ico(icon) | |
| If you want just to save icon and reuse it later (for example for switching), use: | |
| iconOn := SaveBase64Ico(iconA, false) | |
| IconOff := SaveBase64Ico(iconB, false) | |
| Also if you want to be nice and clear cache later, use something like: | |
| //CODE START | |
| tempicopath := SaveBase64Ico(icon) | |
| OnExit, ExitSub ; execute ExitSub on exit | |
| return | |
| ExitSub: | |
| FileDelete, tempicopath | |
| return | |
| //CODE END | |
| Usage: | |
| 1) encode icon to base64 | |
| * use tool like https://emn178.github.io/online-tools/base64_encode_file.html | |
| * via PowerShell: | |
| [String]$base64 = [Convert]::ToBase64String((Get-Content -Path "[PATH TO YOUR ICON]" -Encoding Byte)); Write-Output $base64 | |
| 2) put function in your code (see Syntax section) | |
| Todo: | |
| * check if icon can be safely deleted after setting it up (then for typical use - set to tray, user wouldn't need to worry about cleaning up cache | |
| */ | |
| MsgBox % path | |
| SaveBase64Ico(sBase64, setAsTrayIcon := true) { | |
| static CRYPT_STRING_BASE64 := 0x00000001 | |
| DllCall("crypt32\CryptStringToBinary", "ptr", &sBase64, "uint", 0, "uint", CRYPT_STRING_BASE64, "ptr", 0, "uint*", nSize, "ptr", 0, "ptr", 0) | |
| VarSetCapacity(bin, nSize, 0) | |
| if !(DllCall("crypt32\CryptStringToBinary", "ptr", &sBase64, "uint", 0, "uint", CRYPT_STRING_BASE64, "ptr", &bin, "uint*", nSize, "ptr", 0, "ptr", 0)) | |
| return "" | |
| cleanName := StrReplace(A_ScriptName, ".ahk", "") | |
| icoPath := A_Temp "\" cleanName "_" A_Now "_" A_MSec ".ico" | |
| f := FileOpen(icoPath, "w") | |
| if (IsObject(f)) { | |
| f.RawWrite(bin, nSize) | |
| f.Close() | |
| if (setAsTrayIcon) { | |
| Menu, Tray, Icon, %icoPath% | |
| } | |
| return icoPath | |
| } | |
| return "" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment