Screen capture of just window

I’ve found where you can issue a shell command using AppleScript to do a screen capture of the entire screen. But does anyone know how to capture just a select window?

I’m wanting to do a screen capture of a Safari window, then take that screen capture and crop out just the section I need. I’ve already figured out how to do the crop, but I can’t find out how to just do the Safari window and not the entire screen.


As you can probably see from the man page for [screencapture] in the Terminal, the documentation is pretty crummy for this command, cool though it is. This code works, but does require some interaction from the user:

display dialog "Place the cross in the window you want, press the space bar to select, and press the mouse button to capture."
do shell script "screencapture -iw ~/desktop/screen01.png"

Incidentally, the window you select DOES NOT need to be in the front, and ONLY the selected window is captured, regardless of what happens to be in front of the selected window.

Good luck,

Wouldn’t there be a way of sending the command-shift-4-caps lock combination and force a control click of the window.

display dialog "blah" giving up after 5

Will dismiss the dialog after N seconds.

Returns “gave up:true” with no button returned.

Just use command-shift-4, press space bar, and click on the Safari window.

I suspect the poster knows about that:

It sounds like he needs a scripted method specifically, possibly as an academic exercise.

Ok, I guess I need to give little more of the details of what I’m doing. And yes, I do know how to do a screen capture manually. This script will run on it’s own, on a dedicated computer, and can not depend on any human interaction.

This will be automated AppleScript running through several thousand web pages and I’m wanting to take screen shots of each one. I know there are other methods of saving stuff like this, like curling the html source. But I need a screen shot frozen in time and not dependent on information not being there months later.

So let’s say instead of using a shell command and calling system events, what would the AppleScript command look like doing “keystroke” with two modifier keys down. CMD-SHIFT-4

<keystroke “4” using command and shift down> doesn’t work

It should be tell application “System Events” keystroke “4” using {command down, shift down}

You could also “freeze” the contents of a page by using a webarchive or printing to PDF. For info on the latter, see my comments here:

Thank you everyone for your recommendations and suggestions. I think I have enough information to complete what I’m trying to do, but if anyone has any more ideas, I will definitely appreciate it. I have learned long time ago that there are many ways to accomplish a simple task in any programming language.

If that is the intended function you desire, I would suggest the printing into PDF as a script like the above poster. Just my 2 cents.