import os import datetime import time import pyperclip from PIL import ImageGrab # Define output directories TEXT_DIR = r"c:\Temp\projectScreenCapture\TXT" IMG_DIR = r"c:\Temp\projectScreenCapture\JPG" LOG_DIR = r"c:\Temp\projectScreenCapture\LOG" LOG_FILE = os.path.join(LOG_DIR, "screenCaptureLogs.txt") # Ensure directories exist os.makedirs(TEXT_DIR, exist_ok=True) os.makedirs(IMG_DIR, exist_ok=True) os.makedirs(LOG_DIR, exist_ok=True) def log_event(capture_num, message): """Append a timestamped message with capture number to the log file.""" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(LOG_FILE, "a", encoding="utf-8") as log: log.write(f"[{timestamp}] Capture {capture_num}: {message}\n") def save_clipboard_text(capture_num): try: text = pyperclip.paste() if text and isinstance(text, str) and text.strip(): timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") file_path = os.path.join(TEXT_DIR, f"clipboard_{timestamp}.txt") with open(file_path, "w", encoding="utf-8") as f: f.write(text) print(f"Text saved to {file_path}") log_event(capture_num, f"Text saved to {file_path}") return True except Exception as e: print(f"Error saving text: {e}") log_event(capture_num, f"Error saving text: {e}") return False def save_clipboard_image(capture_num): try: img = ImageGrab.grabclipboard() if img is not None: timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") file_path = os.path.join(IMG_DIR, f"clipboard_{timestamp}.jpg") # Convert RGBA → RGB if needed if img.mode == "RGBA": img = img.convert("RGB") img.save(file_path, "JPEG") print(f"Image saved to {file_path}") log_event(capture_num, f"Image saved to {file_path}") return True except Exception as e: print(f"Error saving image: {e}") log_event(capture_num, f"Error saving image: {e}") return False if __name__ == "__main__": iterations = 60 * 60 // 15 # 240 iterations (15s × 240 = 60min) for i in range(iterations): capture_num = i + 1 print(f"--- Capture {capture_num}/{iterations} ---") text_saved = save_clipboard_text(capture_num) img_saved = save_clipboard_image(capture_num) if not text_saved and not img_saved: print("No text or image found in clipboard.") log_event(capture_num, "No text or image found in clipboard.") time.sleep(15)