A simple Python script for macOS that monitors the clipboard and automatically removes extraneous URL data when copying images from applications like Safari, ensuring cleaner pasting into other apps. Includes a management script for easy installation and setup as a background service.
When copying images from certain applications on macOS (notably Safari), the clipboard often contains both the image data (e.g., TIFF or PNG) and the source URL as plain text. While this can sometimes be useful, many applications don't handle this combination gracefully when pasting. This can lead to unexpected behaviour, such as pasting the URL instead of the intended image.
This project provides fix_clipboard.py, a script that runs quietly in the background, monitoring the macOS clipboard. It specifically looks for instances where both image data and plain text are present simultaneously (but it's not a file copy operation or content from Microsoft applications).
When this pattern is detected, the script automatically:
- Clears the current clipboard contents.
- Re-copies only the image data back onto the clipboard (preferring TIFF format, falling back to PNG if necessary).
The script intelligently ignores:
- File copy operations (e.g., copying files in Finder)
- Content from Microsoft applications (Office, Teams, etc.) that use the
com.microsoft.DataObjectclipboard type
This ensures that pasting into applications that primarily expect image data works smoothly and predictably, while preserving the intended behavior of other applications.
To simplify setup and running the script automatically on login, a management script manage_clipboard_fixer.sh is included.
- Python 3: Developed and tested with Python 3.9+, but should be compatible with most modern Python 3 versions. Command
python3must be available in your PATH. - PyObjC: Python bindings for Apple's Objective-C frameworks are required to interact with the native clipboard. The management script will install the necessary
pyobjc-framework-Cocoapackage automatically into a dedicated virtual environment.
The manage_clipboard_fixer.sh script handles installation, uninstallation, and status checking of the background service.
-
Download or Clone: Get the project files, ensuring
manage_clipboard_fixer.shandfix_clipboard.pyare in the same directory.git clone https://github.com/gavinmorrison/macos-clipboard-fixer.git cd macos-clipboard-fixer -
Make the Management Script Executable:
chmod +x manage_clipboard_fixer.sh
-
Install: Run the script with the
installcommand. It will prompt you for an installation location (defaults to~/Library/Application Support/ClipboardFixer)../manage_clipboard_fixer.sh install
This command performs the following steps:
- Checks for Python 3.
- Asks for and creates the installation directory.
- Copies
fix_clipboard.pyto the installation directory. - Creates a Python virtual environment (
.venv) inside the installation directory. - Installs
pyobjc-framework-Cocoainto the virtual environment. - Creates a
launchdservice.plistfile in~/Library/LaunchAgents/configured to run the script from the installation directory using the virtual environment's Python. The service label defaults tocom.<your_username>.clipboardfixer. - Loads and starts the
launchdservice.
The script will now run automatically whenever you log in. All logs (standard output and errors) are stored in
/tmp/com.<your_username>.clipboardfixer.log.(Customising the Service Label: If you need to use a different label (e.g.,
com.mycompany.clipboardfixer), edit theSERVICE_LABELvariable directly near the top of themanage_clipboard_fixer.shscript before running theinstallcommand.) -
Check Status: You can check if the
launchdservice is loaded:./manage_clipboard_fixer.sh status
You can view the combined log file using:
tail -f /tmp/com.<your_username>.clipboardfixer.log
(Replace
<your_username>with your actual username, or use the customised label if you changed it.) -
Uninstall: To stop the service and remove the
launchdconfiguration:./manage_clipboard_fixer.sh uninstall
This command will:
- Unload the service from
launchd. - Remove the
.plistfile from~/Library/LaunchAgents/. - Ask if you also want to remove the installation directory (containing the script and virtual environment).
(Note: If you customised the
SERVICE_LABELby editing the script before installation, ensure the script still has the same customised label when you rununinstall.) You may need to manually delete the log file (/tmp/com.<your_username>.clipboardfixer.log) if desired. - Unload the service from
If you prefer not to install the background service, you can still set up the environment manually and run the script directly for testing or temporary use.
- Clone or Download: Get the
fix_clipboard.pyscript. - Set up Virtual Environment:
python3 -m venv .venv source .venv/bin/activate pip install pyobjc-framework-Cocoa - Run the Script:
While the virtual environment is active:
Press
python3 fix_clipboard.py
Ctrl+Cto stop it. Use--debugfor verbose logging or--interval <seconds>to change the check frequency.
This script interacts directly with your macOS clipboard, which can contain sensitive information, including passwords. Running any script as a background service carries inherent risks. While this script is designed to be simple and focused, the author is not liable for any damages, data loss, or security breaches that may arise from its use, misuse, or any vulnerabilities.
Users are strongly advised to:
- Review the source code (
fix_clipboard.py) thoroughly before installation to understand its functionality. - Understand the implications of running a script as a
launchdservice. - Use this software at their own risk.
Apple, macOS, and Safari are trademarks of Apple Inc., registered in the U.S. and other countries and regions. This project is not affiliated with, sponsored by, or endorsed by Apple Inc.
This project is licensed under the MIT Licence. See the LICENSE file for details.