Using Unique Icons for Specific Windows Shortcuts (AutoHotkey Menu Tip)

Draw Icons for AutoHotkey Menus Directly from Windows Shortcuts

I talked extensively in an earlier blog about adding icons to the menus in the QuickLinks.ahk script by using the FileGetShortcut command. This AutoHotkey command digs out data such as the target program from the shortcut. At the time, I didn’t attempt to use the captured icon data (OutIcon and OutIconNum—icon location and number respectively) because, in most cases, the variables came up blank. Even using the standard context menu creation option (New⇒Shortcut) often did not save the icon data for viewing with GetFileShortcut. Frankly, that would have made the process too easy.

Tip: The file found at the free AutoHotkey scripts page includes ShortcutAnalize.ahk which sets up Hotkeys for quickly reading selected Windows shortcut data using the GetFileShortcut command.

IrfanView Icons
In the Change Icon window, select any icon (or Browse… for an icon), then click OK. After clicking Apply in the Properties window, AutoHotkey can read the icon data for inserting into menus.

I tested what it might take to get the default icon (or any other icon) saved, then returning icon data to the FileGetShortcut variables. I discovered that manually opening the shortcut Properties window (right-click, Properties), opening the Change Icon window (click Change Icon…), shown at right, then selecting a new icon (or clicking OK to use the default icon) registers the icon in the shortcut. In other words, simply clicking OK in the Change Icon window, then Apply in the shortcut Properties window, fills in the FileGetShortcut icon variable blanks.

That means you can use any unique icon for any specific shortcut link file (.lnk) by manually embedding an icon path and icon number in the shortcut (as described above). With the right AutoHotkey code in your script, you only need to do this manual icon setup once for any .lnk file.

Code for Inserting Windows Shortcut Icons into Menus

The code for the QuickLinks.ahk script already includes an inspection of shortcut (.lnk) inner values:

   , C:\Users\%A_UserName%\QuickLinks\%MainMenu%\%NewName%.lnk 
   , OutTarget, OutDir, OutArgs, OutDescription, OutIcon
   , OutIconNum, OutRunState

(Single line of code wrapped for display purposes using line continuation.)

If available, the OutIcon variable contains the path and filename for the icon(s) and the OutIconNum variable holds the number of the icon.

Remember, the FileGetShort command only works on Windows link (.lnk) files. It does not work for Web links (.url).

The new icon insertion code launches only when icon data appear in the shortcut. Place the code for inserting the shortcut icon into the menu after all other icon insertion commands:

 If (OutIcon != "")
   Menu, %MainMenu%, Icon, %NewName%, %OutIcon%, %OutIconNum%

This ensures that the shortcut icon becomes the default—overwriting all other icons except the AddIcon() function code.

By using this manual Windows (File) Explorer icon setup technique, it may save the need to add extra IfInString icon insertion code lines. If reading a .lnk type file, AutoHotkey can extract the icon data directly from the shortcut without any changes to the script. In fact, it might allow you to start removing lines from the AddIcon() function.

This code can be found in the QuickLinks.ahk script. You can read a fairly complete discussion of how to setup and use QuickLinks at the “A Free QuickLinks Windows App from Jack” Web page.  Three chapters of the e-book Digging Deeper Into AutoHotkey discuss the QuickLinks.ahk script. Plus, Chapter Thirty of AutoHotkey Applications offers a basic explanation of how to add icons to AutoHotkey menus—although I offer much more in these blogs at Jack’s AutoHotkey Blog.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s