Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: An alias to ease screen captures of remote Macs UNIX
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: 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.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,053 views]  

10.5: An alias to ease screen captures of remote Macs | 9 comments | Create New Account
Click here to return to the '10.5: An alias to ease screen captures of remote Macs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: An alias to ease screen captures of remote Macs
Authored by: skrawcke on Dec 20, '07 08:09:19AM
ok that is a whole lota work for something you can do as easy as connect to the remote computer via Screen Sharing and taking a screen grab from your local mac.

Take a look at this
http://www.macworld.com/article/131094/2007/12/screensharepower.html
and you can even extend the utility of Screen Sharing.




[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: fds on Dec 20, '07 08:18:49AM

I'm sure Rob will appreciate the irony of seeing one of his own articles recommended to him. :)



[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: adrianm on Dec 20, '07 08:34:08AM

Can you screen capture the login window using Screen Sharing though?

---
~/.sig: not found



[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: robg on Dec 20, '07 02:44:09PM

I wasn't able to do so, which is why I came up with this solution.

-rob.



[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: skrawcke on Dec 22, '07 03:34:13PM

I just tried it and was able to get it to work..



[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: fds on Dec 20, '07 08:13:38AM

In bash, aliases don't take parameters -- they are not like functions.
More like simple text expansion, it simply prepends the alias' expanded content in front of the rest of your command line.

So that $1 $2 $3 at the end of your alias doesn't actually have any effect.
Better yet, it never was limited to taking at most 3 additional parameters, you could in fact specify any number of them. :)



[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: adrianm on Dec 20, '07 08:29:50AM
To find the loginwindow for a particular user:

ps doesn't seem to let you choose 'processes without controlling terminal' and 'specific user' at the same time, but you can do this:


ps -ax -o user,command,pid | grep [l]oginwindow |  awk '$1=="adrian" {print $2}'
which would get the pid of the loginwindow owned by "adrian"

---
~/.sig: not found

[ Reply to This | # ]

10.5: An alias to ease screen captures of remote Macs
Authored by: adrianm on Dec 20, '07 08:32:03AM

'course, I could combine the grep into the awk expression, but then no-one else would have scope to improve it ;-)

---
~/.sig: not found



[ Reply to This | # ]
10.5: An alias to ease screen captures of remote Macs
Authored by: Unsoluble on Dec 20, '07 02:36:49PM

Or use Remote Desktop?



[ Reply to This | # ]