• How to take a Screen Shot from the Command Line in OS X

    I recently had some issues with VNCing in to my Mac at home over an SSH tunnel, and couldn’t figure out what was wrong with it. I could get an SSH shell no problem, and even mount a directory share over AFP, but when firing up VNC over the tunnel I just got a black screen. I was trying to VNC in since I couldn’t get my home iTunes library share through to my work computer over SSH, which usually means that a new version of iTunes has installed itself and the stupid license agreement is waiting to be approved.

    So, I did a little Googling and found that there is a utility on OS X called screenCapture, found in /usr/sbin/screencapture. From the man page:

    To capture screen content while logged in via ssh, you must launch screencapture in the same mach bootstrap hierarchy as loginwindow:
         PID=pid of loginwindow
         sudo launchctl bsexec $PID screencapture [options]

    So, at the time my loginwindow process had a PID of 35, so I used the following.

    sudo launchctl bsexec 35 screencapture /Users/porgesm/cap.png

    This wrote a screen capture to the file cap.png in my home directory. I then used Quick Look in Finder to view the file (since I had mounted my home directory over AFP), and that let me see that my computer did indeed have an iTunes license agreement up. Working from memory, I sent a few key commands over my VNC connection, and ran the screencapture command to see what I was doing. I was able to remotely accept the iTunes license agreement and get iTunes sharing working again.

    As it turned out, there was something about Finder’s VNC client on my laptop that was screwy. After changing the image quality for screen sharing several times, the picture for VNC returned and I was back in business. However, I thought the screen capturing technique was useful, so I’m blogging about it here.

    Category: Uncategorized | Tags: