1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import win32com.client
import os.path
def createShortcut(linkname, targetname='', wdir='', args='', icon='', description=''):
shell = win32com.client.Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(linkname)
if args != '':
shortcut.Arguments = args
if icon != '':
shortcut.IconLocation = icon
if wdir != '':
shortcut.WorkingDirectory = wdir
if description != '':
shortcut.Description = description
if os.path.isdir(targetname):
print 'isdir!'
targetname = os.path.abspath(targetname) + os.sep
shortcut.Targetpath = targetname
shortcut.save()
if __name__ == "__main__":
import Tkinter
import tkFileDialog
linkname = u"noname.lnk"
targetname = u""
def guiCreateShortcut(targetname, wdir='', args='', icon='', description=''):
global linkname
tempname = tkFileDialog.asksaveasfilename(title="Set shortlink name", parent=root, initialdir=linkname, initialfile=linkname, filetypes=[("shortcuts", ".lnk")], defaultextension=".lnk")
if tempname!= "":
linkname = tempname
createShortcut(linkname, targetname, wdir, 'Pierre Faller', icon, description)
else:
print "guiCreateShortcut abort"
def createFileShortcut():
global targetname
tempname = tkFileDialog.askopenfilename(title="Choose file to point to", parent=root, initialdir=targetname, initialfile=targetname)
if tempname != "":
targetname = tempname
guiCreateShortcut(targetname, os.path.dirname(targetname), 'Pierre Faller de la Michaudiere')
else:
print 'createFileShortcut abort'
def createDirShortcut():
global targetname
tempname = tkFileDialog.askdirectory(title="Choose directory to point to", parent=root, initialdir=targetname)
if tempname != None:
targetname = tempname
guiCreateShortcut(targetname)
else:
print 'createDirShortcut abort'
root = Tkinter.Tk()
Tkinter.Button(root, text="Create a file shortcut", command=createFileShortcut, width=80, height=3).grid()
Tkinter.Button(root, text="Create a directory shortcut", command=createDirShortcut, width=80, height=3).grid()
root.mainloop() |
Partager