screenshot of active or frontmost window

Hi All,
Is there any chance of capturing an active window using applescript or some other routines without user interaction.
The screenshot must be the same as using CMD+Shift+4; space; mouse click.

The solution is not very elegant but it can probably make the deal

set p2pict to POSIX path of (((path to desktop) as Unicode text) & "_myWindow.png")
set qfp2pict to quoted form of p2pict

-- get screen dimensions
copy {word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number, ¬
	(word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number)} to {scrn_w, scrn_h}

tell application "System Events"
	set proc to name of first process whose frontmost is true
	tell window 1 of process proc
		-- get window initial position
		copy position to {ini_x, ini_y}
		copy size to {win_w, win_h}
		
		-- center window because the sips crop command automatically centers the cropped area in the image
		set position to {(scrn_w - win_w) / 2, (scrn_h - win_h) / 2}
	end tell
	
	-- capture the screen , crop the image and open it.
	do shell script "screencapture" & space & "-wx" & space & qfp2pict & "; sips  -c" & space & (win_h) & space & (win_w) & space & qfp2pict & space & "--out" & space & qfp2pict & "; open" & space & qfp2pict
	
	-- revert window position
	tell window 1 of process proc to set position to {ini_x, ini_y}
end tell

WOW Outstanding!!

Tom

Browser: Safari 528.16
Operating System: Mac OS X (10.5)