I have an image, Test.png
, which I want to copy to the clipboard from the terminal as part of a Bash script so I can paste it into Word.
I've tried using pbcopy
like this:
$ cat Test.png | pbcopy
However, when I try to paste the contents of the clipboard into Word, I get multiple pages of random characters. I assume the clipboard is flagged as containing text, so pastes a text representation of the image bytes:
How can I correctly copy an image from the terminal? This appears to happen with all PNG images, but if it helps the image that I'm using is this one:
Best Answer
Here is a link to an answer by Chris Johnsen on a related question that does what you're looking for. (In that case it's an HTML file, but it works for our needs here.)
https://apple.stackexchange.com/a/15542
Quote from the original answer:
osascript can ... be used as a hash-bang interpreter (since 10.5). Put this in a file (e.g. file-to-clipboard)
Make the file executable (
chmod +x /path/to/where/ever/you/put/file-to-clipboard
). Then run it like so:If it is stored in a directory in the PATH, then you can omit the path to the “script” file.