webmages

April 8, 2003

osx screenshots (in png)

Filed under: Uncategorized — Mark Andrachek @ 4:56 am

With OSX Jaguar, it’s pretty easy to take a screenshot using the screencapture command line utility, for example:

screencapture -x desktop.pdf

The only problem is that it’s in pdf format. In previous versions of OSX it was possible to change this format, but it appears that this functionality is lost in Jaguar. Ok. So I want to display this on the web. We need to get it into a more common compressed format that will load directly in a browser.

I’ve been familiar with and used the ImageMagick convert program for some time, so I decided to download their Darwin binaries and give them a shot. It took a bit of hassel, but it worked.

First I tried expanding the archive under /usr/local/. No matter what I did with setting environment variables it kept saying it couldn’t find files in /ImageMagick-5.5.6/lib/. Ok, I gave up and moved it from /usr/local/ to /. I set the PATH to include /ImageMagick-5.5.6/bin, and set MAGICK_HOME to /ImageMagick-5.5.6/. The convert command line utility now actually responds usefully.

Yay! ….. Not.

I try and run “convert desktop.pdf desktop.png”. Arg! No! It doesn’t work!

Unrecoverable error: rangecheck in .putdeviceprops
convert: Postscript delegate failed

What to do. I check out pdf* on my system and notice that there’s a pdf2ps program, which converts the pdf document to a postscript document. It works, and produces desktop.ps. Will convert work on the postscript file?

Yes! “convert desktop.ps desktop.png” results in a valid png file!! Yay!

Now all that’s needed is a script to put it all together right? WRONG. The size of the file that was output was way too small. It turns out that when the display is dimmed in sleep mode, you get a solid black image. I stumbled upon a simple program, undim that can take care of this for you on the command line. I put mine in /usr/local/bin

Lastly, you probably don’t want a screenshot of your screen saver, so you’ll need a tidbit of a command line to turn it off:

kill `ps -caxwwopid,command | grep ScreenSaverEngin | cut -b 1-6`

Finally we come to the script:

#!/bin/bash
PATH=$PATH:/ImageMagick-5.5.6/bin:/usr/local/bin
MAGICK_HOME=/ImageMagick-5.5.6/
export PATH
export MAGICK_HOME
cd ~/Sites
undim > /dev/null
kill `ps -caxwwopid,command | grep ScreenSaverEngin | cut -b 1-6`
screencapture -x desktop.pdf
pdf2ps desktop.pdf
convert desktop.ps desktop.png
rm desktop.pdf
rm desktop.ps

2 Comments »

  1. You should check out the ’sips’ command line tool in MacOS X, which is faster and should save you the overhead of converting to postscript first before converting to jpeg.

    Comment by Sveinbjorn Thordarson — February 4, 2004 @ 9:47 pm

  2. sips looks cool, however it’s only available in 10.3 and later.

    Comment by hallow — April 19, 2004 @ 4:47 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Blog at WordPress.com.