-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAutoRunManager.py
More file actions
311 lines (261 loc) · 9.25 KB
/
CustomAutoRunManager.py
File metadata and controls
311 lines (261 loc) · 9.25 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
import os
import sys
import ctypes
import hashlib
import json
import webbrowser
import win32com.client
# ======================================================
# CONFIG
# ======================================================
DEBUG = False # set True untuk troubleshooting
APP_NAME = "Custom AutoRun Manager"
COMPANY = "UNAMED666"
# ======================================================
# ADMIN RELAUNCH (GUI SAFE)
# ======================================================
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if not is_admin():
ctypes.windll.shell32.ShellExecuteW(
None,
"runas",
sys.executable.replace("python.exe", "pythonw.exe"),
f'"{os.path.abspath(__file__)}"',
None,
1
)
sys.exit(0)
# ======================================================
# PATHS & CONSTANTS
# ======================================================
CREATE_NO_WINDOW = subprocess.CREATE_NO_WINDOW
APP_DIR = os.path.join(os.getenv("APPDATA"), "CustomAutoRunManager")
DATA_FILE = os.path.join(APP_DIR, "data.json")
STARTUP_DIR = os.path.join(
os.getenv("APPDATA"),
"Microsoft", "Windows", "Start Menu", "Programs", "Startup"
)
os.makedirs(APP_DIR, exist_ok=True)
# ======================================================
# THEME (DARK)
# ======================================================
BG = "#1e1e1e"
FG = "#e0e0e0"
FG_DIM = "#9a9a9a"
ACCENT = "#3a9cff"
BTN_BG = "#2d2d2d"
SEL_BG = "#3a3a3a"
# ======================================================
# UTILITIES
# ======================================================
def run_silent(cmd):
subprocess.run(
cmd,
creationflags=CREATE_NO_WINDOW,
stdout=None if DEBUG else subprocess.DEVNULL,
stderr=None if DEBUG else subprocess.DEVNULL
)
def task_name_from_path(path):
base = os.path.splitext(os.path.basename(path))[0]
h = hashlib.md5(path.encode("utf-8")).hexdigest()[:8]
return f"CustomAutoRun_{base}_{h}"
# ======================================================
# ADMIN TASK (SCHEDULER)
# ======================================================
def create_admin_task(path):
run_silent([
"schtasks", "/create", "/f",
"/sc", "onlogon",
"/rl", "highest",
"/tn", task_name_from_path(path),
"/tr", f'"{path}"'
])
def delete_admin_task(path):
run_silent([
"schtasks", "/delete", "/f",
"/tn", task_name_from_path(path)
])
def admin_task_exists(path):
r = subprocess.run(
["schtasks", "/query", "/tn", task_name_from_path(path)],
creationflags=CREATE_NO_WINDOW,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return r.returncode == 0
# ======================================================
# LOCAL STARTUP (SHORTCUT)
# ======================================================
def unique_shortcut_name(base):
name, ext = os.path.splitext(base)
i = 1
candidate = base
while os.path.exists(os.path.join(STARTUP_DIR, candidate)):
candidate = f"{name} ({i}){ext}"
i += 1
return candidate
def create_startup_shortcut(target):
shell = win32com.client.Dispatch("WScript.Shell")
base = os.path.basename(target) + ".lnk"
unique = unique_shortcut_name(base)
link_path = os.path.join(STARTUP_DIR, unique)
shortcut = shell.CreateShortcut(link_path)
shortcut.TargetPath = target
shortcut.WorkingDirectory = os.path.dirname(target)
shortcut.Save()
return unique
def delete_startup_shortcut(name):
p = os.path.join(STARTUP_DIR, name)
if os.path.exists(p):
os.remove(p)
# ======================================================
# DATA STORAGE
# ======================================================
def load_data():
if not os.path.exists(DATA_FILE):
return {"admin": [], "local": []}
with open(DATA_FILE, "r", encoding="utf-8") as f:
return json.load(f)
def save_data(data):
with open(DATA_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
data_store = load_data()
# ======================================================
# UI
# ======================================================
root = tk.Tk()
root.title(APP_NAME)
root.geometry("900x500")
root.configure(bg=BG)
root.resizable(True, True)
# ---------- HEADER ----------
header = tk.Frame(root, bg=BG)
header.pack(fill=tk.X, pady=(8, 4))
header.columnconfigure(0, weight=1)
header.columnconfigure(1, weight=1)
header.columnconfigure(2, weight=1)
tk.Label(header, text="UNAMED666", fg=BG, bg=BG).grid(row=0, column=0)
tk.Label(
header,
text=APP_NAME,
font=("Monotype Corsiva", 24, "bold"),
bg=BG,
fg=FG
).grid(row=0, column=1)
author = tk.Label(
header,
text="by: UNAMED666",
fg="#FFD700",
bg=BG,
cursor="hand2",
font=("Segoe UI", 9, "underline")
)
author.grid(row=0, column=2, sticky="e", padx=14)
author.bind("<Button-1>", lambda e: webbrowser.open("https://github.com/unamed666/CustomAutoRunManager"))
# ---------- MAIN ----------
main = tk.Frame(root, bg=BG)
main.pack(fill=tk.BOTH, expand=True, padx=12, pady=8)
# Admin column
admin_frame = tk.Frame(main, bg=BG)
admin_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 6))
tk.Label(admin_frame, text="Administrator", bg=BG, fg=FG).pack(anchor="w")
dataA = tk.Listbox(admin_frame, bg=BTN_BG, fg=FG,
selectbackground=SEL_BG, highlightthickness=0)
dataA.pack(fill=tk.BOTH, expand=True)
# Local column
local_frame = tk.Frame(main, bg=BG)
local_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=(6, 0))
tk.Label(local_frame, text="Local", bg=BG, fg=FG).pack(anchor="w")
dataB = tk.Listbox(local_frame, bg=BTN_BG, fg=FG,
selectbackground=SEL_BG, highlightthickness=0)
dataB.pack(fill=tk.BOTH, expand=True)
# Remove button
btn_remove = tk.Button(
root, text="Remove",
bg=BTN_BG, fg=FG,
activebackground=SEL_BG,
relief="flat",
state=tk.DISABLED, width=16
)
btn_remove.pack(pady=8)
# ======================================================
# CONTEXT MENUS
# ======================================================
menu_admin = tk.Menu(root, tearoff=0, bg=BTN_BG, fg=FG)
menu_local = tk.Menu(root, tearoff=0, bg=BTN_BG, fg=FG)
def add_admin():
path = filedialog.askopenfilename(filetypes=[("Executable", "*.exe")])
if not path or path in data_store["admin"]:
return
create_admin_task(path)
data_store["admin"].append(path)
save_data(data_store)
dataA.insert(tk.END, path)
def add_local():
path = filedialog.askopenfilename(filetypes=[("Executable / Script", "*.exe *.ahk")])
if not path:
return
shortcut = create_startup_shortcut(path)
data_store["local"].append({"path": path, "shortcut": shortcut})
save_data(data_store)
dataB.insert(tk.END, shortcut)
def open_task_scheduler():
os.startfile("taskschd.msc")
def open_startup_folder():
os.startfile(STARTUP_DIR)
menu_admin.add_command(label="Add (Administrator)", command=add_admin)
menu_admin.add_command(label="Open Task Scheduler", command=open_task_scheduler)
menu_local.add_command(label="Add (Local)", command=add_local)
menu_local.add_command(label="Open Startup Folder", command=open_startup_folder)
def show_menu(menu, listbox, event):
i = listbox.nearest(event.y)
listbox.selection_clear(0, tk.END)
listbox.selection_set(i)
menu.tk_popup(event.x_root, event.y_root)
# ======================================================
# EVENTS
# ======================================================
def on_select(event):
btn_remove.config(
state=tk.NORMAL if (dataA.curselection() or dataB.curselection()) else tk.DISABLED
)
def on_remove():
if dataA.curselection():
i = dataA.curselection()[0]
path = dataA.get(i)
delete_admin_task(path)
dataA.delete(i)
data_store["admin"].remove(path)
elif dataB.curselection():
i = dataB.curselection()[0]
name = dataB.get(i)
delete_startup_shortcut(name)
dataB.delete(i)
data_store["local"] = [e for e in data_store["local"] if e["shortcut"] != name]
save_data(data_store)
btn_remove.config(state=tk.DISABLED)
# ======================================================
# INITIAL LOAD
# ======================================================
for p in data_store["admin"]:
if admin_task_exists(p):
dataA.insert(tk.END, p)
for e in data_store["local"]:
if os.path.exists(os.path.join(STARTUP_DIR, e["shortcut"])):
dataB.insert(tk.END, e["shortcut"])
# ======================================================
# BINDINGS
# ======================================================
dataA.bind("<<ListboxSelect>>", on_select)
dataB.bind("<<ListboxSelect>>", on_select)
dataA.bind("<Button-3>", lambda e: show_menu(menu_admin, dataA, e))
dataB.bind("<Button-3>", lambda e: show_menu(menu_local, dataB, e))
btn_remove.config(command=on_remove)
root.mainloop()