-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_app.py
More file actions
311 lines (256 loc) · 11.9 KB
/
main_app.py
File metadata and controls
311 lines (256 loc) · 11.9 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
#!/usr/bin/env python3
"""
Mac Discord Rich Presence Tracker
A minimal application that tracks active Mac applications and updates Discord status.
"""
import time
import threading
import tkinter as tk
from tkinter import ttk, scrolledtext
import subprocess
import os
import logging
from pypresence import Presence
# Configure logging (console only)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class MacWindowMonitor:
"""Monitor active window on macOS using AppleScript"""
def get_active_app(self):
"""Get the currently active application name"""
try:
script = '''
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
return frontApp
end tell
'''
result = subprocess.run(
['osascript', '-e', script],
capture_output=True,
text=True,
timeout=5
)
if result.returncode == 0:
return result.stdout.strip()
except Exception as e:
logger.error(f"Error getting active app: {e}")
return None
class DiscordRPCManager:
"""Manage Discord Rich Presence connection and updates"""
def __init__(self, client_id="1404326169888690296"):
self.client_id = client_id
self.rpc = None
self.connected = False
def connect(self):
"""Connect to Discord RPC"""
try:
self.rpc = Presence(self.client_id)
self.rpc.connect()
self.connected = True
logger.info("Connected to Discord RPC")
return True
except Exception as e:
logger.error(f"Failed to connect to Discord: {e}")
self.connected = False
return False
def disconnect(self):
"""Disconnect from Discord RPC"""
if self.rpc and self.connected:
try:
self.rpc.close()
self.connected = False
logger.info("Disconnected from Discord RPC")
except Exception as e:
logger.error(f"Error disconnecting: {e}")
def update_status(self, app_name, custom_message=""):
"""Update Discord status with current app"""
if not self.connected or not self.rpc:
return False
try:
display_name = self.get_display_name(app_name)
details = f"Using {display_name}"
if custom_message:
details = f"{custom_message} - {display_name}"
self.rpc.update(
details=details,
state="Active",
start=int(time.time())
)
return True
except Exception as e:
logger.error(f"Failed to update status: {e}")
return False
def get_display_name(self, app_name):
"""Get display name for app (simple cleanup)"""
if not app_name:
return "Unknown App"
# Basic cleanup - remove common suffixes and clean up name
name = app_name.replace(".app", "").strip()
return name if name else "Unknown App"
class WhatIsBroDoingGUI:
"""Main GUI application"""
def __init__(self):
self.root = tk.Tk()
self.root.title("What Is Bro Doing - Mac Discord Tracker")
self.root.geometry("500x600")
# Initialize components
self.monitor = MacWindowMonitor()
self.discord_rpc = DiscordRPCManager()
# State variables
self.tracking = False
self.current_app = "None"
self.tracking_thread = None
self.create_widgets()
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
def create_widgets(self):
"""Create GUI widgets"""
# Configure root window styling
self.root.configure(bg="#2b2b2b")
# Title
title_label = tk.Label(self.root, text="Mac Discord Rich Presence Tracker",
font=("Arial", 18, "bold"), bg="#2b2b2b", fg="#ffffff")
title_label.grid(row=0, column=0, columnspan=2, pady=15)
# Connection status
self.connection_label = tk.Label(self.root, text="Discord: Disconnected",
fg="#ff6b6b", bg="#2b2b2b", font=("Arial", 11))
self.connection_label.grid(row=1, column=0, columnspan=2, pady=8)
# Status info frame
info_frame = tk.Frame(self.root, bg="#3c3c3c", relief="ridge", bd=1)
info_frame.grid(row=2, column=0, columnspan=2, pady=10, padx=20, sticky="ew")
# Current app
tk.Label(info_frame, text="Current App:", bg="#3c3c3c", fg="#cccccc",
font=("Arial", 10)).grid(row=0, column=0, sticky="w", padx=15, pady=8)
self.app_label = tk.Label(info_frame, text="None", font=("Arial", 10, "bold"),
bg="#3c3c3c", fg="#4ecdc4")
self.app_label.grid(row=0, column=1, sticky="w", pady=8)
# Tracking status
tk.Label(info_frame, text="Status:", bg="#3c3c3c", fg="#cccccc",
font=("Arial", 10)).grid(row=1, column=0, sticky="w", padx=15, pady=8)
self.status_label = tk.Label(info_frame, text="Stopped", fg="#ff6b6b",
bg="#3c3c3c", font=("Arial", 10, "bold"))
self.status_label.grid(row=1, column=1, sticky="w", pady=8)
# Buttons frame
button_frame = tk.Frame(self.root, bg="#2b2b2b")
button_frame.grid(row=3, column=0, columnspan=2, pady=15)
self.connect_btn = tk.Button(button_frame, text="Connect to Discord",
command=self.connect_discord, bg="#003D82", fg="white",
font=("Arial", 11, "bold"), relief="flat", padx=25, pady=10,
activebackground="#002A5C", cursor="hand2", bd=0,
highlightthickness=0, disabledforeground="white")
self.connect_btn.pack(side=tk.LEFT, padx=10)
self.toggle_btn = tk.Button(button_frame, text="Start Tracking",
command=self.toggle_tracking, bg="#1E7E34", fg="white",
font=("Arial", 11, "bold"), relief="flat", padx=25, pady=10,
activebackground="#155724", cursor="hand2", bd=0,
highlightthickness=0, disabledforeground="white")
self.toggle_btn.pack(side=tk.LEFT, padx=10)
# Custom message
message_frame = tk.Frame(self.root, bg="#2b2b2b")
message_frame.grid(row=4, column=0, columnspan=2, pady=15, padx=20, sticky="ew")
tk.Label(message_frame, text="Custom Message:", bg="#2b2b2b", fg="#cccccc",
font=("Arial", 10)).pack(anchor="w")
self.message_entry = tk.Entry(message_frame, width=50, bg="#3c3c3c", fg="#ffffff",
font=("Arial", 10), relief="flat", bd=5)
self.message_entry.pack(fill="x", pady=(5, 0))
# Log area
log_frame = tk.Frame(self.root, bg="#2b2b2b")
log_frame.grid(row=5, column=0, columnspan=2, pady=15, padx=20, sticky="nsew")
tk.Label(log_frame, text="Activity Log:", bg="#2b2b2b", fg="#cccccc",
font=("Arial", 10)).pack(anchor="w")
# Create text widget without scrollbar
self.log_text = tk.Text(log_frame, height=12, width=60, bg="#1e1e1e", fg="#ffffff",
font=("Consolas", 9), relief="flat", bd=5, state="disabled",
wrap="word")
self.log_text.pack(fill="both", expand=True, pady=(5, 0))
# Configure grid weights
self.root.grid_rowconfigure(5, weight=1)
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_columnconfigure(1, weight=1)
def log_message(self, message):
"""Add message to log area"""
timestamp = time.strftime("%H:%M:%S")
log_entry = f"[{timestamp}] {message}\n"
# Temporarily enable text widget to insert message
self.log_text.config(state="normal")
self.log_text.insert(tk.END, log_entry)
self.log_text.see(tk.END)
self.log_text.config(state="disabled") # Make it read-only again
logger.info(message)
def connect_discord(self):
"""Connect to Discord"""
if self.discord_rpc.connect():
self.connection_label.config(text="Discord: Connected", fg="#57f287")
self.connect_btn.config(text="Disconnect", command=self.disconnect_discord, bg="#FF3B30", activebackground="#D70015")
self.log_message("Connected to Discord successfully")
else:
self.log_message("Failed to connect to Discord")
def disconnect_discord(self):
"""Disconnect from Discord"""
self.discord_rpc.disconnect()
self.connection_label.config(text="Discord: Disconnected", fg="#ff6b6b")
self.connect_btn.config(text="Connect to Discord", command=self.connect_discord, bg="#003D82", activebackground="#002A5C")
self.log_message("Disconnected from Discord")
def toggle_tracking(self):
"""Toggle tracking on/off"""
if self.tracking:
self.stop_tracking()
else:
self.start_tracking()
def start_tracking(self):
"""Start tracking active applications"""
if not self.discord_rpc.connected:
self.log_message("Please connect to Discord first")
return
self.tracking = True
self.status_label.config(text="Running", fg="#57f287")
self.toggle_btn.config(text="Stop Tracking", bg="#FF3B30", activebackground="#D70015")
self.tracking_thread = threading.Thread(target=self.tracking_loop, daemon=True)
self.tracking_thread.start()
self.log_message("Started tracking applications")
def stop_tracking(self):
"""Stop tracking applications"""
self.tracking = False
self.status_label.config(text="Stopped", fg="#ff6b6b")
self.toggle_btn.config(text="Start Tracking", bg="#1E7E34", activebackground="#155724")
self.log_message("Stopped tracking applications")
def tracking_loop(self):
"""Main tracking loop"""
last_app = None
while self.tracking:
try:
current_app = self.monitor.get_active_app()
if current_app and current_app != last_app:
self.current_app = current_app
self.app_label.config(text=current_app)
custom_message = self.message_entry.get().strip()
if self.discord_rpc.update_status(current_app, custom_message):
self.log_message(f"Updated status: {current_app}")
last_app = current_app
time.sleep(2) # Check every 2 seconds
except Exception as e:
self.log_message(f"Error in tracking loop: {e}")
time.sleep(5)
def on_closing(self):
"""Handle window closing"""
self.tracking = False
if self.discord_rpc.connected:
self.discord_rpc.disconnect()
self.root.destroy()
def run(self):
"""Start the GUI application"""
self.log_message("Mac Discord Tracker started")
self.root.mainloop()
def main():
"""Main entry point"""
# Start the application
app = WhatIsBroDoingGUI()
app.run()
if __name__ == "__main__":
main()