Take a screenshot of the top-most window

I want to have an AppleScript that takes a screenshot of the front most application. I have used this AppleScript:

tell application "System Events" -- get frontmost process
   set frontmostProcess to first process where it is frontmost -- this will be the script process
   set visible of frontmostProcess to false -- hide the script process
   repeat while (frontmostProcess is frontmost) -- wait until the script is hided
       delay 0.2
   end repeat
   set secondFrontmost to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process)
   set frontmost of frontmostProcess to true -- unhide the script process
end tell
tell application secondFrontmost to set winID to id of window 1 -- get WindowID of frontmost window of frontmost process
do shell script "screencapture -c -x -l " & winID -- -c is used to store it in the clipboard. -x is used to mute the sound. -l is used to refer to the prefered windowid.

but it doesn't work for Snow Leopard. If anybody could give me some guidance as to what is the unsupported section of the code, I'd be happy to go and research myself.

I think Snow Leopard doesn't support the frontmost part but I can't get any information to back this assumption up...


Cmd+Shift+4 and Space and Click Window does work but I want an Applescript so I could replace the shortcut to open the Applescript - which will automatically take a picture of the active window.

This is so I don't have to click space and then click the windows I want.

I take screenshots a lot and constantly clicking space and clicking is getting annoying now.

The applescript, essentially, skips the space+click window


I just hacked together this little tool.

https://github.com/vorgos/QuickGrab

It is a command line tool that will capture the top most active window or any window you specify just like the -l option in the newer version of screencapture.

Hope that helped.


Taking a screenshot of the front-most window (via shift-command-4 spacebar click) won't suit?