10.5: An alias to ease screen captures of remote Macs

Dec 20, '07 07:30:08AM

Contributed by: robg

Given what I do for Macworld, I take a lot of screenshots. Sometimes I need to take those shots remotely -- for instance, to grab a screnshot of the login window, you must connect to a logged-out Mac via ssh, then use the screencapture command, as described in this older hint.

With 10.5, though, the rules have changed, as described in the man pages for screencapture:

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 to take a screen capture, you need to first get the PID of the loginwindow, which you can do via ps ax | grep [l]oginwindow in Terminal. The PID is the first number in the output; assuming it was 935, you'd then execute sudo launchctl bsexec 935 screencapture, followed by your desired screencapture options.

Being the lazy sort, this seemed like too much work, so I came up with the following solution.

On any machine whose screen you'd like to capture remotely, add this alias to the .profile (or .bash_profile, etc.) file:

alias capscreen='sudo launchctl bsexec `ps ax | grep [l]oginwindow | cut -c 1-5` screencapture $1 $2 $3'
How it works

This is a pretty simple alias. If you're newer to scripting than I am :), here's basically what it does. alias capscreen='... creates a new command named capscreen. The next bit, up to bsexec, is the syntax as specified in the man pages. Everything between the backticks (`ps ax...$2 $3`) is then run, and thanks to the backticks, only the result of that string of commands is returned to the alias.

In this case, the backticked section runs ps ax to list the jobs, then greps for the loginwindow string (the [l] is a handy trick to prevent grep from returning a match for itself), and cuts the first five columns of the result, which is the PID. The last part of the command, starting with screencapture, tells the machine to capture the screen with up to three ($1 $2 $3) command-line options. Finally, a closing quote (') specifies the end of the alias.

Using the alias

After ssh'ing into the remote Mac, type capscreen opt1 opt2 /file/to/be/saved/name.ext. For instance, to take a TIFF image with a delay of 10 seconds and save it to the remote user's Desktop, the command would look like this:
capscreen -ttiff -T10 /Users/theuser/Desktop/screenpic.tif
Provide your admin password when prompted, and the screenshot will be saved in the location you specified. (You'll need to use a full path, not the ~/ shortcut variety.) I set up the alias to handle three arguments, as that's the most I need -- I usually only use two, one for the type and the second for the save location. If you need more arguments, though, just add them to the alias ($4 $5 $6 etc.).

Caveat: This simple alias assumes there's only one loginwindow process. If the other Mac has fast user switching enabled, and another user is logged in, then there will be two loginwindow processes, and my alias will fail. I'm sure there are ways to work around this, but they were beyond my simple shell scripting capabilities. Feel free to offer up solutions in the comments.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071220052216569