-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
135 lines (117 loc) · 4.35 KB
/
search.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
import os,Tkinter,time,thread,tkMessageBox
def spider():
crawler = os.walk("/Users/kmspriyatham/Movies/My Movies")
appender(crawler)
#crawler = os.walk("/Users/kmspriyatham/Codes/")
#appender(crawler)
crawler = os.walk("/Users/kmspriyatham/Documents/")
appender(crawler)
crawler = os.walk("/Users/kmspriyatham/Academics/")
appender(crawler)
crawler = os.walk("/Users/kmspriyatham/Music/iTunes/iTunes Media/Music/")
appender(crawler)
for items in os.listdir("/Applications/"):
filesList.append(os.path.join("/Applications",items))
rankFileHandler()
def appender(crawler):
for dirPath,dirNames,fileNames in crawler:
for items in fileNames:
if(items != ".DS_Store"):
filesList.append(os.path.join(dirPath,items))
def search():
prevSearchTerm = ""
count = 0
while True:
searchTerm = searchField.get()
if(searchTerm == ""):
searchResults.delete(0,Tkinter.END)
searchResultsList[:] = []
prevSearchTerm = ""
elif(searchTerm == prevSearchTerm):
pass
else:
count = 0
searchResults.delete(0,Tkinter.END)
searchResultsList[:] = []
for files in filesList:
if searchTerm.lower() in os.path.basename(files.lower()):
searchResults.insert(Tkinter.END,os.path.split(files)[1])
searchResultsList.append(files)
count += 1
if count > 27:
break
prevSearchTerm = searchTerm
time.sleep(0.001)
def fileOpener(PressEvent):
try:
fileName = searchResultsList[searchResults.index(Tkinter.ACTIVE)]
fileToBeOpened = str(fileName)
fileToBeOpened = nameModifier(fileToBeOpened)
openCommand = "open " + fileToBeOpened
os.system(openCommand)
return "break"
except Tkinter.TclError:
print "No File Selected"
def fileDeleter(PressEvent):
if(tkMessageBox.askokcancel("Delete","Are you sure you want to delete the file?")):
try:
fileName = searchResultsList[searchResults.index(Tkinter.ACTIVE)]
searchResults.delete(Tkinter.ACTIVE)
os.remove(fileName)
filesList.pop(filesList.index(fileName))
searchResultsList.pop(searchResultsList.index(fileName))
tkMessageBox.showinfo("Alert!","File Deleted")
return "break"
except Tkinter.TclError:
print "Couldn't be deleted"
def nameModifier(fileName):
fileName = fileName.replace(" ","\ ")
fileName = fileName.replace("(","\(")
fileName = fileName.replace(")","\)")
fileName = fileName.replace("'","\\'")
fileName = fileName.replace(",","\,")
fileName = fileName.replace("[","\[")
fileName = fileName.replace("]","\]")
fileName = fileName.replace("&","\&")
return fileName
def focusShiftDown(PressEvent):
searchResults.focus_set()
searchResults.activate(0)
return "break"
def focusShiftUp(PressEvent):
if(searchResults.index(Tkinter.ACTIVE) == 0):
searchField.focus_set()
return "break"
def quitSearch():
exit(0)
def rankFileHandler():
rankFile = open("rankFile.txt","r+")
for items in filesList:
rankFile.write(os.path.split(items)[1] )
rankFile.close()
filesList = []
searchResultsList = []
searchWindow = Tkinter.Tk()
searchWindow.title("Search")
searchWindow.geometry("700x525+350+100")
searchWindow.minsize(700,525)
searchWindow.maxsize(700,525)
searchWindow.configure(background = "white")
os.system("""osascript -e 'tell application "System Events" to set frontmost of application process "Python" of application "System Events" to true'""")
searchField = Tkinter.Entry(searchWindow)
searchField.focus_set()
searchField.bind("<Down>",focusShiftDown)
searchField.pack()
searchResults = Tkinter.Listbox(searchWindow)
searchResults.config(height = 27,width = 72)
searchResults.bind("<Return>",fileOpener)
searchResults.bind("<Double-Button-1>",fileOpener)
searchResults.bind("<BackSpace>",fileDeleter)
searchResults.bind("<Up>",focusShiftUp)
searchResults.pack()
quitButton = Tkinter.Button(searchWindow,text = "Quit",command = quitSearch)
quitButton.pack()
thread.start_new_thread(search,())
thread.start_new_thread(spider,())
searchWindow.mainloop()