Repository navigation
Expand file tree
/
Copy pathScriptManager.py
More file actions
255 lines (204 loc) · 8.08 KB
/
Copy pathScriptManager.py
File metadata and controls
255 lines (204 loc) · 8.08 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# importing modules #
from configparser import ConfigParser
import customtkinter as ctk
from PIL import Image
import tkinter as tk
import subprocess
import threading
import platform
import pystray
import psutil
import time
import os
if platform.system() == "Windows": # Windows
import win32con
import win32gui
# defining functions #
def CheckProcess(pname):
return pname in (p.name() for p in psutil.process_iter())
def Changeini(option,choice):
os.remove("config.ini")
try:
Config.set(section="Default",option=option,value=str(choice))
except:
print("Could not change configuration")
finally:
with open("Config.ini","w") as f:
Config.write(f)
f.close()
def cfg(option):
try:
main = Config.get("Default",option)
return main
except:
print("Could not open configuration")
def KillProc(name): # os.system("taskkill /f /im Your_Process_Name.exe") is an alt for windows only
for proc in psutil.process_iter():
if proc.name() == name:
proc.kill()
def cfg(option):
main = Config.get("Default",option)
if option == "minimize_to_tray":
if main or main == "True":
return 1
elif not main or main == "False":
return 0
else:
return main
def GetSize(option):
main = Config.get("Default","resolution")
height, width = main.split("x")
if option == "width":
return width
if option == "height":
return height
def raise_above_all(window):
window.lift()
window.attributes('-topmost', True)
window.after_idle(window.attributes,'-topmost', False)
window.focus_force()
def Changeini(option,choice):
os.remove("Config.ini")
try:
Config.set(section="Default",option=option,value=str(choice))
except:
print("Could not change configuration")
finally:
with open("Config.ini","w") as f:
Config.write(f)
f.close()
def pathBrowse(entry,title):
directory = ctk.filedialog.askdirectory(title=title)
entry.delete(0,"end")
entry.insert(0,directory)
def executableBrowse(entry,title):
directory = ctk.filedialog.askopenfilename(title=title,filetypes=[("Executables",".exe .jar .appimage .py .pyw .pyi")])
entry.delete(0,"end")
entry.insert(0,directory)
def getNameFrompath(path):
filename = os.path.basename(path)
program_name = os.path.splitext(filename)[0]
return program_name
# defining variables #
Config = ConfigParser()
Config.read(r"Config.ini")
Jobs = eval(cfg("Jobs"))
running_Jobs = {}
# classes #
class App(ctk.CTk):
def __init__(self):
super().__init__()
def loadCfg(self):
self.title(cfg("title"))
self.geometry(cfg("resolution"))
self.resizable(cfg("resizable"), cfg("resizable"))
self._set_appearance_mode(cfg("appearance_mode"))
loadCfg(self)
def ShowTray():
self.withdraw()
logo = Image.open(r"script.ico")
menu = pystray.Menu(
pystray.MenuItem("Show", HideTray, default=True),
pystray.MenuItem("Quit", Quit)
)
icon = pystray.Icon("ScriptManager", logo , "ScriptManager", menu)
icon.run()
def Quit(icon,item):
icon.stop()
self.destroy()
def HideTray(icon,item):
icon.stop()
self.after(0,self.deiconify)
if cfg("minimize_to_tray") == "True" or cfg("minimize_to_tray") == True :
self.protocol('WM_DELETE_WINDOW', ShowTray)
if cfg("start_minimized") == "True" or cfg("start_minimized") == True:
ShowTray()
raise_above_all(self)
self.iconbitmap("script.ico")
self.MainFrame = MainFrame(self)
self.Frames = [self.MainFrame]
def frameswitch(self,target):
for Frame in self.Frames:
if Frame != target:
Frame.pack_forget()
else:
Frame.pack(expand=True,fill="both")
def loop():
while True:
time.sleep(3)
for item in Jobs.items():
if CheckProcess(item[0]):
if running_Jobs.get(item[0]) is None:
print(" [+] " + item[0] + " running")
p = subprocess.Popen(["python", item[1]],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for line in p.stdout:
line = line.decode("utf-8").strip()
NameOfScript = getNameFrompath(item[1])
print(f" [{NameOfScript}] " + line)
running_Jobs[item[0]] = item[1]
try:
for item in running_Jobs.items():
if not CheckProcess(item[0]):
running_Jobs.pop(item[0])
print(" [-] " + item[0] + " Closed")
except:
print(" [*] No Jobs Running")
thread = threading.Thread(target=loop)
thread.start()
self.mainloop()
class MainFrame(ctk.CTkFrame):
def __init__(self,parent):
try:
fgc = "#" + str(int(cfg("main_colour")))
except:
fgc = str(cfg("main_colour"))
super().__init__(parent,fg_color=fgc)
self.pack(fill="both",expand=True)
self.create_widgets()
def create_widgets(self):
self.JobsView = ctk.CTkScrollableFrame(self,width=350,corner_radius=0)
self.JobsView.pack(fill="both",expand=True)
def delete(label, btn, job):
label.destroy()
btn.destroy()
Jobs.pop(job)
Changeini("Jobs", Jobs)
for job in Jobs.copy().items():
label = ctk.CTkLabel(self.JobsView,height=20,width=300,text=str(str(job[0]) + " --> " + str(job[1])),font=("Airel",30))
label.pack(pady=5,expand=True)
Delete = ctk.CTkButton(self.JobsView,text="Delete",font=("Airel",30),width=60)
Delete.configure(command=lambda label=label, Delete=Delete, job=job: delete(label, Delete, job[0]))
Delete.pack(pady=5,expand=True)
def AddJob():
InputWindow = ctk.CTkToplevel()
InputWindow.wm_title("Add a Job")
InputWindow.geometry("400x220")
InputWindow.resizable(False,False)
InputWindow.attributes('-topmost', True)
ptw_label = ctk.CTkLabel(InputWindow,text="The name of the Program to watch :")
ptw_label.pack()
ptw_inputfield = ctk.CTkEntry(InputWindow)
ptw_inputfield.pack()
ptr_label = ctk.CTkLabel(InputWindow,text="The path of the Program to run :")
ptr_label.pack()
ptr_inputfield = ctk.CTkEntry(InputWindow)
ptr_inputfield.pack()
ptr_browse = ctk.CTkButton(InputWindow,text="Browse",command=lambda:executableBrowse(ptr_inputfield,"Program to be executed"))
ptr_browse.pack(pady=10)
def Add():
Jobs[str(ptw_inputfield.get())] = str(ptr_inputfield.get())
Changeini("Jobs",Jobs)
_job = Jobs[str(ptw_inputfield.get())]
label = ctk.CTkLabel(self.JobsView,height=20,width=300,text=str(str(ptw_inputfield.get()) + " --> " + str(ptr_inputfield.get())),font=("Airel",30))
label.pack(pady=5,expand=True)
Delete = ctk.CTkButton(self.JobsView,text="Delete",font=("Airel",30),width=60)
Delete.configure(command=lambda label=label, Delete=Delete, job=_job: delete(label, Delete, job[0]))
Delete.pack(pady=5,expand=True)
InputWindow.destroy()
AddButton = ctk.CTkButton(InputWindow,text="Add",command=lambda:Add())
AddButton.pack(pady=10)
Add_btn = ctk.CTkButton(self.JobsView,text="Add",font=("Airel",30),width=60,command=lambda : AddJob())
Add_btn.pack(pady=70,side="bottom",expand=True)
# runtime #
if __name__ == "__main__" :
App()