You can absolutely automate this on macOS using a LaunchAgent and a small script that monitors clipboard changes, cleans the text, and replaces it in the clipboard.
Here’s how to set it up:
✅ Goal
Whenever you press Cmd+C:
- A background script detects the new clipboard content.
- It removes hidden Unicode characters like Narrow No-Break Space (U+202F).
- The cleaned text is put back into the clipboard automatically.
- You can paste clean text with
Cmd+V.
🔧 Tools We’ll Use
launchd – To run a script in the background whenever your user session starts.
- A shell or Python script – That monitors clipboard changes and sanitizes the content.
pbcopy and pbpaste – Built-in macOS tools for clipboard manipulation.
- Optionally:
osascript – For finer control over clipboard monitoring.
📁 Step-by-Step Setup
1. Install Dependencies
We’ll use a simple Bash + osascript solution here, but if you want better Unicode handling, Python could be used instead.
Make sure you have AppleScript enabled and working (it is by default).
2. Create the Cleaning Script
Create a file named clean_clipboard.sh in a safe place like ~/scripts/clean_clipboard.sh:
mkdir -p ~/scripts
nano ~/scripts/clean_clipboard.sh
Paste this content:
#!/bin/bash
# This script watches for clipboard changes and cleans them
# Characters to remove: U+202F (Narrow No-Break Space) and other invisible chars
# Add more Unicode chars inside the pattern as needed
while true; do
sleep 0.5
current_clip=$(pbpaste 2>/dev/null | tr -cd '\11\12\15\40-\176' | perl -pe 's/\x{202F}//g')
if [[ $? -ne 0 ]]; then
continue
fi
echo "$current_clip" | pbcopy
done
💡 This script runs forever (while true) and every 0.5 seconds checks the clipboard. If it finds unwanted characters (like U+202F), it removes them and replaces the clipboard content.
Make it executable:
chmod +x ~/scripts/clean_clipboard.sh
3. Create a LaunchAgent
Create a .plist file in ~/Library/LaunchAgents/:
nano ~/Library/LaunchAgents/local.cleanclipboard.plist
Paste this:
<?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>local.cleanclipboard</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>/Users/$(whoami)/scripts/clean_clipboard.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
Save and exit.
4. Load the Agent
Now load and start it:
launchctl load ~/Library/LaunchAgents/local.cleanclipboard.plist
launchctl start local.cleanclipboard
It will now run automatically when you log in.
✅ Test It
- Copy some text containing invisible characters (e.g., from Word or PDF).
- Paste it somewhere (
Cmd+V) — it should be clean now.
🛠️ Optional Improvements
If you want better Unicode support, consider rewriting the cleaning part in Python, which handles Unicode more robustly:
Example in Python (optional):
import sys
import pyperclip
text = pyperclip.paste()
cleaned = ''.join(c for c in text if ord(c) not in {0x202F})
pyperclip.copy(cleaned)
Install pyperclip first:
pip install pyperclip
Then update the script accordingly.
🚫 Troubleshooting
- If the script doesn’t run, check permissions and paths.
- Use
log stream --predicate 'process == "clean_clipboard.sh"' to debug logs.
- If clipboard feels sluggish, increase the
sleep 0.5 interval.
Let me know if you'd like a version that uses Python or supports removing additional Unicode characters like Zero Width Space (U+200B), etc.