Working With The Clipboard
PyXA affords full access to the system clipboard (or pasteboard, in Objective-C terms), making it easy to read content from or write content to the clipboard from within Python. PyXA provides a convenient XAClipboard
class to handle clipboard-related actions.
Reading Clipboard Content
To read content from the clipboard, you need only to instantiate an instance of XAClipboard
and access its content
property. This will provide a dictionary containing the raw NSData object, property list, and string associated with each object type in the clipboard. The example below shows this in action. In this case, the text “Testing 1 2 3…” was copied from a plaintext file, where no formatting data is present.
import PyXA
cb = PyXA.XAClipboard()
print(cb.content)
# {'public.utf8-plain-text': {'data': {length = 16, bytes = 0x54657374696e672031203220332e2e2e}, 'properties': None, 'strings': 'Testing 1 2 3...'}}
In comparison, when running the code after copying text from within Pages, we also receive the formatting data objects and their associated properties and strings, as shown below. Note that we use print instead of pprint in order to save space here, but the output follows the save structure as before: for each type of data contained in the clipboard, we receive the NSData object, the property list, and the string associated with that data type. The key difference is that copying formatted text places several types of data on the clipboard, including the plaintext representation, while copying plaintext directly only involves a single clipboard type.
import PyXA
cb = PyXA.XAClipboard()
print(cb.content)
# {'com.apple.flat-rtfd': {'data': {length = 408, bytes = 0x72746664 00000000 03000000 02000000 ... 00000000 00000000 }, 'properties': None, 'strings': None}, 'public.rtf': {'data': {length = 317, bytes = 0x7b5c7274 66315c61 6e73695c 616e7369 ... 20322033 2e2e2e7d }, 'properties': None, 'strings': '{\\rtf1\\ansi\\ansicpg1252\\cocoartf2638\n\\cocoatextscaling0\\cocoaplatform0{\\fonttbl\\f0\\fnil\\fcharset0 HelveticaNeue;}\n{\\colortbl;\\red255\\green255\\blue255;\\red0\\green0\\blue0;}\n{\\*\\expandedcolortbl;;\\cssrgb\\c0\\c0\\c0;}\n\\deftab720\n\\pard\\pardeftab720\\partightenfactor0\n\n\\f0\\fs22 \\cf2 \\up0 \\nosupersub \\ulnone Testing 1 2 3...}'}, 'public.utf16-external-plain-text': {'data': {length = 34, bytes = 0xfffe5400 65007300 74006900 6e006700 ... 33002e00 2e002e00 }, 'properties': None, 'strings': None}, 'public.utf8-plain-text': {'data': {length = 16, bytes = 0x54657374696e672031203220332e2e2e}, 'properties': None, 'strings': 'Testing 1 2 3...'}, 'org.nspasteboard.TransientType': {'data': {length = 0, bytes = 0x}, 'properties': None, 'strings': ''}, 'org.nspasteboard.AutoGeneratedType': {'data': {length = 0, bytes = 0x}, 'properties': None, 'strings': ''}}
You can parse the content dictionary to obtain the exact information that you need, or you can use built-in convenience functions to obtain the most common data types. These functions currently include get_strings()
, get_urls()
, and get_images()
. For example:
import PyXA
cb = PyXA.XAClipboard()
print(cb.get_strings())
# ['Testing 1 2 3...']
import PyXA
cb = PyXA.XAClipboard()
print(cb.get_urls())
# [<<class 'PyXA.XABase.XAPath'>file:///Users/exampleUser/Documents/example.jpeg>]
import PyXA
cb = PyXA.XAClipboard()
print(cb.get_images())
# [<PyXA.XABase.XAImage object at 0x11c551e20>, <PyXA.XABase.XAImage object at 0x11c551fa0>, <PyXA.XABase.XAImage object at 0x100cb9310>]
Writing Content to the Clipboard
To write content to the clipboard, set the content
property of the XAClipboard
object. This works for Python’s base literal classes, some general Python classes, and all PyXA classes. The example below highlights how this functionality can be used for different data types.
import PyXA
cb = PyXA.XAClipboard()
cb.content = "Testing 1 2 3"
cb.content = 42
cb.content = True
cb.content = [1, 2, "Three"]
image = PyXA.XAImage("/Users/exampleUser/Documents/Test.jpg")
cb.content = image
path = PyXA.XAPath("/Users/steven/Downloads/Test.jpg")
cb.content = path
url = PyXA.XAURL("http://macrumors.com")
cb.content = url
sound = PyXA.XASound("/Users/steven/Downloads/Test.mp3")
cb.content = sound
For strings and other literal values, the plaintext form of the value will be added to the clipboard. For PyXA class objects, one or more items representing the object in multiple forms will be added to the clipboard. For example, setting the clipboard content to an XAURL
object will add the URL as a URL type and as a plaintext string, while setting the content to an XASound
object will add the raw sound data, a file URL, and the file path string. More information on how each PyXA object type is represented can be found in the corresponding code reference material.
You can also copy XAList
objects to the clipboard in the same way. When you do this, data for each element in the list is added to the clipboard. The type of data added to the clipboard varies based on the list type, but it generally matches the data type added for a single element of the corresponding class. For example, for Music tracks, a single track copied to the clipboard adds the track name to the clipboard, while a list of tracks copies the names of each track to the clipboard (as separate items).
import PyXA
tracks = PyXA.Application("Music").tracks()
PyXA.XAClipboard().content = tracks