Martin (gzlist) schrieb:
> On 03/11/2008, Markus Gritsch <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> I would like to create shell links (.lnk) from my Python program.
>> ...
>> It would be very nice, if someone could give me any advice
>> how to do this using comtypes.
>
> When I wanted to do this in a hurry and didn't have time to look up
> the right way to do it, I ended up with code something like:
>
> import comtypes
> from comtypes.client import CreateObject
> ws = CreateObject("WScript.Shell")
> from comtypes.gen import IWshRuntimeLibrary
> shortcut = comtypes.cast(ws.CreateShortcut("test_shortcut.lnk"),
> comtypes.POINTER(IWshRuntimeLibrary.IWshShortcut))
> shortcut.TargetPath = "cmd.exe"
> shortcut.Arguments = "/K C:\Python24\python.exe"
> shortcut.Save()
>
> This worked for me, but may be bad practice or generally bogus in some
> way or other.
>
> I think I got there by reading
> <http://www.ironpython.info/index.php/Creating_a_Shortcut_File_with_WSH_Interop>
> then looking at the comtypes generated code, then poking things until
> it stopped throwing exceptions.
Cool! The only thing I would suggest to change
is to use QueryInterface instead of cast:
> import comtypes
> from comtypes.client import CreateObject
> ws = CreateObject("WScript.Shell")
> from comtypes.gen import IWshRuntimeLibrary
shortcut =
ws.CreateShortcut("test_shortcut.lnk").QueryInterface(IWshRuntimeLibrary.IWshShortcut)
> shortcut.TargetPath = "cmd.exe"
> shortcut.Arguments = "/K C:\Python24\python.exe"
> shortcut.Save()
--
Thanks,
Thomas
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
comtypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/comtypes-users