Win32api.... can it create a Windows shortcut (.lnk) file?

Mike Fletcher mcfletch at
Thu Nov 11 09:11:02 CET 1999

Here's a quicky demo of both creating and reading a lnk file on windows.
There's no real reason for the wrapper class other than that I'm a raving
lunatic and it's 3am :) ...

# From a demo by Mark Hammond, corrupted by Mike Fletcher
from import shell
import pythoncom, os

class PyShortcut:
	def __init__( self ):
		self._base = pythoncom.CoCreateInstance(
			shell.CLSID_ShellLink, None,
			pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink
	def load( self, filename ):
		# Get an IPersist interface
		# which allows save/restore of object to/from files
		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__':
	import sys
	file = sys.argv[1]
	shortcut = PyShortcut()
	if os.path.exists( file ):
		# load and dump info from file...
		shortcut.load( file )
		# now print data...
		print 'Shortcut in file %s to
		# create the shortcut using rest of args...
		data = map( None, sys.argv[2:], ("SetPath", "SetArguments",
"SetDescription", "SetWorkingDirectory") )
		for value, function in data:
			if value and function:
				# call function on each non-null value
				getattr( shortcut, function)( value ) file )

-----Original Message-----
From: python-list-admin at
[mailto:python-list-admin at]On Behalf Of me
Sent: November 11, 1999 1:54 AM
To: python-list at
Subject: Win32api.... can it create a Windows shortcut (.lnk) file?

I'm just trying to find an easy to create shortcuts on Windows platforms
through Python... it doesn't seem to be a simple matter though.  I've gone
through all the Win32 docs, and found nothing, and have started writing
(with limited success) a C function to do it.

Is there an easy way?  q:]

Kevin Cazabon
kcazabon at


More information about the Python-list mailing list