Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Win32 Shortcuts (.lnk files) with com in python (See related posts)

Class and general usage for accessing windows shortcuts in Python.

import pythoncom

class Win32Shortcut:
    def __init__(self, lnkname):
        self.shortcut = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink, None,
            pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
        self.shortcut.QueryInterface(pythoncom.IID_IPersistFile).Load(lnkname)

    def __getattr__(self, name):
        return getattr(self.shortcut, name)

s = Win32Shortcut(path)
iconPath = s.GetIconLocation()[0]
itemPath = s.GetPath(0)[0]




Comments on this post

gooli posts on Apr 22, 2007 at 07:53
There's an import missing that is needed from the constants:

from win32com.shell import shell


You need to have PyWin32 installed if you're using the standard Python distribution. If you're using ActivePython, you're all set.

You need to create an account or log in to post comments to this site.