Pythonを作成し、デスクトップを削除し、グループのショートカットを起動する例を共有します。


一、Pythonがデスクトップを作ってショートカット方式を作る2つの例
例一:

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':   
        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")

例二:まずActiveState ActivePythonをインストールします。  . これはwishellライブラリを持っていますので、

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) 
注:win 64はサポートされていません。
二、wishellモジュールを使ってデスクトップを作成、削除し、スタートグループのショートカット方式
アプリケーションを作成して発行する時、私達はユーザーのデスクトップにショートカット方式を作って、ユーザー操作に便利にしたいです。wishellモジュールは私達が必要な機能を提供します。
この関数は、デスクトップにプログラム自体のショートカットを作成します。

from os import path 
import winshell 

def create_shortcut_to_desktop(): 
    target = argv[0] 
    title = ' '
    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) 
以下の関数は、このプログラムのショートカットをデスクトップから削除します。

 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)
 
以下の関数は、ショートカットをセットアップするためのスタートグループを実現しました。

from os import path 
import winshell 

def create_shortcut_to_startup(): 
      target = argv[0] 
      title = ' '
      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)