Last active
March 2, 2023 21:38
-
-
Save davebrny/2d2bc026efd177c41de1e5482b137fa2 to your computer and use it in GitHub Desktop.
🖼️ (autohotkey) - automagically find an icon for a script
This file contains 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
/* | |
[icon folders] | |
folder_1 = | |
folder_2 = | |
*/ | |
script_icon(ahk_filepath="") { | |
if (ahk_filepath = "") | |
{ | |
icon_name := subStr(a_scriptName, 1, strLen(a_scriptName) - 4) | |
icon_dir := a_scriptDir | |
} | |
else splitPath, ahk_filepath, , icon_dir, , icon_name | |
;# check in the script directory | |
if fileExist(a_scriptDir "\" icon_name ".ico") | |
icon_path := a_scriptDir "\" icon_name ".ico" | |
;# a_scriptDir\icons | |
else if fileExist(a_scriptDir "\icons\" icon_name ".ico") | |
icon_path := a_scriptDir "\icons\" icon_name ".ico" | |
;# a_scriptDir\icon | |
else if fileExist(a_scriptDir "\icon\" icon_name ".ico") | |
icon_path := a_scriptDir "\icon\" icon_name ".ico" | |
;# search through icon folders | |
if (icon_path = "") | |
{ ; also look for an icon that matches the first letter | |
stringLeft, first_character, % regExReplace(icon_name, "[\W]"), 1 | |
loop, | |
{ | |
iniRead, icon_folder, % a_lineFile, icon folders, folder_%a_index% | |
if (icon_folder = "") | |
continue ; to next folder | |
loop, files, % rTrim(icon_folder, "\") . "\*.ico", R | |
{ | |
splitPath, a_loopFileFullPath, , , , this_icon | |
if (this_icon = icon_name) | |
{ | |
icon_match := a_loopFileFullPath | |
break 2 | |
} | |
if (this_icon = first_character) and (letter_match = "") | |
letter_match := a_loopFileFullPath ; save the first match | |
} | |
} | |
until (icon_folder = "ERROR") | |
if (icon_match = "") | |
icon_path := letter_match ; the icon that matched the first letter | |
else icon_path := icon_match | |
} | |
;# if nothing has been found then use the default H icon | |
if (icon_path = "") | |
icon_path := a_ahkPath | |
return icon_path | |
} | |
/* | |
[script info] | |
version = 1.1 | |
description = automagically find an icon | |
author = davebrny | |
source = https://gist.github.com/davebrny/2d2bc026efd177c41de1e5482b137fa2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage
- add
script_icon.ahk
to your user library- use the
script_icon()
function with themenu
command in the auto-execute section of your scripts:- (add it to your 'new script' template if you have one)
- add and rename icons in your icon folder to match your script names
- reload your scripts to have the icons appear
icon search order
1 - look in the script directory for an .ico file matching the script name
2 - look in the folders
icon
oricons
in the script directory3 - search through your icon folders
4 - same icon folders but matching the first letter of the script name
alphabet icons
if no icons are found that match the exact script name then an icon that matches the first letter of the script will be used instead.
download one of the following icon sets and place them in one of your icon folders
grey icons (click on "download" in the top right corner and select "direct download")
green icons
icon font: ubuntu titling
example
heres an example of script_icon() in use with the same icons showing across the tray menu, shortcut files, compiled scripts and in a context menu.