Resolving windows shortcut to url

Ivo ivonet at gmail.com
Fri Sep 21 16:29:37 EDT 2007


Ivo wrote:
> Richard Townsend wrote:
>> If I have a windows shortcut to a URL, is there a way to get the URL
>> in a Python app?
>>
>> I found some code that uses pythoncom to resolve shortcuts to local
>> files, but I haven't found any examples for URLs.
>>
>> The PyWin32 help mentions the PyIUniformResourceLocator Object, but I
>> couldn't find an example of how to use it.
>>
> do you mean a *.lnk file or a *.url file?

for a link (*.lnk) file:


from win32com.shell import shell
import pythoncom
import os, sys

class PyShortcut(object):
     def __init__(self):
         self._base = pythoncom.CoCreateInstance(shell.CLSID_ShellLink,
                                                 None,
 
pythoncom.CLSCTX_INPROC_SERVER,
                                                 shell.IID_IShellLink)

     def load(self, filename):
 
self._base.QueryInterface(pythoncom.IID_IPersistFile).Load(filename)

     def save(self, filename):
 
self._base.QueryInterface(pythoncom.IID_IPersistFile).Save(filename, 0)

     def __getattr__(self, name):
         if name != "_base":
             return getattr(self._base, name)


if __name__=="__main__":
     lnk = PyShortcut()
     lnk.load("path to your shortcut file including the .lnk extention 
even if you don't see it in windows")
     print "Location:", lnk.GetPath(shell.SLGP_RAWPATH)[0]



More information about the Python-list mailing list