Jean-Francois T.
Jean-Francois T.

Reputation: 12940

Screenshot with Tcl (for window not written in Tk)

I need to use Tcl to capture a screenshot of a given program not written with Tk (for example Firefox).

All I found is how to use Img to capture Tk applications.

Any idea on how to do that?

Thanks

Upvotes: 0

Views: 614

Answers (1)

Yusuke
Yusuke

Reputation: 23

You can find a useful information here (for Windows only.)

http://wiki.tcl.tk/15647

Take an example of "wish.exe" assuming that it's default title is "wish".

package require twapi

set pid [exec C:/Tcl/bin/wish.exe &]
set hwnd [twapi::find_windows -text "wish" -toplevel 1 -visible 1 -single]

twapi::set_focus $hwnd
twapi::send_keys {%{PRTSC}}; # type Alt+PrintScreen key
after 200; # avoid an access denied error.
set ts [clock format [clock seconds] -format "%Y%m%d%H%M%S"]
set filename "screenshotwin_$ts.bmp"
set phImg [Clipboard2Img]; # refer to the wiki page
$phImg write [file rootname $filename].png -format PNG

Or simply call nircmd. This is better than depending on the Alt+PrintScreen key.

exec C:/Tcl/bin/wish.exe &
after 100
exec nircmd.exe win activate title "wish"
after 100
exec nircmd.exe savescreenshotwin "screenshotwin.png"

Upvotes: 1

Related Questions