Screen Time Tracker

import psutil

import json

import time

import os

import threading

from datetime import datetime, date

from pathlib import Path

from collections import defaultdict

 

# ============================================================

# CONFIGURATION

# ============================================================

 

DATA_FILE      = "screen_time_data.json"

POLL_INTERVAL  = 3       # seconds between process checks

DAILY_LIMIT    = {}      # optional per-app daily limits in minutes

 

# ============================================================

# LOAD & SAVE DATA

# ============================================================

 

def load_data():

    if Path(DATA_FILE).exists():

        try:

            with open(DATA_FILE, "r") as f:

                return json.load(f)

        except:

            return {}

    return {}

 

 

def save_data(data):

    with open(DATA_FILE, "w") as f:

        json.dump(data, f, indent=2)

 

 

# ============================================================

# GET ACTIVE PROCESSES

# ============================================================

 

def get_running_apps():

    """Return a set of currently running process names (lowercased)."""

    apps = set()

    for proc in psutil.process_iter(["name", "status"]):

        try:

            if proc.info["status"] == psutil.STATUS_RUNNING:

                name = proc.info["name"].lower().replace(".exe", "")

                apps.add(name)

        except (psutil.NoSuchProcess, psutil.AccessDenied):

            pass

    return apps

 

 

def get_all_processes():

    """Return all running process names (including sleeping/idle)."""

    apps = set()

    for proc in psutil.process_iter(["name"]):

        try:

            name = proc.info["name"].lower().replace(".exe", "")

            apps.add(name)

        except (psutil.NoSuchProcess, psutil.AccessDenied):

            pass

    return apps

 

 

# ============================================================

# SCREEN TIME TRACKER CLASS

# ============================================================

 

class ScreenTimeTracker:

    def __init__(self):

        self.data          = load_data()

        self.tracking      = {}       # app -> start_time (if currently active)

        self.watch_list    = set()    # apps to specifically watch

        self.running       = False

        self.today         = str(date.today())

        self.limits        = {}       # app -> minutes limit

        self.alerted       = set()    # apps already alerted today

 

        # Ensure today's entry exists

        if self.today not in self.data:

            self.data[self.today] = {}

 

    # ----------------------------------------------------------

    # Add / Remove from Watch List

    # ----------------------------------------------------------

 

    def add_to_watchlist(self, app_name):

        name = app_name.lower().replace(".exe", "")

        self.watch_list.add(name)

        print(f"\n   Added to watchlist: {name}")

 

    def remove_from_watchlist(self, app_name):

        name = app_name.lower().replace(".exe", "")

        self.watch_list.discard(name)

        print(f"\n   Removed from watchlist: {name}")

 

    def set_limit(self, app_name, minutes):

        name = app_name.lower().replace(".exe", "")

        self.limits[name] = minutes

        print(f"\n   Limit set: {name} → {minutes} min/day")

 

    # ----------------------------------------------------------

    # Core Tracking Loop

    # ----------------------------------------------------------

 

    def track(self):

        print("\n   Tracker running in background...")

        self.running = True

 

        while self.running:

            now        = datetime.now()

            today_str  = str(date.today())

 

            # New day reset

            if today_str != self.today:

                self.today   = today_str

                self.alerted = set()

                if self.today not in self.data:

                    self.data[self.today] = {}

 

            # Get active apps

            if self.watch_list:

                active_now = get_all_processes() & self.watch_list

            else:

                active_now = get_running_apps()

 

            # Apps that just started

            for app in active_now:

                if app not in self.tracking:

                    self.tracking[app] = now

 

            # Apps that just stopped

            stopped = set(self.tracking.keys()) - active_now

            for app in stopped:

                elapsed = (now - self.tracking[app]).total_seconds()

                self._add_time(app, elapsed)

                del self.tracking[app]

 

            # Check limits

            for app in active_now:

                total_mins = self._get_today_minutes(app)

                if app in self.limits and total_mins >= self.limits[app]:

                    if app not in self.alerted:

                        print(f"\n  ⚠  LIMIT REACHED: [{app}] — {total_mins:.1f} min used (limit: {self.limits[app]} min)")

                        print("  > ", end="", flush=True)

                        self.alerted.add(app)

 

            save_data(self.data)

            time.sleep(POLL_INTERVAL)

 

        # Final flush — save any still-running apps

        now = datetime.now()

        for app, start in self.tracking.items():

            elapsed = (now - start).total_seconds()

            self._add_time(app, elapsed)

        save_data(self.data)

 

    def stop(self):

        self.running = False

 

    # ----------------------------------------------------------

    # Time Helpers

    # ----------------------------------------------------------

 

    def _add_time(self, app, seconds):

        today = self.today

        if today not in self.data:

            self.data[today] = {}

        self.data[today][app] = self.data[today].get(app, 0) + seconds

 

    def _get_today_minutes(self, app):

        return self.data.get(self.today, {}).get(app, 0) / 60

 

    # ----------------------------------------------------------

    # Reports

    # ----------------------------------------------------------

 

    def report_today(self):

        today_data = self.data.get(self.today, {})

 

        # Add currently running session time (not yet saved)

        now = datetime.now()

        live = {}

        for app, start in self.tracking.items():

            live[app] = (now - start).total_seconds()

 

        merged = dict(today_data)

        for app, secs in live.items():

            merged[app] = merged.get(app, 0) + secs

 

        if not merged:

            print("\n   No screen time data for today yet.")

            return

 

        sorted_apps = sorted(merged.items(), key=lambda x: x[1], reverse=True)

 

        print("\n" + "="*55)

        print(f"   SCREEN TIME REPORT — {self.today}")

        print("="*55)

        print(f"\n  {'APP':<25} {'TIME':>12}  {'LIMIT':>10}")

        print("  " + "-"*50)

 

        total_secs = 0

        for app, secs in sorted_apps:

            h, m, s   = _hms(secs)

            time_str  = f"{h}h {m:02d}m {s:02d}s" if h else f"{m}m {s:02d}s"

            limit_str = f"{self.limits[app]} min" if app in self.limits else "—"

            bar       = _bar(secs, max(v for _, v in sorted_apps))

            print(f"  {app:<25} {time_str:>12}  {limit_str:>10}  {bar}")

            total_secs += secs

 

        print("  " + "-"*50)

        h, m, s = _hms(total_secs)

        print(f"  {'TOTAL':<25} {h}h {m:02d}m {s:02d}s")

        print("="*55)

 

    def report_week(self):

        print("\n" + "="*55)

        print("   WEEKLY SCREEN TIME SUMMARY")

        print("="*55)

 

        app_totals = defaultdict(float)

        day_totals = {}

 

        for day_str, apps in sorted(self.data.items())[-7:]:

            day_total = sum(apps.values())

            day_totals[day_str] = day_total

            for app, secs in apps.items():

                app_totals[app] += secs

 

        print("\n  Daily totals:")

        for day, secs in day_totals.items():

            h, m, _ = _hms(secs)

            bar      = _bar(secs, max(day_totals.values()) if day_totals else 1)

            print(f"  {day}   {h}h {m:02d}m  {bar}")

 

        print("\n  Top apps this week:")

        top = sorted(app_totals.items(), key=lambda x: x[1], reverse=True)[:10]

        for app, secs in top:

            h, m, _ = _hms(secs)

            print(f"  • {app:<25} {h}h {m:02d}m")

        print("="*55)

 

    def list_watchlist(self):

        print("\n  👁  Current watchlist:")

        if self.watch_list:

            for app in sorted(self.watch_list):

                limit = f"  (limit: {self.limits[app]} min)" if app in self.limits else ""

                print(f"  • {app}{limit}")

        else:

            print("  (empty — tracking ALL running processes)")

 

    def show_running_now(self):

        apps = get_all_processes()

        print(f"\n  🖥  Currently running processes ({len(apps)}):")

        for i, app in enumerate(sorted(apps), 1):

            active = "" if app in self.tracking else "  "

            print(f"  {active} {app}")

            if i % 30 == 0:

                cont = input("  ... show more? (y/n): ").strip().lower()

                if cont != "y":

                    break

 

 

# ============================================================

# HELPERS

# ============================================================

 

def _hms(seconds):

    seconds = int(seconds)

    h = seconds // 3600

    m = (seconds % 3600) // 60

    s = seconds % 60

    return h, m, s

 

 

def _bar(value, max_value, width=10):

    if max_value == 0:

        return ""

    filled = int((value / max_value) * width)

    return "█" * filled + "░" * (width - filled)

 

 

# ============================================================

# MAIN MENU

# ============================================================

 

def print_menu():

    print("\n" + "-"*45)

    print("       SCREEN TIME TRACKER")

    print("-"*45)

    print("  1. Start tracking (background)")

    print("  2. Today's report")

    print("  3. Weekly summary")

    print("  4. Manage watchlist")

    print("  5. Set app time limit")

    print("  6. Show all running processes")

    print("  0. Stop & Exit")

    print("-"*45)

 

 

def manage_watchlist(tracker):

    print("\n  Watchlist options:")

    print("  1. Add app to watchlist")

    print("  2. Remove app from watchlist")

    print("  3. View current watchlist")

    sub = input("  Choice: ").strip()

    if sub == "1":

        app = input("  App name (e.g. chrome, code, vlc): ").strip()

        tracker.add_to_watchlist(app)

    elif sub == "2":

        app = input("  App name to remove: ").strip()

        tracker.remove_from_watchlist(app)

    elif sub == "3":

        tracker.list_watchlist()

 

 

def main():

    print("\n" + "="*55)

    print("       SCREEN TIME TRACKER")

    print("="*55)

    print(f"\n  Data file    : {DATA_FILE}")

    print(f"  Poll interval: every {POLL_INTERVAL} seconds")

    print("\n   Tip: Add specific apps to watchlist for focused tracking.")

    print("         Or track ALL processes without a watchlist.")

 

    tracker = ScreenTimeTracker()

    tracker_thread = None

 

    while True:

        print_menu()

        choice = input("  > ").strip()

 

        if choice == "1":

            if tracker_thread and tracker_thread.is_alive():

                print("\n   Tracker is already running.")

            else:

                tracker_thread = threading.Thread(target=tracker.track, daemon=True)

                tracker_thread.start()

 

        elif choice == "2":

            tracker.report_today()

 

        elif choice == "3":

            tracker.report_week()

 

        elif choice == "4":

            manage_watchlist(tracker)

 

        elif choice == "5":

            app  = input("\n  App name: ").strip()

            mins = int(input("  Daily limit (minutes): ").strip())

            tracker.set_limit(app, mins)

 

        elif choice == "6":

            tracker.show_running_now()

 

        elif choice == "0":

            print("\n   Stopping tracker...")

            tracker.stop()

            if tracker_thread:

                tracker_thread.join(timeout=5)

            print("   Data saved.")

            print("   Goodbye!\n")

            break

 

        else:

            print("   Invalid choice.")

 

 

# ============================================================

# RUN

# ============================================================

 

if __name__ == "__main__":

    main()

Auto Email Scheduler

 import smtplib

import schedule

import json

import os

import time

import threading

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email import encoders

from datetime import datetime, timedelta

from pathlib import Path

 

# ============================================================

# CONFIGURATION FILE

# ============================================================

 

CONFIG_FILE   = "email_config.json"

SCHEDULE_FILE = "email_schedule.json"

 

# ============================================================

# SAVE & LOAD CONFIG (SMTP credentials)

# ============================================================

 

def save_config(config):

    with open(CONFIG_FILE, "w") as f:

        json.dump(config, f, indent=2)

    print("   Configuration saved.")

 

 

def load_config():

    if Path(CONFIG_FILE).exists():

        with open(CONFIG_FILE, "r") as f:

            return json.load(f)

    return None

 

 

# ============================================================

# SAVE & LOAD SCHEDULED EMAILS

# ============================================================

 

def save_schedule(emails):

    with open(SCHEDULE_FILE, "w") as f:

        json.dump(emails, f, indent=2)

 

 

def load_schedule():

    if Path(SCHEDULE_FILE).exists():

        try:

            with open(SCHEDULE_FILE, "r") as f:

                return json.load(f)

        except:

            return []

    return []

 

 

# ============================================================

# SETUP SMTP CREDENTIALS

# ============================================================

 

def setup_credentials():

    print("\n" + "="*55)

    print("   SMTP CREDENTIALS SETUP")

    print("="*55)

    print("\n  Supported providers:")

    print("  1. Gmail       (smtp.gmail.com : 587)")

    print("  2. Outlook     (smtp.office365.com : 587)")

    print("  3. Yahoo       (smtp.mail.yahoo.com : 587)")

    print("  4. Custom SMTP")

 

    choice = input("\n  Choose provider (1-4): ").strip()

 

    providers = {

        "1": ("smtp.gmail.com",        587),

        "2": ("smtp.office365.com",    587),

        "3": ("smtp.mail.yahoo.com",   587),

    }

 

    if choice in providers:

        smtp_host, smtp_port = providers[choice]

    else:

        smtp_host = input("  SMTP Host: ").strip()

        smtp_port = int(input("  SMTP Port: ").strip())

 

    email    = input("\n  Your email address: ").strip()

    password = input("  App password (NOT your login password): ").strip()

 

    config = {

        "smtp_host": smtp_host,

        "smtp_port": smtp_port,

        "email":     email,

        "password":  password

    }

 

    # Test connection

    print("\n   Testing connection...")

    try:

        server = smtplib.SMTP(smtp_host, smtp_port)

        server.starttls()

        server.login(email, password)

        server.quit()

        print("   Connection successful!")

        save_config(config)

    except Exception as e:

        print(f"   Connection failed: {e}")

        print("   Tip: Use an App Password, not your account password.")

        print("     Gmail: myaccount.google.com → Security → App Passwords")

 

    return config

 

 

# ============================================================

# SEND EMAIL

# ============================================================

 

def send_email(config, to, subject, body, attachment_path=None, is_html=False):

    try:

        msg = MIMEMultipart()

        msg["From"]    = config["email"]

        msg["To"]      = to

        msg["Subject"] = subject

 

        # Body

        mime_type = "html" if is_html else "plain"

        msg.attach(MIMEText(body, mime_type))

 

        # Attachment

        if attachment_path and Path(attachment_path).exists():

            with open(attachment_path, "rb") as f:

                part = MIMEBase("application", "octet-stream")

                part.set_payload(f.read())

            encoders.encode_base64(part)

            part.add_header(

                "Content-Disposition",

                f"attachment; filename={Path(attachment_path).name}"

            )

            msg.attach(part)

 

        server = smtplib.SMTP(config["smtp_host"], config["smtp_port"])

        server.starttls()

        server.login(config["email"], config["password"])

        server.sendmail(config["email"], to, msg.as_string())

        server.quit()

 

        print(f"\n   Email sent to {to} at {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}")

        return True

 

    except Exception as e:

        print(f"\n   Failed to send email to {to}: {e}")

        return False

 

 

# ============================================================

# COMPOSE EMAIL

# ============================================================

 

def compose_email():

    print("\n" + "="*55)

    print("    COMPOSE EMAIL")

    print("="*55)

 

    to         = input("\n  To (email address): ").strip()

    subject    = input("  Subject: ").strip()

 

    print("  Body (type END on a new line to finish):")

    lines = []

    while True:

        line = input()

        if line.strip().upper() == "END":

            break

        lines.append(line)

    body = "\n".join(lines)

 

    attachment = input("\n  Attachment path (or press Enter to skip): ").strip()

    attachment = attachment if attachment and Path(attachment).exists() else None

 

    if attachment is None and input("  Was that a path? File not found - skip attachment? (y/n): ").strip().lower() != "n":

        attachment = None

 

    return {

        "to":         to,

        "subject":    subject,

        "body":       body,

        "attachment": attachment,

        "is_html":    False

    }

 

 

# ============================================================

# SCHEDULE EMAIL

# ============================================================

 

def schedule_email(email_data):

    print("\n" + "="*55)

    print("   SCHEDULE EMAIL")

    print("="*55)

    print("\n  Schedule type:")

    print("  1. One-time  (specific date & time)")

    print("  2. Daily     (every day at a time)")

    print("  3. Weekly    (specific day each week)")

    print("  4. Send NOW")

 

    stype = input("\n  Choice (1-4): ").strip()

 

    if stype == "1":

        dt_str = input("  Send at (DD-MM-YYYY HH:MM): ").strip()

        try:

            send_at = datetime.strptime(dt_str, "%d-%m-%Y %H:%M")

            email_data["schedule_type"] = "once"

            email_data["send_at"]       = send_at.strftime("%d-%m-%Y %H:%M")

            email_data["sent"]          = False

        except ValueError:

            print("   Invalid date format.")

            return None

 

    elif stype == "2":

        time_str = input("  Daily at (HH:MM): ").strip()

        email_data["schedule_type"] = "daily"

        email_data["send_at"]       = time_str

        email_data["sent"]          = False

 

    elif stype == "3":

        days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]

        print("  Days:", ", ".join(d.capitalize() for d in days))

        day      = input("  Day: ").strip().lower()

        time_str = input("  Time (HH:MM): ").strip()

        if day not in days:

            print("   Invalid day.")

            return None

        email_data["schedule_type"] = "weekly"

        email_data["send_day"]      = day

        email_data["send_at"]       = time_str

        email_data["sent"]          = False

 

    elif stype == "4":

        email_data["schedule_type"] = "now"

        email_data["sent"]          = False

 

    else:

        print("   Invalid choice.")

        return None

 

    email_data["id"]      = datetime.now().strftime("%Y%m%d%H%M%S")

    email_data["created"] = datetime.now().strftime("%d-%m-%Y %H:%M:%S")

    return email_data

 

 

# ============================================================

# VIEW SCHEDULED EMAILS

# ============================================================

 

def view_scheduled(emails):

    pending = [e for e in emails if not e.get("sent")]

    sent    = [e for e in emails if e.get("sent")]

 

    print("\n" + "="*55)

    print(f"   SCHEDULED EMAILS  ({len(pending)} pending, {len(sent)} sent)")

    print("="*55)

 

    if not emails:

        print("\n   No emails scheduled yet.")

        return

 

    if pending:

        print("\n   PENDING:")

        for i, e in enumerate(pending, 1):

            print(f"\n  [{i}] To: {e['to']}")

            print(f"      Subject: {e['subject']}")

            stype = e.get("schedule_type", "")

            if stype == "once":

                print(f"      Send at: {e['send_at']} (one-time)")

            elif stype == "daily":

                print(f"      Daily at: {e['send_at']}")

            elif stype == "weekly":

                print(f"      Weekly: {e['send_day'].capitalize()} at {e['send_at']}")

            print(f"      Created: {e.get('created','')}")

 

    if sent:

        print("\n   SENT:")

        for e in sent[-5:]:  # show last 5 sent

            print(f"  • [{e['to']}] \"{e['subject']}\" — sent {e.get('sent_at','')}")

 

 

# ============================================================

# DELETE SCHEDULED EMAIL

# ============================================================

 

def delete_scheduled(emails):

    pending = [e for e in emails if not e.get("sent")]

    if not pending:

        print("\n  📭 No pending emails to delete.")

        return emails

 

    view_scheduled(emails)

    try:

        idx = int(input("\n  Enter number to delete: ").strip()) - 1

        if 0 <= idx < len(pending):

            removed = pending[idx]

            emails  = [e for e in emails if e["id"] != removed["id"]]

            save_schedule(emails)

            print(f"  🗑  Deleted email to {removed['to']}")

        else:

            print("   Invalid number.")

    except ValueError:

        print("   Invalid input.")

    return emails

 

 

# ============================================================

# SCHEDULER RUNNER (Background Thread)

# ============================================================

 

def run_scheduler(config, emails_ref):

    print("   Scheduler running in background...")

 

    while True:

        now = datetime.now()

        now_time = now.strftime("%H:%M")

        now_day  = now.strftime("%A").lower()

 

        for email in emails_ref:

            if email.get("sent"):

                continue

 

            stype = email.get("schedule_type")

 

            should_send = False

 

            if stype == "now":

                should_send = True

 

            elif stype == "once":

                try:

                    send_dt = datetime.strptime(email["send_at"], "%d-%m-%Y %H:%M")

                    if now >= send_dt and not email.get("sent"):

                        should_send = True

                except:

                    pass

 

            elif stype == "daily":

                if now_time == email.get("send_at"):

                    should_send = True

 

            elif stype == "weekly":

                if now_day == email.get("send_day") and now_time == email.get("send_at"):

                    should_send = True

 

            if should_send:

                success = send_email(

                    config,

                    email["to"],

                    email["subject"],

                    email["body"],

                    email.get("attachment"),

                    email.get("is_html", False)

                )

                if success:

                    email["sent"]    = True

                    email["sent_at"] = now.strftime("%d-%m-%Y %H:%M:%S")

                    save_schedule(emails_ref)

 

        time.sleep(30)  # Check every 30 seconds

 

 

# ============================================================

# MAIN MENU

# ============================================================

 

def print_menu():

    print("\n" + "-"*45)

    print("   AUTO EMAIL SCHEDULER")

    print("-"*45)

    print("  1. Setup / Update SMTP credentials")

    print("  2. Compose & Schedule new email")

    print("  3. View scheduled emails")

    print("  4. Delete a scheduled email")

    print("  5. Start scheduler (monitor & send)")

    print("  0. Exit")

    print("-"*45)

 

 

def main():

    print("\n" + "="*55)

    print("      AUTO EMAIL SCHEDULER")

    print("="*55)

 

    config = load_config()

    emails = load_schedule()

 

    if config:

        print(f"\n   Loaded credentials for: {config['email']}")

    else:

        print("\n  ⚠  No credentials found. Please set up SMTP first (Option 1).")

 

    scheduler_thread = None

 

    while True:

        print_menu()

        choice = input("  > ").strip()

 

        if choice == "1":

            config = setup_credentials()

 

        elif choice == "2":

            if not config:

                print("\n   Please set up SMTP credentials first (Option 1).")

                continue

            email_data = compose_email()

            if email_data:

                scheduled = schedule_email(email_data)

                if scheduled:

                    emails.append(scheduled)

                    save_schedule(emails)

                    print(f"\n   Email scheduled successfully! (ID: {scheduled['id']})")

 

        elif choice == "3":

            view_scheduled(emails)

 

        elif choice == "4":

            emails = delete_scheduled(emails)

 

        elif choice == "5":

            if not config:

                print("\n   Please set up SMTP credentials first.")

                continue

            if scheduler_thread and scheduler_thread.is_alive():

                print("\n   Scheduler is already running.")

            else:

                scheduler_thread = threading.Thread(

                    target=run_scheduler,

                    args=(config, emails),

                    daemon=True

                )

                scheduler_thread.start()

                print("\n   Scheduler started! It checks every 30 seconds.")

                print("  Keep this window open. Press 0 to exit.\n")

 

        elif choice == "0":

            print("\n   Goodbye! Scheduled emails will not send after exit.\n")

            break

 

        else:

            print("   Invalid choice.")

 

 

# ============================================================

# RUN

# ============================================================

 

if __name__ == "__main__":

    main()

Clipboard History Manager

import pyperclip

import keyboard

import json

import os

import time

import threading

from datetime import datetime

from pathlib import Path

 

# ============================================================

# CONFIGURATION

# ============================================================

 

HISTORY_FILE  = "clipboard_history.json"

MAX_ENTRIES   = 50        # Maximum clipboard entries to store

POLL_INTERVAL = 1.0       # Seconds between clipboard checks

PREVIEW_LEN   = 60        # Characters to show in preview

 

# ============================================================

# LOAD & SAVE HISTORY

# ============================================================

 

def load_history():

    if Path(HISTORY_FILE).exists():

        try:

            with open(HISTORY_FILE, "r", encoding="utf-8") as f:

                return json.load(f)

        except:

            return []

    return []

 

 

def save_history(history):

    with open(HISTORY_FILE, "w", encoding="utf-8") as f:

        json.dump(history, f, indent=2, ensure_ascii=False)

 

 

# ============================================================

# ADD ENTRY

# ============================================================

 

def add_entry(history, text):

    # Avoid duplicate consecutive entries

    if history and history[-1]["text"] == text:

        return history

 

    # Avoid duplicates anywhere in history — move to top instead

    history = [h for h in history if h["text"] != text]

 

    entry = {

        "text":      text,

        "timestamp": datetime.now().strftime("%d-%m-%Y %H:%M:%S"),

        "length":    len(text)

    }

 

    history.append(entry)

 

    # Keep only last MAX_ENTRIES

    if len(history) > MAX_ENTRIES:

        history = history[-MAX_ENTRIES:]

 

    save_history(history)

    return history

 

 

# ============================================================

# MONITOR CLIPBOARD (Background Thread)

# ============================================================

 

class ClipboardMonitor(threading.Thread):

    def __init__(self):

        super().__init__(daemon=True)

        self.history     = load_history()

        self.last_text   = ""

        self.running     = True

        self.entry_count = len(self.history)

 

    def run(self):

        print("   Clipboard monitor started (background)...")

        while self.running:

            try:

                current = pyperclip.paste()

                if current and current != self.last_text:

                    self.history  = add_entry(self.history, current)

                    self.last_text = current

                    new_count = len(self.history)

                    if new_count != self.entry_count:

                        preview = current[:PREVIEW_LEN].replace("\n", " ")

                        print(f"\n   Captured: \"{preview}{'...' if len(current) > PREVIEW_LEN else ''}\"")

                        print("  > ", end="", flush=True)

                        self.entry_count = new_count

            except:

                pass

            time.sleep(POLL_INTERVAL)

 

    def stop(self):

        self.running = False

 

 

# ============================================================

# DISPLAY HISTORY

# ============================================================

 

def display_history(history):

    if not history:

        print("\n   No clipboard history yet.")

        return

 

    print("\n" + "="*60)

    print(f"   CLIPBOARD HISTORY  ({len(history)} entries)")

    print("="*60)

 

    for i, entry in enumerate(reversed(history), 1):

        preview = entry["text"][:PREVIEW_LEN].replace("\n", " ")

        dots    = "..." if len(entry["text"]) > PREVIEW_LEN else ""

        print(f"\n  [{i:02d}]  {entry['timestamp']}  |  {entry['length']} chars")

        print(f"       \"{preview}{dots}\"")

 

    print("\n" + "="*60)

 

 

# ============================================================

# SEARCH HISTORY

# ============================================================

 

def search_history(history, keyword):

    keyword_lower = keyword.lower()

    results = [

        (i, h) for i, h in enumerate(history)

        if keyword_lower in h["text"].lower()

    ]

 

    if not results:

        print(f"\n   No results found for: \"{keyword}\"")

        return

 

    print(f"\n   Found {len(results)} result(s) for \"{keyword}\":\n")

    print("-"*60)

    for rank, (idx, entry) in enumerate(results, 1):

        preview = entry["text"][:PREVIEW_LEN].replace("\n", " ")

        dots    = "..." if len(entry["text"]) > PREVIEW_LEN else ""

        print(f"  [{rank}] 🕒 {entry['timestamp']}  (index {idx+1})")

        print(f"       \"{preview}{dots}\"")

        print()

 

 

# ============================================================

# RESTORE TO CLIPBOARD

# ============================================================

 

def restore_entry(history, index):

    """Copy a history entry back to clipboard by display index (1-based, newest first)."""

    if index < 1 or index > len(history):

        print("   Invalid index.")

        return

 

    # Display is reversed so index 1 = last item

    real_index = len(history) - index

    entry = history[real_index]

 

    pyperclip.copy(entry["text"])

    preview = entry["text"][:PREVIEW_LEN].replace("\n", " ")

    print(f"\n    Restored to clipboard: \"{preview}{'...' if len(entry['text']) > PREVIEW_LEN else ''}\"")

 

 

# ============================================================

# DELETE ENTRY

# ============================================================

 

def delete_entry(history, index):

    if index < 1 or index > len(history):

        print("   Invalid index.")

        return history

 

    real_index = len(history) - index

    removed    = history.pop(real_index)

    save_history(history)

 

    preview = removed["text"][:PREVIEW_LEN].replace("\n", " ")

    print(f"\n    Deleted: \"{preview}{'...' if len(removed['text']) > PREVIEW_LEN else ''}\"")

    return history

 

 

# ============================================================

# CLEAR ALL HISTORY

# ============================================================

 

def clear_history():

    confirm = input("\n  ⚠  Clear ALL clipboard history? (yes/no): ").strip().lower()

    if confirm == "yes":

        save_history([])

        print("    Clipboard history cleared.")

        return []

    print("  Cancelled.")

    return load_history()

 

 

# ============================================================

# EXPORT HISTORY TO TXT

# ============================================================

 

def export_history(history):

    if not history:

        print("   Nothing to export.")

        return

 

    filename = f"clipboard_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"

    with open(filename, "w", encoding="utf-8") as f:

        f.write("CLIPBOARD HISTORY EXPORT\n")

        f.write(f"Exported: {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\n")

        f.write(f"Total entries: {len(history)}\n")

        f.write("="*60 + "\n\n")

 

        for i, entry in enumerate(reversed(history), 1):

            f.write(f"[{i:02d}] {entry['timestamp']}  ({entry['length']} chars)\n")

            f.write(entry["text"] + "\n")

            f.write("-"*60 + "\n\n")

 

    print(f"\n   Exported to: {filename}")

 

 

# ============================================================

# MAIN MENU

# ============================================================

 

def print_menu():

    print("\n" + "-"*40)

    print("  CLIPBOARD HISTORY MANAGER")

    print("-"*40)

    print("  1. View history")

    print("  2. Search history")

    print("  3. Restore entry to clipboard")

    print("  4. Delete an entry")

    print("  5. Export history to .txt")

    print("  6. Clear all history")

    print("  0. Exit")

    print("-"*40)

 

 

def main():

    print("\n" + "="*55)

    print("      CLIPBOARD HISTORY MANAGER")

    print("="*55)

    print(f"\n  Max entries : {MAX_ENTRIES}")

    print(f"  History file: {HISTORY_FILE}")

    print(f"  Poll interval: every {POLL_INTERVAL}s")

 

    # Start background monitor

    monitor = ClipboardMonitor()

    monitor.start()

 

    time.sleep(0.5)

 

    while True:

        print_menu()

        choice = input("  > ").strip()

 

        if choice == "1":

            display_history(monitor.history)

 

        elif choice == "2":

            keyword = input("\n  Search keyword: ").strip()

            if keyword:

                search_history(monitor.history, keyword)

 

        elif choice == "3":

            display_history(monitor.history)

            try:

                idx = int(input("\n  Enter entry number to restore: ").strip())

                restore_entry(monitor.history, idx)

            except ValueError:

                print("   Invalid number.")

 

        elif choice == "4":

            display_history(monitor.history)

            try:

                idx = int(input("\n  Enter entry number to delete: ").strip())

                monitor.history = delete_entry(monitor.history, idx)

            except ValueError:

                print("   Invalid number.")

 

        elif choice == "5":

            export_history(monitor.history)

 

        elif choice == "6":

            monitor.history = clear_history()

 

        elif choice == "0":

            monitor.stop()

            print("\n   Clipboard History Manager closed. Goodbye!\n")

            break

 

        else:

            print("   Invalid choice. Try again.")

 

 

# ============================================================

# RUN

# ============================================================

 

if __name__ == "__main__":

    main()

PDF Watermark Adder

 import os

from pathlib import Path

from pypdf import PdfReader, PdfWriter

from reportlab.pdfgen import canvas

from reportlab.lib.pagesizes import letter, A4

from reportlab.lib.colors import Color

import io

 

# ============================================================

# STEP 1: Create Text Watermark as PDF (in memory)

# ============================================================

 

def create_text_watermark(text, page_width, page_height,

                           font_size=50, opacity=0.15,

                           rotation=45, color=(0.5, 0.5, 0.5)):

    """

    Generate a transparent diagonal text watermark

    and return it as a PDF bytes object.

    """

    packet = io.BytesIO()

    c = canvas.Canvas(packet, pagesize=(page_width, page_height))

 

    # Set transparency

    c.setFillColor(Color(color[0], color[1], color[2], alpha=opacity))

    c.setFont("Helvetica-Bold", font_size)

 

    # Move to center of page and rotate

    c.translate(page_width / 2, page_height / 2)

    c.rotate(rotation)

 

    # Draw watermark text centered

    c.drawCentredString(0, 0, text)

 

    c.save()

    packet.seek(0)

    return packet

 

 

# ============================================================

# STEP 2: Create Image Watermark as PDF (in memory)

# ============================================================

 

def create_image_watermark(image_path, page_width, page_height,

                            opacity=0.2, scale=0.4):

    """

    Place an image watermark centered on the page.

    """

    packet = io.BytesIO()

    c = canvas.Canvas(packet, pagesize=(page_width, page_height))

 

    img_width  = page_width * scale

    img_height = page_height * scale

    x = (page_width - img_width) / 2

    y = (page_height - img_height) / 2

 

    c.setFillAlpha(opacity)

    c.drawImage(image_path, x, y, width=img_width, height=img_height,

                mask='auto', preserveAspectRatio=True)

 

    c.save()

    packet.seek(0)

    return packet

 

 

# ============================================================

# STEP 3: Apply Watermark to Each Page

# ============================================================

 

def apply_watermark(input_pdf, output_pdf, watermark_packet_func,

                    page_range=None):

    """

    Merge watermark onto each page of the input PDF.

    page_range: tuple (start, end) 1-indexed inclusive, or None for all pages

    """

    reader = PdfReader(input_pdf)

    writer = PdfWriter()

 

    total_pages = len(reader.pages)

    print(f"\n Total pages in PDF: {total_pages}")

 

    # Determine page range

    if page_range:

        start = max(0, page_range[0] - 1)

        end   = min(total_pages, page_range[1])

    else:

        start = 0

        end   = total_pages

 

    for i, page in enumerate(reader.pages):

        if start <= i < end:

            # Get actual page dimensions

            media_box  = page.mediabox

            page_w = float(media_box.width)

            page_h = float(media_box.height)

 

            # Create fresh watermark for this page size

            wm_packet = watermark_packet_func(page_w, page_h)

            wm_reader = PdfReader(wm_packet)

            watermark_page = wm_reader.pages[0]

 

            # Merge watermark onto page

            page.merge_page(watermark_page)

 

        writer.add_page(page)

 

    with open(output_pdf, "wb") as f:

        writer.write(f)

 

    print(f" Watermarked PDF saved: {output_pdf}")

 

 

# ============================================================

# STEP 4: Batch Watermark Multiple PDFs

# ============================================================

 

def batch_watermark(folder_path, watermark_func, suffix="_watermarked"):

    folder = Path(folder_path)

    pdf_files = list(folder.glob("*.pdf"))

 

    if not pdf_files:

        print(" No PDF files found in folder.")

        return

 

    print(f"\n Found {len(pdf_files)} PDF(s) — processing...")

 

    for pdf in pdf_files:

        output_name = pdf.stem + suffix + ".pdf"

        output_path = pdf.parent / output_name

        try:

            apply_watermark(str(pdf), str(output_path), watermark_func)

        except Exception as e:

            print(f"  ⚠ Error processing {pdf.name}: {e}")

 

 

# ============================================================

# HELPER: Get Page Range Input

# ============================================================

 

def get_page_range(total_hint="all"):

    choice = input(f"\nApply watermark to all pages or specific range? (all/range) [{total_hint}]: ").strip().lower()

    if choice == "range":

        start = int(input("  Start page (1-indexed): ").strip())

        end   = int(input("  End page: ").strip())

        return (start, end)

    return None

 

 

# ============================================================

# MAIN MENU

# ============================================================

 

def main():

    print("\n" + "="*55)

    print("  PDF WATERMARK ADDER")

    print("="*55)

 

    print("\nMode:")

    print("  1. Add TEXT watermark to single PDF")

    print("  2. Add IMAGE watermark to single PDF")

    print("  3. Batch TEXT watermark (entire folder)")

 

    mode = input("\nEnter choice (1/2/3): ").strip()

 

    # ---- Single PDF Modes ----

    if mode in ["1", "2"]:

        input_pdf = input("\nEnter input PDF path: ").strip()

        if not os.path.isfile(input_pdf):

            print(" Invalid file path!")

            return

 

        default_out = Path(input_pdf).stem + "_watermarked.pdf"

        output_pdf  = input(f"Output PDF name [{default_out}]: ").strip() or default_out

 

        page_range = get_page_range()

 

        if mode == "1":

            # --- Text Watermark Settings ---

            text      = input("\nWatermark text (e.g. CONFIDENTIAL): ").strip() or "CONFIDENTIAL"

            font_size = int(input("Font size (default 50): ").strip() or 50)

            opacity   = float(input("Opacity 0.0-1.0 (default 0.15): ").strip() or 0.15)

            rotation  = float(input("Rotation angle (default 45): ").strip() or 45)

 

            print("\nColor options: 1=Gray  2=Red  3=Blue  4=Black")

            color_choice = input("Choose color (default 1): ").strip() or "1"

            colors = {

                "1": (0.5, 0.5, 0.5),

                "2": (0.8, 0.1, 0.1),

                "3": (0.1, 0.1, 0.8),

                "4": (0.0, 0.0, 0.0)

            }

            color = colors.get(color_choice, (0.5, 0.5, 0.5))

 

            def watermark_func(w, h):

                return create_text_watermark(

                    text, w, h,

                    font_size=font_size,

                    opacity=opacity,

                    rotation=rotation,

                    color=color

                )

 

        else:

            # --- Image Watermark Settings ---

            image_path = input("\nEnter image path (.png/.jpg): ").strip()

            if not os.path.isfile(image_path):

                print(" Invalid image path!")

                return

 

            opacity = float(input("Opacity 0.0-1.0 (default 0.2): ").strip() or 0.2)

            scale   = float(input("Image scale 0.1-1.0 (default 0.4): ").strip() or 0.4)

 

            def watermark_func(w, h):

                return create_image_watermark(

                    image_path, w, h,

                    opacity=opacity,

                    scale=scale

                )

 

        apply_watermark(input_pdf, output_pdf, watermark_func, page_range)

 

    # ---- Batch Mode ----

    elif mode == "3":

        folder = input("\nEnter folder path containing PDFs: ").strip()

        if not os.path.isdir(folder):

            print(" Invalid folder!")

            return

 

        text      = input("Watermark text (e.g. DRAFT): ").strip() or "DRAFT"

        font_size = int(input("Font size (default 60): ").strip() or 60)

        opacity   = float(input("Opacity 0.0-1.0 (default 0.15): ").strip() or 0.15)

        rotation  = float(input("Rotation angle (default 45): ").strip() or 45)

 

        def watermark_func(w, h):

            return create_text_watermark(text, w, h,

                                         font_size=font_size,

                                         opacity=opacity,

                                         rotation=rotation)

 

        batch_watermark(folder, watermark_func)

 

    else:

        print(" Invalid choice.")

        return

 

    print("\n" + "="*55)

    print("  All done! Your watermarked PDF(s) are ready.")

    print("="*55 + "\n")

 

 

# ============================================================

# RUN

# ============================================================

 

if __name__ == "__main__":

    main()

 

Bulk File Renamer

import os

import re

import shutil

from pathlib import Path

from datetime import datetime

 

# ============================================================

# UTILITY: List Files in Folder

# ============================================================

 

def list_files(folder, extension_filter=None):

    files = []

    for f in sorted(Path(folder).iterdir()):

        if f.is_file():

            if extension_filter:

                if f.suffix.lower() == extension_filter.lower():

                    files.append(f)

            else:

                files.append(f)

    return files

 

 

# ============================================================

# MODE 1: Add Prefix / Suffix

# ============================================================

 

def rename_prefix_suffix(files, prefix="", suffix=""):

    renamed = []

    for f in files:

        stem = f.stem        # filename without extension

        ext  = f.suffix      # .jpg, .txt etc.

        new_name = f"{prefix}{stem}{suffix}{ext}"

        new_path = f.parent / new_name

        renamed.append((f, new_path))

    return renamed

 

 

# ============================================================

# MODE 2: Sequential Numbering

# ============================================================

 

def rename_sequential(files, base_name="file", start=1, padding=3):

    renamed = []

    for i, f in enumerate(files, start=start):

        ext = f.suffix

        number = str(i).zfill(padding)

        new_name = f"{base_name}_{number}{ext}"

        new_path = f.parent / new_name

        renamed.append((f, new_path))

    return renamed

 

 

# ============================================================

# MODE 3: Find & Replace in Filename

# ============================================================

 

def rename_find_replace(files, find_text, replace_text):

    renamed = []

    for f in files:

        new_name = f.name.replace(find_text, replace_text)

        new_path = f.parent / new_name

        renamed.append((f, new_path))

    return renamed

 

 

# ============================================================

# MODE 4: Regex-Based Rename

# ============================================================

 

def rename_regex(files, pattern, replacement):

    renamed = []

    for f in files:

        try:

            new_name = re.sub(pattern, replacement, f.name)

            new_path = f.parent / new_name

            renamed.append((f, new_path))

        except re.error as e:

            print(f"  ⚠ Regex error for {f.name}: {e}")

    return renamed

 

 

# ============================================================

# MODE 5: Add Date Stamp

# ============================================================

 

def rename_add_date(files, position="prefix"):

    today = datetime.today().strftime("%Y%m%d")

    renamed = []

    for f in files:

        stem = f.stem

        ext  = f.suffix

        if position == "prefix":

            new_name = f"{today}_{stem}{ext}"

        else:

            new_name = f"{stem}_{today}{ext}"

        new_path = f.parent / new_name

        renamed.append((f, new_path))

    return renamed

 

 

# ============================================================

# PREVIEW & CONFIRM

# ============================================================

 

def preview_changes(renamed):

    print("\n" + "-"*60)

    print(f"{'ORIGINAL':<30} {'NEW NAME':<30}")

    print("-"*60)

    for old, new in renamed:

        print(f"{old.name:<30} {new.name:<30}")

    print("-"*60)

 

 

def apply_rename(renamed, dry_run=False):

    success = 0

    skipped = 0

    for old, new in renamed:

        if old == new:

            skipped += 1

            continue

        if new.exists():

            print(f"  ⚠ Skipped (already exists): {new.name}")

            skipped += 1

            continue

        if not dry_run:

            old.rename(new)

        success += 1

 

    if dry_run:

        print(f"\n Dry Run Complete: {success} files would be renamed, {skipped} skipped.")

    else:

        print(f"\n Done! {success} files renamed, {skipped} skipped.")

 

 

# ============================================================

# BACKUP (Optional)

# ============================================================

 

def backup_folder(folder):

    backup_path = str(folder) + "_backup_" + datetime.today().strftime("%Y%m%d_%H%M%S")

    shutil.copytree(folder, backup_path)

    print(f" Backup created: {backup_path}")

 

 

# ============================================================

# MAIN MENU

# ============================================================

 

def main():

    print("\n" + "="*55)

    print("          BULK FILE RENAMER")

    print("="*55)

 

    # --- Folder Path ---

    folder = input("\nEnter folder path: ").strip()

    if not os.path.isdir(folder):

        print(" Invalid folder path!")

        return

 

    # --- Optional Extension Filter ---

    ext_filter = input("Filter by extension? (e.g. .jpg) or press Enter for all: ").strip()

    if not ext_filter:

        ext_filter = None

 

    files = list_files(folder, ext_filter)

    if not files:

        print(" No files found!")

        return

 

    print(f"\n Found {len(files)} file(s) in '{folder}'")

 

    # --- Optional Backup ---

    backup = input("\nCreate backup before renaming? (y/n): ").strip().lower()

    if backup == "y":

        backup_folder(folder)

 

    # --- Choose Mode ---

    print("\nChoose Rename Mode:")

    print("  1. Add Prefix / Suffix")

    print("  2. Sequential Numbering")

    print("  3. Find & Replace")

    print("  4. Regex Pattern")

    print("  5. Add Date Stamp")

 

    choice = input("\nEnter choice (1-5): ").strip()

 

    renamed = []

 

    if choice == "1":

        prefix = input("Enter prefix (or leave blank): ").strip()

        suffix = input("Enter suffix (or leave blank): ").strip()

        renamed = rename_prefix_suffix(files, prefix, suffix)

 

    elif choice == "2":

        base = input("Enter base name (e.g. photo): ").strip()

        start = int(input("Start number (default 1): ").strip() or 1)

        padding = int(input("Number padding digits (default 3): ").strip() or 3)

        renamed = rename_sequential(files, base, start, padding)

 

    elif choice == "3":

        find = input("Find text: ").strip()

        replace = input("Replace with: ").strip()

        renamed = rename_find_replace(files, find, replace)

 

    elif choice == "4":

        print("  Example pattern: (\\d+)  → matches numbers")

        pattern = input("Regex pattern: ").strip()

        replacement = input("Replacement: ").strip()

        renamed = rename_regex(files, pattern, replacement)

 

    elif choice == "5":

        pos = input("Add date as prefix or suffix? (prefix/suffix): ").strip().lower()

        if pos not in ["prefix", "suffix"]:

            pos = "prefix"

        renamed = rename_add_date(files, pos)

 

    else:

        print(" Invalid choice.")

        return

 

    if not renamed:

        print(" No files to rename.")

        return

 

    # --- Preview ---

    preview_changes(renamed)

 

    # --- Dry Run or Apply ---

    mode = input("\nChoose action:\n  1. Apply rename\n  2. Dry run (preview only)\nChoice: ").strip()

 

    if mode == "1":

        apply_rename(renamed, dry_run=False)

    else:

        apply_rename(renamed, dry_run=True)

 

 

# ============================================================

# RUN

# ============================================================

 

if __name__ == "__main__":

    main()