2

I am trying to write a program where I can open any program from a folder of shortcuts on my desktop. As part of the gui, I would like the shortcut's icon to be visible next to its name. Any idea how I can get a shortcuts icon from a directory, without manually finding and saving it?

I tried to find the shortcut's file properties to see where its icon was, but I couldn't find any code to help me see it.

Alexpert2
  • 21
  • 2
  • This is the best I found: http://timgolden.me.uk/pywin32-docs/win32com.shell_and_Windows_Shell_Links.html – ivvija Jan 24 '23 at 15:30

1 Answers1

0

Using pywin32 according to http://timgolden.me.uk/pywin32-docs/win32com.shell_and_Windows_Shell_Links.html

from win32com.shell import shell
import pythoncom

shortcut = pythoncom.CoCreateInstance(
    shell.CLSID_ShellLink, None,
    pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink
)
shortcut.QueryInterface( pythoncom.IID_IPersistFile ).Load( filepath )

Get the Icon path and ID:

>>> print(shortcut.GetIconLocation())
('', 0)

If manually changed:

('%ProgramFiles%\\Blender Foundation\\Blender 3.4\\blender-launcher.exe', 0)

So we need to get the EXE path to resolve the Icon if the path is empty. See docs for the ENUM supplied.
And I manually pretty printed the result.

>>> print(shortcut.GetPath(shell.SLGP_UNCPRIORITY ))
(
  'C:\\Program Files\\Blender Foundation\\Blender 3.4\\blender-launcher.exe', 
  (
    32, 
    pywintypes.datetime(2022, 12, 20, 2, 2, 52, tzinfo=TimeZoneInfo('GMT Standard Time', True)), 
    pywintypes.datetime(2023, 1, 16, 10, 30, 16, 724000, tzinfo=TimeZoneInfo('GMT Standard Time', True)), 
    pywintypes.datetime(2022, 12, 20, 2, 2, 52, tzinfo=TimeZoneInfo('GMT Standard Time', True)), 
    0, 
    1079520, 
    0, 
    0, 
    'blender-launcher.exe', 
    ''
  )
)

The extra data seems to be from the EXE, see MS Windows Docs: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataa

ivvija
  • 482
  • 4
  • 9
  • For getting the ICO out of the EXE see https://stackoverflow.com/questions/23263599/how-to-extract-128x128-icon-bitmap-data-from-exe-in-python – ivvija Jan 24 '23 at 16:03