I’ve been using this, which works, but seems very clumsy to me. When you run it, the frontmost window of the app selected (shown as Script Debugger 4) should turn transparent blue. Clicking the mouse on it should save the screenshot to the desktop. Are there nicer ways?
set snap to "Script Debugger 4" -- not scriptable
set tPath to "~/desktop/"
tell application "System Events"
set P to application processes where it is visible
repeat with theApp in P
if name of theApp is snap then
set W to title of windows of theApp
exit repeat
end if
end repeat
set B to {}
repeat with aTitle in W
if contents of aTitle is not "" then
set B to (contents of aTitle & ".png") as Unicode text
exit repeat
end if
end repeat
end tell
do shell script "screencapture -iW " & tPath & B