print #handle, "!contents? string$";

This command returns the entire text of the window.  After this command is issued, the entire text is contained in the variable string$.