Of course! The term nspasteboard in Python almost always refers to the pyobjc framework, which provides a bridge between Python and Apple's Objective-C runtime. This allows you to interact with native macOS APIs, including the system pasteboard (also known as the "general pasteboard").

Here's a comprehensive guide on how to use Python to interact with the macOS pasteboard.
Installation: pyobjc
First, you need to install the necessary pyobjc packages. The easiest way is with pip.
pip install pyobjc-core pyobjc-framework-Cocoa
pyobjc-core: The core framework for the Python-Objective-C bridge.pyobjc-framework-Cocoa: The bindings for Apple's Cocoa framework, which contains the pasteboard classes.
Key Concepts
Before diving into code, it's helpful to understand the main players from the Objective-C world that you'll be using via pyobjc:
NSPasteboard: This is the main class representing the system pasteboard. You get an instance of it to read from or write to it.NSPasteboardTypeString: A constant representing the "string" data type. This is what you use for plain text.NSPasteboardTypePNG: A constant for PNG image data.NSPasteboardTypeTIFF: A constant for TIFF image data (often used for copied images).NSPasteboardTypeURL: A constant for a URL string.clearContents(): A method to clear everything on the pasteboard.stringForType(): A method to retrieve a string if the pasteboard contains data of the specified type.setString(): A method to add a string to the pasteboard.writeObjects(): A more general method to write various types of objects, including images.propertyListForType(): Used for more complex data types like property lists (e.g., a list of strings or dictionaries).
Practical Examples
Let's create a Python script with functions to read and write different types of data to the pasteboard.

Example: A Complete Script (pasteboard_manager.py)
This script includes functions to get and set strings, images, URLs, and lists.
import sys
from AppKit import NSPasteboard, NSPasteboardTypeString, NSPasteboardTypePNG, NSImage, NSURL
from Foundation import NSData
# --- Helper to get the general pasteboard ---
def get_pasteboard():
"""Returns the shared system pasteboard."""
return NSPasteboard.generalPasteboard()
# --- WRITING TO THE PASTEBOARD ---
def set_string(text):
"""Sets a string to the pasteboard."""
pb = get_pasteboard()
pb.clearContents() # Clear existing items
pb.setString_forType_(text, NSPasteboardTypeString)
print(f"Set string to pasteboard: '{text}'")
def set_image(image_path):
"""Sets an image from a file path to the pasteboard."""
pb = get_pasteboard()
pb.clearContents()
# Load the image using NSImage
image = NSImage.alloc().initWithContentsOfFile_(image_path)
if image:
# Write the image object to the pasteboard
pb.writeObjects_([image])
print(f"Set image from '{image_path}' to pasteboard.")
else:
print(f"Error: Could not load image from '{image_path}'")
def set_url(url_string):
"""Sets a URL to the pasteboard."""
pb = get_pasteboard()
pb.clearContents()
# Create an NSURL object
url = NSURL.URLWithString_(url_string)
if url:
# Write the URL object
pb.writeObjects_([url])
print(f"Set URL to pasteboard: '{url_string}'")
else:
print(f"Error: Invalid URL string '{url_string}'")
def set_list_of_strings(items):
"""Sets a list of strings as a single item on the pasteboard."""
pb = get_pasteboard()
pb.clearContents()
# Property lists are a common way to pass structured data
pb.setPropertyList_forType_(items, NSPasteboardTypeString)
print(f"Set list of strings to pasteboard: {items}")
# --- READING FROM THE PASTEBOARD ---
def get_string():
"""Gets the first string from the pasteboard."""
pb = get_pasteboard()
string = pb.stringForType_(NSPasteboardTypeString)
if string:
print(f"Got string from pasteboard: '{string}'")
return string
else:
print("Pasteboard does not contain a string.")
return None
def get_image_data():
"""Gets PNG image data from the pasteboard, if available."""
pb = get_pasteboard()
# Try to get PNG data first
data = pb.dataForType_(NSPasteboardTypePNG)
if data:
print("Found PNG image data on pasteboard.")
return data
# If no PNG, try TIFF
data = pb.dataForType_(NSPasteboardTypeTIFF)
if data:
print("Found TIFF image data on pasteboard.")
return data
print("Pasteboard does not contain an image.")
return None
def get_url():
"""Gets a URL string from the pasteboard."""
pb = get_pasteboard()
# The URL object itself can be retrieved
url_object = pb.readObjectsForClasses_options_([NSURL], None)
if url_object:
url_string = url_object[0].absoluteString()
print(f"Got URL from pasteboard: '{url_string}'")
return url_string
print("Pasteboard does not contain a URL.")
return None
def get_list_of_strings():
"""Gets a list of strings that was set as a property list."""
pb = get_pasteboard()
plist = pb.propertyListForType_(NSPasteboardTypeString)
if plist and isinstance(plist, list):
print(f"Got list of strings from pasteboard: {plist}")
return plist
print("Pasteboard does not contain a list of strings.")
return None
# --- Main execution block for testing ---
if __name__ == '__main__':
if len(sys.argv) > 1:
command = sys.argv[1]
if command == "set_string":
set_string("Hello from Python!")
elif command == "get_string":
get_string()
elif command == "set_image":
# You need to have an image file named 'test.png' in the same directory
set_image("test.png")
elif command == "get_image":
img_data = get_image_data()
if img_data:
print(f"Image data length: {len(img_data)} bytes")
elif command == "set_url":
set_url("https://www.python.org")
elif command == "get_url":
get_url()
elif command == "set_list":
set_list_of_strings(["apple", "banana", "cherry"])
elif command == "get_list":
get_list_of_strings()
else:
print("Unknown command. Available commands:")
print(" set_string, get_string")
print(" set_image, get_image")
print(" set_url, get_url")
print(" set_list, get_list")
else:
print("No command provided. Run with a command, e.g., 'python pasteboard_manager.py set_string'")
How to Run the Example
-
Save the code above as
pasteboard_manager.py. -
For the image test, create a simple image file named
test.pngin the same directory. -
Run it from your terminal:
(图片来源网络,侵删)-
Set a string:
python pasteboard_manager.py set_string
Now, paste into any app (e.g., TextEdit). You should see "Hello from Python!".
-
Get the string:
# First, copy some text into your clipboard python pasteboard_manager.py get_string
-
Set an image:
python pasteboard_manager.py set_image
Paste into Preview or an image editor. Your
test.pngshould appear. -
Get a URL:
# First, copy a URL from your browser python pasteboard_manager.py get_url
-
Set and get a list:
python pasteboard_manager.py set_list python pasteboard_manager.py get_list
-
Important Considerations
- macOS Only: This code will not work on Windows or Linux.
pyobjcis specific to macOS. - Permissions: Modern macOS versions have strict privacy controls. Your script might need to request "Accessibility" or "Full Disk Access" permissions if it's trying to read the clipboard in the background, especially from non-Apple apps. For simple, one-off script execution, this is usually not an issue.
- Clearing the Pasteboard:
pb.clearContents()is crucial if you want your script to be the only thing on the clipboard. Without it, you might be adding your data on top of whatever was already there. - Error Handling: The examples include basic error handling (e.g., checking if an image file was loaded). In production code, you would want more robust error handling.
Alternatives to pyobjc
While pyobjc is the most direct and powerful way, there are other libraries that offer a simpler, cross-platform (but macOS-capable) API.
pyperclip
pyperclip is the go-to library for simple, cross-platform clipboard operations. It handles the platform-specific details for you.
Installation:
pip install pyperclip
Usage:
import pyperclip
# Set text to the clipboard
pyperclip.copy("This is a test from pyperclip.")
# Get text from the clipboard
text = pyperclip.paste()
print(f"Pasted text: {text}")
Limitation: pyperclip is primarily for plain text. It does not support images or other data types.
clipboard
Another simple library, similar to pyperclip.
Installation:
pip install clipboard
Usage:
import clipboard
# Set text
clipboard.copy("Text from the 'clipboard' library.")
# Get text
text = clipboard.paste()
print(f"Pasted text: {text}")
Limitation: Also primarily for plain text.
Summary: Which to Use?
| Library | Use Case | Pros | Cons |
|---|---|---|---|
pyobjc |
Full control. When you need to handle strings, images, URLs, custom data types, or interact with other macOS-specific clipboard features. | - Full power of macOS APIs - Handles all data types - "Native" performance |
- macOS only - Steeper learning curve (Objective-C concepts) - More verbose syntax |
pyperclip |
Simple text. The vast majority of use cases where you just need to copy and paste plain text. | - Extremely simple API - Cross-platform (macOS, Windows, Linux) |
- Only handles plain text - Relies on external command-line tools on some platforms |
clipboard |
Simple text. A lightweight alternative to pyperclip. |
- Very simple API - Pure Python implementation on macOS |
- Only handles plain text - Less widely used/maintained than pyperclip |
For any task more complex than just text, pyobjc is the correct and necessary tool in Python on macOS. For simple text, pyperclip is highly recommended for its simplicity and cross-platform nature.
