11

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:

enter image description here

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:

enter image description here

3 Answers 3

5

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)

#!/usr/bin/osascript
on run args
  set the clipboard to POSIX file (first item of args)
end

Make the file executable (chmod +x /path/to/where/ever/you/put/file-to-clipboard). Then run it like so:

/path/to/where/ever/you/put/file-to-clipboard ~/Desktop/ded.html

If it is stored in a directory in the PATH, then you can omit the path to the “script” file.

4
  • 1
    Unfortunately this doesn't work; when attempting to pasting into Word, there is no output at all. Thanks for your help though. Jun 3, 2017 at 8:20
  • Hmm. The hash-bang script works perfectly for me. I can't think of anything to suggest, other than making sure that Word can import your image file through the normal menu options first. Jun 3, 2017 at 8:55
  • I tried the Bash osascript. I'll try the hash bang one and see if that works. Jun 3, 2017 at 9:02
  • Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change.
    – DavidPostill
    Jun 3, 2017 at 15:10
2

I faced this problem and tried the other answers here and on related questions, but none of them worked - then I found this script, and it worked beautifully: https://gist.github.com/mwender/49609a18be41b45b2ae4

If you want an alternative, I also found this repo which worked: https://github.com/mike-neck/imgcopy

I believe you need to have xcode installed to build the second one though.

0

brew install pink, then you can use piknik to copy anything.
pbcopy < beauty.png works for me sometimes. It's strange that it does not work any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.