Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active March 2, 2023 21:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davebrny/2d2bc026efd177c41de1e5482b137fa2 to your computer and use it in GitHub Desktop.
Save davebrny/2d2bc026efd177c41de1e5482b137fa2 to your computer and use it in GitHub Desktop.
🖼️ (autohotkey) - automagically find an icon for a script
/*
[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
*/
@davebrny
Copy link
Author

davebrny commented Mar 6, 2017

usage

- add script_icon.ahk to your user library

- use the script_icon() function with the menu command in the auto-execute section of your scripts:

menu, tray, icon, % script_icon()

- (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

 

to search in multiple icon folders, increment the number for each key name: folder_3, folder_4

this only works with uncompiled .ahk scripts.   to have compiled scripts use the same icons, use this script in conjunction with compile.ahk

 

icon search order

1 - look in the script directory for an .ico file matching the script name
2 - look in the folders icon or icons in the script directory
3 - 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

ahk keys

ahk green

 

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.

text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment