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

I dislike filling my desktop with shortcut icons in Windows. This code enables creating shortcuts in a CSV file.

Python, 65 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
# Shortcut Utility
# FB - 20150329
# Create a file named "SHORTCUTS.CSV"
# in the same directory as this script
# which should contain lines in the format:
# target,description
# Example:
# c:\,c: drive
# c:\windows,windows directory
# c:\windows\notepad.exe,notepad
# C:\Users\desktop.ini,desktop.ini
# http://google.com,google

import Tkinter as tk
import os

targets = []
descriptions = []
targetTypes = [] # url/file/dir

def LoadCSV():
    f = open('SHORTCUTS.CSV','r')
    lines = f.readlines()
    for line in lines:
        lineList = line.strip().split(',')
        target = lineList[0]
        targets.append(target)
        descriptions.append(lineList[1])
        if target.lower().startswith('http'):
            targetTypes.append('url')
        elif os.path.isfile(target):
            targetTypes.append('file')
        elif os.path.isdir(target):
            targetTypes.append('dir')

class App:
    def __init__(self, root):
        self.root = root
        LoadCSV()
        for i, description in enumerate(descriptions):
            color = "#000000"
            if targetTypes[i] == "url":
                color = "#ff0000"
            elif targetTypes[i] == "file":
                color = "#00ff00"
            elif targetTypes[i] == "dir":
                color = "#0000ff"
            link = tk.Label(text = description, foreground = color)
            link.bind("<1>", lambda event, text = description: self.click_link(event, text))
            link.pack()

    def click_link(self, event, description):
        for i, desc in enumerate(descriptions):
            if description == desc:
                if targetTypes[i] == "url":
                    os.system('explorer ' + targets[i])
                elif targetTypes[i] == "file":
                    os.system(targets[i])
                elif targetTypes[i] == "dir":
                    os.system('explorer ' + targets[i])
                break

root = tk.Tk()
app = App(root)
root.mainloop()