What's the quickest way to get a graphic on the clipboard saved to disk?

I have a graphic on the clipboard on OS X.

What's the quickest way to get it onto disk as a png or jpg?


Maybe the File | New From Clipboard menu of /Application/Preview.app.

enter image description here


Here is a utility to do just that.

pngpaste

Paste PNG into files, much like pbpaste does for text.

However instead of pngpaste > thefile.png, it's pngpaste thefile.png, so one does not accidentally barf binary into the console.


If you have Preview.app open you can simply 'create new' cmd+n and that will generate the proper canvas and paste the clipboard image. Only thing left to do is save that file. Presto!


Not nearly as slick, but without using Preview.

Finder -> Edit -> Show Clipboard

Cmd+Shift+4 to get the screen shot marquee tool, and copy the part of the clipboard you want. It's now a PNG on your desktop. But probably not precisely the same image file.

But at that rate, you probably could have screen-captured the original source using the same method and went right to the PNG without using the clipboard.


From terminal, you can get an image from the clipboard with osascript.

Define the following functions. The first function gets the clipboard contents as a string of hex digits. The second function decodes the hex digits into binary.

# get clipboard as <class>
getclip() {
  local class=$1; shift; : ${class:?}
  osascript -e "get the clipboard as «class ${class}»"
}

# get clipboard as <class> (decoding hex string)
getclipb() {
  local class=$1; shift; : ${class:?}
  getclip "$class" | sed "s/«data ${class}//; s/»//" | xxd -r -p
}
$ getclipb PNGf >x.png

You can print the clipboard information (current set of data formats & sizes) with this function:

# print clipboard info
cbi() {
  osascript -e "clipboard info" |
  sed -E 's/, /,/g; s/,([0-9]+)/:\1/g' | tr ':,' '\t\n'
}
$ cbi | expand -t 16
«class PNGf»    3970
«class 8BPS»    4610
GIF picture     60
«class jp2 »    4367
JPEG picture    4877
TIFF picture    4810
«class BMP »    534
«class TPIC»    68