Welcome, guest | Sign In | My Account | Store | Cart

i've made some shortcut on my usb key for launchy and i had always to change them if on one pc the usb drive was i: on an other it was k: it was such a pain each time. Now it change all the shortcut automatically.

Python, 71 lines
 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
60
61
62
63
64
65
66
67
68
69
70
71
import sys,glob,re
import pythoncom
from win32com.shell import shell
import win32com
import win32com.client
import string
"""
By bussiere bussiere @at gmail.com
thanks to :
http://www.blog.pythonlibrary.org/
http://www.blog.pythonlibrary.org/?p=21
and :
http://codesnippets.joyent.com/tag/python
http://codesnippets.joyent.com/tag/python#post529
"""

__Author__ ="bussiere"
__Email__ = "bussiere @at gmail.com"
__Titre__ = "Changing shortcut on a usb key v2"
__Description__ = "Changing the drive of a list of shortcut automatically must be placed in the shortcut directory on the usb key"
__Discussion__ = "i've made some shortcut on my usb key for  http://www.launchy.net/ launchy and i had always to change them if on one pc the usb drive was i: on an other it was k: it was such a pain each time. Now it change all the shortcut automatically."
__Tags__ ="Usb shortcut windows key raccourcis"


class Win32Shortcut:
    def __init__(self, lnkname):
        self.shortcut = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink, None,
            pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
        self.shortcut.QueryInterface(pythoncom.IID_IPersistFile).Load(lnkname)

    def __getattr__(self, name):
        return getattr(self.shortcut, name)


def main():
    
    shell2 = win32com.client.Dispatch('WScript.Shell')
    # here we just get the drive where is the usb key
    drive = sys.path[0][0:2]
    #here we list all the file on the shortcut directory
    files = glob.glob(sys.path[0]+'/*')
    # here we take one file path
    path = glob.glob(sys.path[0])[0]
    #we normalize the path for python
    path = string.replace(path,'\\','\\\\')
    # we prepare a regexp for finding the shortcuts
    p = re.compile('\.lnk')
    
    for file in files :
        # we list all the files and find the shortcuts .lnk
    	if p.search(file) :
            # we get the shortcut 
            s = Win32Shortcut(file)
            #we take the target directory of the shortcut
            itemPath = s.GetPath(0)[0]
            #we normalize the path of the shortcut
            file = string.replace(file,'\\','\\\\')
            # we overwrite the shortcut (same directory, same name).
            shortcut = shell2.CreateShortCut(file)
            #we replace the target path (drive = usb drive, path without the drive = itemPath[2:])
            shortcut.Targetpath =  drive + itemPath[2:]
            #we set the directory drive
            shortcut.WorkingDirectory = path
    		#we save the shortcut
            shortcut.save()



if __name__ == "__main__":
    main()

i've made some shortcut on my usb key for http://www.launchy.net/ launchy and i had always to change them if on one pc the usb drive was i: on an other it was k: it was such a pain each time. Now it change all the shortcut automatically.