The past couple of days, I've needed a way to create a shortcut on a user's desktop during the login process. I had a way that worked for most shortcuts, but I just could not figure out how to do this one.
Here's the setup: I required a way to make a shortcut that went to a specific website using a specific web browser. My first try is as follows:
import win32com.client import winshell userDesktop = winshell.desktop() shell = win32com.client.Dispatch('WScript.Shell') shortcut = shell.CreateShortCut(userDesktop + '\\Zimbra Webmail.lnk') shortcut.Targetpath = r'C:\Program Files\Mozilla Firefox\firefox.exe' shortcut.Arguments = 'http://mysite.com/auth/preauth.php' shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox' shortcut.save()
The problem with this code is that it creates the following as the Target Path for the shortcut:
"C:\"C:\Program Files\Mozilla Firefox\firefox.exe" http://mysite.com/auth/preauth.php
We'll come back to the solution in a moment, but first I'm guessing that some of you may be wondering how I knew where Mozilla would be. Well, at my place of employment, we place Mozilla in a specific place on the hard drive and if one of my login scripts doesn't detect that location, then the script will automatically reinstall the program. In talking about this subject with the very knowledgeable people on the pywin32 mailing list, they reminded me that I should be getting the location via the registry.
Tim Roberts pointed out this method of discovery:
import _winreg ffkey = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Mozilla\\Mozilla Firefox') ffver = _winreg.QueryValueEx( ffkey, 'CurrentVersion' ) print ffver ffmainkey = _winreg.OpenKey( ffkey, sub + "\\Main" ) ffpath = _winreg.QueryValueEx( ffmainkey, 'PathToExe' ) _winreg.CloseKey( ffkey ) _winreg.CloseKey( ffmainkey )
Whereas Duncan Booth on c.l.py pointed out this method:
import _winreg print _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT, 'FirefoxURL\shell\open\command')
It turns out there are 3 ways to do this, all of which are related.
We'll look at the last two. First, we'll fix my code. This method of adding an "Arguments" parameter was pointed out to me by Chris at c.l.py and also by Roger Upole on the pywin32 list. See below:
import win32com.client import winshell shortcut = shell.CreateShortCut(userDesktop + '\\MyShortcut.lnk') shortcut.TargetPath = r'Program Files\Mozilla Firefox\firefox.exe' shortcut.Arguments = r'http://mysite.com/auth/preauth.php' shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox' shortcut.save()
Finally, Tim Golden pointed out that his winshell module can do what I wanted. He told me that winshell wraps "the IShellLink
functionality" over on the pywin32 list. You can see the results below:
import os import winshell winshell.CreateShortcut ( Path=os.path.join (winshell.desktop (), "Zimbra Monkeys.lnk"), Target=r"c:\Program Files\Mozilla Firefox\firefox.exe", Arguments="http://mysite.com/auth/preauth.php", Description="Open http://localhost with Firefox", StartIn=r'C:\Program Files\Mozilla Firefox' )
Now you have a couple of new tools to use in your projects too.
Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary