Resolving windows shortcut to url

R

Richard Townsend

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.
 
I

Ivo

Richard said:
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?
 
I

Ivo

Ivo said:
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]
 
R

Richard Townsend

Ivo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top