1
Post New Requests Here / Re: IDEA: Pull icon from exe and put on the containing folder
« on: February 11, 2021, 04:39 AM »
Neat idea, so I made an AutoHotkey version with a hotkey and also a command line mode for easy use from FARR.
Uses COM to get the focused file's path more reliably than sending keys to the context menu.
Uses SHGetSetFolderCustomSettings method which makes the new folder icon show immediately, see Microsoft doc.
Uses COM to get the focused file's path more reliably than sending keys to the context menu.
Uses SHGetSetFolderCustomSettings method which makes the new folder icon show immediately, see Microsoft doc.
Code: Autohotkey [Select]
- SetWorkingDir %A_ScriptDir%
- #SingleInstance force
- ListLines, Off
- ; SetExplorerFolderIcon.ahk
- ; 2021-02-11
- ; by Nod5
- ; In Explorer press [F8] to set the focused file's first icon as parent folder icon
- ; -----------------------------------
- ; Default mode:
- ; Select a file in Explorer and press [F8].
- ; To close the script right click its tray menu icon and Exit.
- ; Command line mode:
- ; Pass as parameter a HWND window handle to a specific Explorer window
- ; to act on that window's focused file and parent folder.
- ;
- ; Optional: use this script from FARR (Find And Run Robot)
- ; Create a FARR alias and use this as the result line (modify the path):
- ; Set Explorer parent folder icon | C:\some\folder\SetExplorerFolderIcon.ahk %LASTHWND%
- ; -----------------------------------
- ; if parameter is Explorer HWND then act on that window and exit
- if (vHwnd := A_Args[1])
- {
- GetFocusedFileSetIcon(vHwnd)
- }
- ; else hotkey mode: create Explorer hotkey and await user action
- else
- {
- }
- return
- GetFocusedFileSetIcon(vHwnd := "")
- {
- vFile := ExplorerGetFocusedFilepath(vHwnd)
- {
- SetFolderIcon(vFolder, vFile, 0)
- }
- }
- ; function: ExplorerGetFocusedFilepath(vHwnd := "") v210211
- ; return filepath of focused item in vHwnd (or active) Explorer window
- ExplorerGetFocusedFilepath(vHwnd := "")
- {
- for Window in ComObjCreate("Shell.Application").Windows
- if (Window.HWND = vHwnd)
- ; https://docs.microsoft.com/en-us/windows/win32/shell/shellfolderview-focuseditem
- return vFile := Window.Document.FocusedItem.Path
- }
- ; function: SetFolderIcon() update Explorer folder icon in desktop.ini via SHGetSetFolderCustomSettings
- ; by teadrinker 2017-10-09 https://www.autohotkey.com/boards/viewtopic.php?p=174984#p174984
- ; https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetsetfoldercustomsettings
- ; https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/ns-shlobj_core-shfoldercustomsettings
- SetFolderIcon(folderPath, iconPath, iconIndex) {
- static FCSM_ICONFILE := 0x10, FCS_FORCEWRITE := 0x2
- if !A_IsUnicode {
- StrPut(iconPath, &WiconPath, "UTF-16")
- }
- DllCall("Shell32\SHGetSetFolderCustomSettings", Ptr, &SHFOLDERCUSTOMSETTINGS, WStr, folderPath, UInt, FCS_FORCEWRITE)
- }
- ; -----------------------------------
- ; notes on desktop.ini files
- ; -----------------------------------
- ; https://docs.microsoft.com/en-us/windows/win32/shell/how-to-customize-folders-with-desktop-ini
- ; "text file that allows you to specify how a file system folder is viewed."
- ; See also https://hwiegman.home.xs4all.nl/desktopini.html
- ; Format:
- ; -----
- ; [.ShellClassInfo]
- ; IconResource=C:\test\file.exe,0 ; <filepath, icon-index>
- ; -----
- ; To remove the custom folder icon delete desktop.ini
- ; If we just write the file then the new folder icon does not show immediately (must close/reopen Explorer)
- ; If we instead use SHGetSetFolderCustomSettings the new icon shows immediately.
- ; SHGetSetFolderCustomSettings also hides the desktop.ini file.
- ; https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetsetfoldercustomsettings
- ; https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/ns-shlobj_core-shfoldercustomsettings
- ; AutoHotkey function: SetFolderIcon()
- ; by Teadrinker 2017-10-09 https://www.autohotkey.com/boards/viewtopic.php?p=174984#p174984
- ; -----------------------------------