Create a shortcut using python

Source: Internet
Author: User

Create a shortcut using python

Can I do whatever I want on Windows with pywin32?

Yes.

#-*-Coding: cp936-*-# import osimport pythoncomfrom win32com. shell import shellfrom win32com. shell import shellcondef set_shortcut (filename, lnkname, iconname): # Remove the iconname parameter export cut = pythoncom if no special icon is required. coCreateInstance (shell. CLSID_ShellLink, None, pythoncom. CLSCTX_INPROC_SERVER, shell. IID_IShellLink) closed cut. setPath (filename) shortcut cut. setIconLocation (iconname, 0) # optional. if not, the file icon if OS is used by default. p Ath. splitext (lnkname) [-1]! = '. Lnk ': lnkname + = ". lnk "shortcut cut. queryInterface (pythoncom. IID_IPersistFile ). save (lnkname, 0) if _ name _ = "_ main _": # obtain the "Start" folder path. The key is the final parameter CSIDL_STARTUP, these parameters can be found in Microsoft's official API startup_path = shell. SHGetPathFromIDList (shell. SHGetSpecialFolderLocation (0, shellcon. CSIDL_STARTUP) # obtain the "desktop" folder path and replace the last parameter with CSIDL_DESKTOP to export top_path = shell. SHGetPathFromIDList (shell. SHGetSpecialFolderLocation (0, shellcon. CSIDL_DESKTOP) file_name = "" # full path of the file to create a shortcut, icon_name = "" # full path of the icon file (optional) lnk_name1 = startup_path + "\ My Desktop shortcuts. lnk "# create a shortcut in this path lnk_name2 = startup_path +" \ My Startup Group shortcut. lnk "set_shortcut (file_name, lnk_name1, icon_name) set_shortcut (file_name, lnk_name2, icon_name)


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.