Python Create, delete desktop, launch group shortcuts for example sharing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.