2 Examples of Python-created shortcuts for desktop construction
Example one:
The code is as follows:
Import OS
Import pythoncom
From Win32com.shell Import shell
From Win32com.shell import Shellcon
Def createdesktoplnk (filename,lnkname):
shortcut = pythoncom. CoCreateInstance (
Shell. Clsid_shelllink, none,
pythoncom. Clsctx_inproc_server, Shell. Iid_ishelllink)
shortcut. SetPath (filename)
if Os.path.splitext (lnkname) [-1]! = '. lnk ':   &NBSP
Lnkname + = ". Lnk"
# Get Desktop path
DesktopPath = Shell. SHGetPathFromIDList (shell. SHGetSpecialFolderLocation (0,shellcon. Csidl_desktop)
lnkname = Os.path.join (desktoppath,lnkname)
shortcut. QueryInterface (Pythoncom. Iid_ipersistfile). Save (lnkname,0)
if __name__ = = ' __main__ ':
Createdesktoplnk (U "C:\Python27\python.exe", "Mypython")
Example two:
First you have to install ActiveState ActivePython. Because it's got a WinShell library.
The code is as follows:
From OS import path
Import WinShell
#----------------------------------------------------------------------
def create_shortcut_to_desktop (Target,title):
"" "Create Shortcut to Desktop" ""
s = path.basename (target)
FName = Path.splitext (s) [0]
WinShell. CreateShortcut (
Path = Path.join (Winshell.desktop (), fname + '. lnk '),
target = target,
icon= (target, 0),
Description=title)
Note: Win64 is not supported
Second, use WinShell module to create, delete Desktop, start group shortcut
When we write an application and publish it, we want to create a shortcut on the user's desktop to facilitate user operation, and the WinShell module provides the functions we need.
The following function creates a shortcut to the desktop for the program itself:
The code is as follows:
From OS import path
Import WinShell
Def create_shortcut_to_desktop ():
target = argv[0]
title = ' My Shortcuts '
s = path.basename (target)
FName = Path.splitext (s) [0]
WinShell. CreateShortcut (
Path = Path.join (Winshell.desktop (), fname + '. lnk '),
target = target,
icon= (target, 0),
Description=title)
The following function is implemented to remove the shortcut from the desktop:
The code is as follows:
Def delete_shortcut_from_startup ():
target = argv[0]
s = path.basename (target)
FName = Path.splitext (s) [0]
Delfile = Path.join (Winshell.startup (), fname + '. Lnk ')
Winshell.delete_file (Delfile)
The following function implements the Setup shortcut to the Startup group:
The code is as follows:
From OS import path
Import WinShell
Def create_shortcut_to_startup ():
target = argv[0]
title = ' My Shortcuts '
s = path.basename (target)
FName = Path.splitext (s) [0]
WinShell. CreateShortcut (
Path = Path.join (Winshell.startup (),
FName + '. lnk '),
target = target,
icon= (target, 0),
Description=title)
Python Create, delete desktop, launch group shortcuts for example sharing