Images on the clipboard via script
March 10, 2014 at 10:07 PM by Dr. Drang
Last night I had an idea for improving my snapflickr
screen capture script. Although, as its name implies, snapflickr
was written to upload screen snapshots to my Flickr account, I often use it to create local files that I edit in Acorn before uploading. My bright idea was to have snapflickr
put the image on the clipboard so I didn’t have to dig around in the file system to find the just-created image file. In the process of adding this feature, I found a very useful little command line utility that does for images what pbcopy
does for text.
Acorn looks at the clipboard when you select
and populates the size fields with the properties of the image that’s there.You can instantly create a new file with the contents of the clipboard by either clicking the
button or typing ⌘V. Either way, if I need to edit a screenshot before uploading it to Flickr, it’s much more convenient to get the image into Acorn this way than to open it from my Screenshots folder.The problem, though, is getting the image onto the clipboard from within snapflickr
. My searches for a supported Python library that works with the Mac clipboard came up empty.1 And while pbcopy
can work with PostScript images (a throwback to its NeXT roots, I assume), it can’t work with the PNGs I like to generate from screenshots.
More Googling led me to this question on Stack Overflow,2 which led me to Alec Jacobson’s lovely little impbcopy
utility. Alec provides the source code for a short program that takes an image as the first argument and pops it into the clipboard:
impbcopy image.png
It can also accept standard input by giving it the hyphen (-
) argument:
cat image.png | impbcopy -
You’ll need Xcode and the command line developer tools installed to compile impbcopy
. If which gcc
returns a useful path, you’ll probably have no trouble compiling. Just follow the instruction on Alec’s site.
Once I had impbcopy
installed, adding clipboard support to snapflickr
was as easy as pie. Just a few lines with subprocess
. More important, I now have a tool I can use in other image processing scripts.
-
Yes, there’s PyObjC, but I’m leery of spending my time learning a library that was all the rage several years ago. And if I’m going to learn Cocoa, I’d rather do it in Objective C itself instead of this underscore-laden hermaphrodite. ↩
-
Virtually every programming question on Google leads to Stack Overflow. ↩