Binary (image) data clipboard in macOS
When I copy things to the clipboard, I can dump them into my console window or redirect to a file using:
pbpaste > out.txt
But if I right click an image and copy it in a browser, then att开发者_Python百科empt:
pbpaste > out.jpg
Nothing is outputted.
Where does macOS store the image data in the clipboard? Is there some way to access it from the commandline, similarly to pbpaste?
In response to a question from Jeff a utility was written to let you paste graphics to a PNG.
man pbpaste
says that it only looks for plain text, rich text, or encapsulated postscript. I don't know any command that handles more general pasteboard data, but it probably wouldn't be hard to write one.
For anyone looking for how to store arbitrary binary data in the clipboard, it seems this may not be practical. In part, I believe because there's only a handful of types the pasteboard accepts. None of which are suitable for arbitrary unformatted data.
You can seemingly lie to the pasteboard and place arbitrary data into one of the non-string types like, PDF pasteboardType and later read the same data back from the clipboard.
Using the Python pasteboard package, (a small Objective-C program with Python bindings) this can be demonstrated:
import secrets
data = secrets.token_bytes(100) # generate random bytes
import pasteboard
pb = pasteboatd.Pasteboard()
# set the data to the pasteboard
pb.set_contents(data, pasteboard.PDF)
# get the contents back, same as before
pb.get_contents(pasteboard.PDF) == data # True
精彩评论