; 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])
{
if WinExist("ahk_class CabinetWClass ahk_id " vHwnd
) GetFocusedFileSetIcon(vHwnd)
}
; else hotkey mode: create Explorer hotkey and await user action
else
{
Hotkey, F8, GetFocusedFileSetIcon
}
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 := "")
{
if vHwnd
and WinExist("ahk_class CabinetWClass ahk_id " 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")
}
NumPut(size
, SHFOLDERCUSTOMSETTINGS
) NumPut(FCSM_ICONFILE
, SHFOLDERCUSTOMSETTINGS
, 4) NumPut(A_IsUnicode ?
&iconPath :
&WiconPath
, SHFOLDERCUSTOMSETTINGS
, 4*2 + A_PtrSize*8) NumPut(iconIndex
, SHFOLDERCUSTOMSETTINGS
, 4*2 + A_PtrSize*9 + 4) 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
; -----------------------------------