AutoHotkey Tip of the Week: Windows Trick for Adding Embedded Folder Icons to QuickLinks Menus

This Technique Accesses Icons Embedded in Windows Folders for Inserting into Pop-up Menus—Plus, the New Combined Switch/Case Statement QuickLinks QL_GetIcon() Function

I completely rewrote the functions from the last blog for adding icons to the menus in the QuickLinks.ahk script combining the two into a shorter prioritized list using Switch/Case statements. In the process—after investigating how to read icons embedded in Windows folder/directory listings—I discovered an interesting Windows secret. It turns out that this procedure requires a totally different Windows maneuver than that used for reading Windows Shortcut file icons.

The Windows Desktop.ini File

UnHideFiles
Ryan’s UnHideFiles.ahk script makes Windows Registry changes to hide and unhide files.

When you embed an icon into a Windows folder (right-click on the folder name in Windows File Explorer, select Properties and the Customize tap, then click Change Icon… and browse for icons), rather than saving the icon path and icon number in the folder itself—as Windows does for shortcut files—it creates a special hidden file named desktop.ini in that same folder. With Windows set to Show Hidden Files, folder and drives in the View tab of the Folder Options window, you can view the hidden desktop.ini file in that folder. (Tip: You can use Ryan’s UnHideFiles.ahk script to hide and unhide files and folders.)

With the preview pane activated in Windows File Explorer, click on the desktop.ini file to display its contents:

FolderEmbeddedIcon

The IniRead command gives us the path and number of the icon found in the [.ShellClassInfo] section and the IconResource key:

  IniRead, OutputVar
    , C:\Users\%A_UserName%\QuickLinks\%submenu%\desktop.ini
    , .ShellClassInfo , IconResource

AutoHotkey parses the OutputVar value (StrSplit() function) into its component parts—the path and icon number—for use in the Menu, …, Icon command:

  Array := StrSplit(OutputVar,",")
  Path := Array[1]
  Icon := Array[2] + 1

Tip: In this folder icon reading situation, Windows seems to use an icon numbering system which starts with zero, while the icon setting Menu, …, Icon command starts with 1. Therefore, the script adjusts for the discrepancy by adding 1 to the Icon number.

When adding the icon to the top-level of the QuickLinks menu as shown in the function, the script uses the following Menu command:

Menu, %menuitem%, Icon, %submenu%, %path% , %icon%

*          *          *

CoverImage200
A Multitude of AutoHotkey Tips and Tricks

Three chapters of the e-book Digging Deeper Into AutoHotkey discuss the beginnings of the QuickLinks.ahk script and how it works. Plus, Chapter Thirty of AutoHotkey Applications explains how I originally added icons to AutoHotkey menus. You can find even more features for the QuickLinks.ahk script demonstrated in Chapter 4.1.4 “Automatically Add Windows Shortcuts to the QuickLinks App”, Chapter 4.1.5 “Spice Up AutoHotkey Menus with Icons”, Chapter 4.1.6 “Finding Windows Icons for AutoHotkey Menus”, Chapter 4.1.7 “Add Secret Windows Tools (God Mode) to QuickLinks Menu”, and Chapter 4.1.8 “Using Unique Icons for Specific Windows Shortcuts” of the book Jack’s Motley Assortment of AutoHotkey Tips.

*          *          *

QuickLinksIconsThe image of the QuickLinks menu at the right shows the icons set by the new prioritized Ql_GetIcon(menuitem,submenu) function. The Switch/Case statements set the icons giving top priority going to folder/file embedded icons—completing checks in the following order:

  1. Check the Desktop.ini file for embedded folder icons.
  2. Check Windows shortcuts for embedded icons.
  3. If the shortcut targets a program, use the program icon.
  4. If it is an Internet .lnk file (likely created by QuickLinks), set the globe-with-mouse icon.
  5. If it is a .url type shortcut file (likely created manually in Windows File Explorer), set the globe icon. (AutoHotkey cannot detect icons embedded in .url shortcuts).
  6. If found, set the special Windows Tools folder (God Mode) shortcut icon to a Tool Folder icon.
  7. If the shortcut opens a folder (D), use the Folder icon.
  8. Read the target file extension (e.g. .pgn, .jpg, .doc, etc.) to set the default icon for that extension—if any. Add extensions and icon paths to this section to include more file types. If not found (Default:), the GoTo command jumps AutoHotkey to the Label subroutine tag LastWord: found in the Switch ⇒ Default: statement.
  9. The final Default: statement adds a default QuickLinks icon.

After satisfying a Case condition and executing its commands, AutoHotkey exits the Switch/Case statements without evaluating any more conditions. This makes the Case statement order—which sets the priorities—very important.

The QL_GetIcon(menuitem,submenu) Icon Adding Switch/Case Function

The QL_GetIcon() function attempts to collect various types of data before executing the Switch/Case statements: FileGetShortcut data, FileGetAttributes data, path and filename for .url type shortcuts, and desktop.ini data. The success or failure of each data evaluation determines the appropriate icon to load.

This QL_GetIcon() function replaces a number of lines of deprecated code (IfInString command) which adds icons to the pop-up menu in the QuickLinks.ahk script. For the latest version of the script—including this function and a few other features—see the posted QuickLinksTimeDateSubMenuSwitch.ahk script.

QL_GetIcon(menuitem,submenu)
{

; Probe shortcut data—if available
  FileGetShortcut
    , C:\Users\%A_UserName%\QuickLinks\%menuitem%\%submenu%.lnk
    , QL_OutTarget, QL_OutDir, QL_OutArgs, QL_OutDescription
    , QL_OutIcon, QL_OutIconNum, QL_OutRunState
; Save file attributes    
  FileGetAttrib, QL_FileAttrib , %QL_OutTarget%
; Save .url type shortcut file name  
  Webfile := "C:\Users\" . A_UserName . "\QuickLinks\"
               . menuitem . "\" . submenu . ".url"
; Read embedded folder icon 
  IniRead, OutputVar
    , C:\Users\%A_UserName%\QuickLinks\%submenu%\desktop.ini
    , .ShellClassInfo , IconResource
/*
This conditional Switch/Case command uses icon data stored 
in the shortcut (if any) to set menu icon. To activate and/or
change a particular icon manually open the folder/shortcut 
Properties window in Windows File Explorer (right-click on 
filename and select Properties) and Change icon..., OK, then Apply.
*/
  Switch
  {
  ; Check for embedded folder icon
    Case (menuitem = "QuickLinks" or menuitem = "QL_AddLinksMenu") 
    and (OutputVar != "Error"):
      Array := StrSplit(OutputVar,",")
      Path := Array[1]
  ; Adjust for discrepancy between Windows/AutoHotkey icon numbering
      Icon := Array[2] + 1  
      Menu, %menuitem%, Icon, %submenu%, %path% , %icon%
  ; Check for embedded shotcut icon
    Case (QL_OutIcon != ""):
      Menu, %menuitem%, Icon, %submenu%
      , %QL_OutIcon%, %QL_OutIconNum%
  ; Set program file icon
    Case Instr(QL_OutTarget,"exe"):
      Menu, %menuitem%, Icon, %submenu%, %QL_OutTarget%, 1
  ; Set Web page icon
    Case Instr(QL_OutDir,"http"), Instr(QL_OutTarget,"http"):
      Menu, %menuitem%, Icon, %submenu%
      , %A_Windir%\system32\SHELL32.dll, 15,
  ; Set Web page icon for .url type shortcuts
    Case FileExist(Webfile):
      Menu, %menuitem%, Icon, %submenu%
      , %A_Windir%\system32\SHELL32.dll, 14,
  ; Set Windows Tools folder (God Mode) icon
     Case Instr(submenu,"Tools"):
        Menu, %menuitem%, Icon, %submenu%
       ,  C:\WINDOWS\System32\SHELL32.dll,36
  ; Set folder shortcut icon
    Case Instr(QL_FileAttrib,"D"):
      Menu, %menuitem%, Icon, %submenu%
      , %A_Windir%\system32\imageres.dll, 4,
  ; Set icon based on file extension for groups of file types
    Case RegExMatch(QL_OutTarget,"\.\w+$",extension): 
      Switch extension
      {
        Case ".jpg",".png":
          Menu, %menuitem%, Icon, %submenu%
          , %A_Windir%\system32\SHELL32.dll, 142,
        Case ".epub",".mobi",".pdf":
          Menu, %menuitem%, Icon, %submenu%
          , C:\AutoHotkey\Icons\Books.ico,0
        Case ".ahk":
          Menu, %menuitem%, Icon, %submenu%
          , C:\Program Files\AutoHotkey\AutoHotkeyU64.exe, 2
      ; Jump to default
        Default: goto LastWord
      }
    ; Add default icon
      Default:
        LastWord:
        Menu, %menuitem%, Icon, %submenu%
          , %A_Windir%\system32\SHELL32.dll, 85,
    }
}

With a little adapting, this function should work just as well for other menus constructed from Windows folders and shortcuts. Since the function uses the new Switch/Case statement, you need to run a current version of AutoHotkey (November 2019).

Embedding icons in the folders or shortcuts offers the number one method for adding icons to your QuickLinks menus. Once you embed an icon, you never again need to alter it, regardless of changes made to the name or target of the folder/shortcut. If you need to alter the function code to accommodate special folders or add more file extensions, study the function of each Case statement to ensure that you add the new code for optimum effect.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s