File size: 2,336 Bytes
10865e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
# link.py
# From a demo by Mark Hammond, corrupted by Mike Fletcher
# (and re-corrupted by Mark Hammond :-)
import os

import pythoncom
from win32com.shell import shell


class PyShortcut:
    def __init__(self):
        self._base = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink,
            None,
            pythoncom.CLSCTX_INPROC_SERVER,
            shell.IID_IShellLink,
        )

    def load(self, filename):
        # Get an IPersist interface
        # which allows save/restore of object to/from files
        self._base.QueryInterface(pythoncom.IID_IPersistFile).Load(filename)

    def save(self, filename):
        self._base.QueryInterface(pythoncom.IID_IPersistFile).Save(filename, 0)

    def __getattr__(self, name):
        if name != "_base":
            return getattr(self._base, name)


if __name__ == "__main__":
    import sys

    if len(sys.argv) < 2:
        print(
            "Usage: %s LinkFile [path [, args[, description[, working_dir]]]]\n\nIf LinkFile does not exist, it will be created using the other args"
        )
        sys.exit(1)
    file = sys.argv[1]
    shortcut = PyShortcut()
    if os.path.exists(file):
        # load and dump info from file...
        shortcut.load(file)
        # now print data...
        print(
            "Shortcut in file %s to file:\n\t%s\nArguments:\n\t%s\nDescription:\n\t%s\nWorking Directory:\n\t%s\nItemIDs:\n\t<skipped>"
            % (
                file,
                shortcut.GetPath(shell.SLGP_SHORTPATH)[0],
                shortcut.GetArguments(),
                shortcut.GetDescription(),
                shortcut.GetWorkingDirectory(),
                # shortcut.GetIDList(),
            )
        )
    else:
        if len(sys.argv) < 3:
            print(
                "Link file does not exist\nYou must supply the path, args, description and working_dir as args"
            )
            sys.exit(1)
        # create the shortcut using rest of args...
        data = map(
            None,
            sys.argv[2:],
            ("SetPath", "SetArguments", "SetDescription", "SetWorkingDirectory"),
        )
        for value, function in data:
            if value and function:
                # call function on each non-null value
                getattr(shortcut, function)(value)
        shortcut.save(file)