Resolving windows shortcut to url

Richard Townsend rt at nospam.com
Fri Sep 21 17:17:48 EDT 2007


On Fri, 21 Sep 2007 22:29:37 +0200, Ivo <ivonet at gmail.com> wrote:

>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]

Hi Ivo,

I belive it is *.lnk files I'm interested in.

For example:

C:\Documents and Settings\All Users\Start
Menu\Programs\Development\Winpdb\winpdb-homepage.lnk

points to http://www.digitalpeers.com/pythondebugger/

However, your code is returning an empty string for location.

lnk.GetPath(shell.SLGP_RAWPATH) returns the tuple:

 ('', (0, <PyTime:01/01/1601 00:00:00>, <PyTime:01/01/1601 00:00:00>,
<PyTime:01/01/1601 00:00:00>, 0L, 0L, 0L, 0L, '', ''))

Regards,
Richard Townsend



More information about the Python-list mailing list