on my macbook, whenever i copy text (cmd + c), i want a launchd agent to automatically…
- User
on my macbook, whenever i copy text (cmd + c), i want a launchd agent to automatically run and look at the text i copied, remove any hidden Unicode characters (like Narrow No-Break Space)/ U+202F, and then replace my copied text in my clipboard with that new clean text so when i go to paste it (cmd + v), there are no hidden unicode characters and the text is just as it was before (not more or less spaces) but just without the hidden characters
- Assistant
You want to automate clipboard cleanup on your Mac using a
launchdagent, which will monitor clipboard activity and remove hidden Unicode characters (e.g., U+202F Narrow No-Break Space) from copied text immediately after copying, so your paste is clean.macOS doesn’t natively allow watching the clipboard for changes directly via
launchd, but here’s how you can achieve this:
✅ Concept Overview
- A background script runs constantly and checks the clipboard every second or so.
- When it detects the clipboard has changed, it:
- Sanitizes the text (removes hidden Unicode characters like U+202F).
- Puts the cleaned text back into the clipboard.
🔧 Step-by-Step Setup
1. Create the Clipboard Cleaning Script
We’ll write a Python script for this. You could also do it in Swift or Bash, but Python is fast to implement and clear.
📄 clipboard_cleaner.py
#!/usr/bin/env python3 import time import subprocess import re # Path to store last seen clipboard content last_text = "" # These are the problematic characters you want to remove # Add more Unicode spaces if needed UNWANTED_CHARACTERS_REGEX = r'[\u202F\u200B\u200C\u200D\uFEFF]' def get_clipboard(): try: return subprocess.check_output(['pbpaste'], text=True) except subprocess.CalledProcessError: return "" def set_clipboard(text): subprocess.run('pbcopy', input=text, text=True) def main(): global last_text while True: current_clipboard = get_clipboard() if current_clipboard != last_text: # Clean text cleaned = re.sub(UNWANTED_CHARACTERS_REGEX, '', current_clipboard) if cleaned != current_clipboard: set_clipboard(cleaned) print("Cleaned and updated clipboard.") last_text = cleaned time.sleep(1) # Check every second if __name__ == "__main__": main()Hidden character list:
\u202F— Narrow No-Break Space\u200B— Zero-Width Space\u200C— Zero Width Non-Joiner\u200D— Zero Width Joiner\uFEFF— Zero Width No-Break Space (BOM)
You can add or remove based on what characters you want to strip.
2. Make It Executable
chmod +x ~/Scripts/clipboard_cleaner.py
3. Create a
launchdAgent👉
launchdwon't respond to CMD+C, but you can run a daemon that keeps cleaning the clipboard in the background. Perfect for your case!📄
~/Library/LaunchAgents/com.yourname.clipboardcleaner.plist<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.yourname.clipboardcleaner</string> <key>ProgramArguments</key> <array> <string>/usr/bin/python3</string> <string>/Users/YOURUSERNAME/Scripts/clipboard_cleaner.py</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> </dict> </plist>🔁 Replace
YOURUSERNAMEwith your actual user folder name.
4. Load the Agent
launchctl load ~/Library/LaunchAgents/com.yourname.clipboardcleaner.plistTo unload later (if needed):
launchctl unload ~/Library/LaunchAgents/com.yourname.clipboardcleaner.plist
✅ Test It
- Copy some text that includes U+202F or zero-width spaces.
- Wait a second.
- Paste — you should now get clean, unhidden-character-free text.
✔️ Works system-wide
✔️ Silent in background
✔️ Totally automatic
✅ Optional: Log Activity or Debug
If you want to track cleaning, add logging to a file from the script or use
launchd'sStandardOutPathandStandardErrorPath.
Would you like a version of this script that also notifies you when a character was removed (macOS notification)?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.