import time
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import win32gui # Windows-only; use AppKit for Mac
LOG_FILE = "screen_time_log.csv"
TRACK_DURATION_MINUTES = 1 # Change as needed
INTERVAL_SECONDS = 5
def get_active_window_title():
try:
return win32gui.GetWindowText(win32gui.GetForegroundWindow())
except:
return "Unknown"
def track_screen_time(duration_minutes=1, interval=5):
end_time = time.time() + (duration_minutes * 60)
usage_log = []
print("Tracking started... Press Ctrl+C to stop early.")
while time.time() < end_time:
window = get_active_window_title()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
usage_log.append((timestamp, window))
time.sleep(interval)
# Save to CSV
df = pd.DataFrame(usage_log, columns=["Timestamp", "Window"])
df.to_csv(LOG_FILE, index=False)
print(f"Tracking complete. Data saved to {LOG_FILE}")
return df
def generate_report(csv_file):
df = pd.read_csv(csv_file)
df["Window"] = df["Window"].fillna("Unknown")
# Count frequency of window usage
summary = df["Window"].value_counts().head(10) # Top 10 apps/windows
# Plot
plt.figure(figsize=(10, 6))
summary.plot(kind="bar", color="skyblue")
plt.title("Most Used Windows/Apps")
plt.xlabel("Window Title")
plt.ylabel("Active Window Count")
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
plt.show()
if __name__ == "__main__":
df = track_screen_time(TRACK_DURATION_MINUTES, INTERVAL_SECONDS)
generate_report(LOG_FILE)