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

Stefan Migowsky smigowsky at dspace.de
Fri Nov 12 08:56:08 CET 1999


Hi, 

since this program is so clean and simple there is no error checking.
The IPersists interface just throws an unspecified OLE exception, if
i.e. the path of the lnk is non existent. So just include some error 
checking like :

...
import os
import types

if type(Path) != types.StringType or\
   type(Target) != types.StringType or\
   type(Arguments) != types.StringType or\
	....
	raise TypeError, "String arguments required"


LinkDir   = os.path.dirname(Path)
TargetDir = os.path.dirname(Target)

if not os.path.isfile(Target) or\
	raise TypeError,  "Target file does not exist"  # Not very correct
but convienient

if not os.path.isdir(LinkDir)
	raise TypeError,  "Link directory does not exist"  # Not very
correct but convienient

....  etc.


Stefan

Dipl.-Physiker Stefan Migowsky	
dSPACE GmbH		
Technologiepark 25			33100 Paderborn (Germany)
mailto:smigowsky at dspace.de 		http://www.dspace.de



-----Original Message-----
From: me [mailto:none at fake.net]
Sent: Friday, November 12, 1999 6:18 AM
To: smigowsky at dspace.de
Subject: Re: Win32api.... can it create a Windows shortcut (.lnk) file?


That's a nice clean example.... but I'm having one small problem, even
taking it exactly as you put it:


##########################################
Traceback (innermost last):
  File "C:\Program
Files\Python\Win32\Pythonwin\pywin\framework\scriptutils.py", 
line 237, in
RunScript
    exec codeObject in __main__.__dict__
  File "C:\PROGRA~1\PYTHON\MYPROJ~1\WINTOOLS\wintools.py", line 
144, in ?
    CreateShortCut(Path,Target,Arguments,StartIn,Icon,Description)
  File "C:\PROGRA~1\PYTHON\MYPROJ~1\WINTOOLS\wintools.py", line 132, in
CreateShortCut
    persist.Save(Path, 1)
com_error: (-2147467259, 'Unspecified error', None, None)
##########################################

Unfortunately, "Unspecified error" doesn't help me too much... 
q:].  I'll be
looking into that tonite, but if you have a quick fix, I'd 
appreciate it.
I've been using the win32api module a lot, and it's great, but 
not pythoncom
a lot yet...

q:]

Kevin.


Stefan Migowsky <smigowsky at dspace.de> wrote in message
news:137053459BA7D211B3930090272A62CA8EDD80 at newsintern.dspace.de...
> Hi
>
>
> Mark Hammond did a great job on wrapping the win32api. So
> creating a shortcut is just so easy :
> ################ CreateShortCut.py ####################
>
> from win32com.shell import shell
> import win32api
> import pythoncom
> import os
> import sys
>
> def CreateShortCut(Path, Target,Arguments = "", StartIn = "", Icon =
("",0),
> Description = ""):
>     # Get the shell interface.
>     sh = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, \
>         pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
>
>     # Get an IPersist interface
>     persist = sh.QueryInterface(pythoncom.IID_IPersistFile)
>
>     # Set the data
>     sh.SetPath(Target)
>     sh.SetDescription(Description)
>     sh.SetArguments(Arguments)
>     sh.SetWorkingDirectory(StartIn)
>     sh.SetIconLocation(Icon[0],Icon[1])
>
>     # Save the link itself.
>     persist.Save(Path, 1)
>
> if __name__ == "__main__":
> TempDir = os.environ["TEMP"]
> WinRoot = os.environ["windir"]
>
> Path        =  WinRoot    + "\\Profiles\\All Users\\Desktop\\New
> Link.lnk"
> Target      =  Pythonroot + "pythonw.exe "
> Arguments   =  TempDir + "\\test.py"
> StartIn     =  TempDir
> Icon        = (Pythonroot + "\\py.ico", 0)
> Description = "New Link"
>
> CreateShortCut(Path,Target,Arguments,StartIn,Icon,Description)
>
>
> Stefan
>
>
>
> Dipl.-Physiker Stefan Migowsky
> dSPACE GmbH
> Technologiepark 25 33100 Paderborn (Germany)
> mailto:smigowsky at dspace.de http://www.dspace.de
>
>
>
> -----Original Message-----
> From: me [mailto:none at fake.net]
> Sent: Thursday, November 11, 1999 7:54 AM
> To: python-list at python.org
> 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:]
>
> Thanks,
> Kevin Cazabon
> 
>
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
>
>






More information about the Python-list mailing list