WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DOS

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Grab Text from a Console Window

Keywords: 	 console text grabber

The following script gets text from a console window. Any text.

_console_text_grabber.WBT

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Name: _console_text_grabber
;
; Version:	1.0
; Date:     5/6/2000
; Author:	D. Rayner
; Description: gets text from a console window.  Any text.
;
; Usage: call("console_text_grabber.wb[c/t]","[return variable] [from_row] [from_column] [to_row] [to_column]")
;
; e.g.   call("console_text_grabber.wbt","givemeit 23 1 23 20") {returns the first 20 characters of the 23rd line of text to the variable 'givemeit'} 
;
;
; All parameters are optional, but a variable "screen_name" must be set in the calling program, and refers to the name of the console.
;
; Required variables: screen_name   (the name of the console window. Be as specific as you can. Case sensitive. Set in calling program)
; Optional variables: return variable (a variable to return to) 
;                     from_row, from_column, to_row, to_column. (co-ordinates, based on 25 row x 80 column screen)
; Defaults: Coordinates default to entire screen. Return variable defaults to 'console_text_grabber' 
;           Coordinates must be included in the command line left-to-right. You can specify only the first [n], but not the last [n].
;
; Requirements: Must be a console window (not full screen), and 'QuickEdit' must be enabled.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;some constants. These shouldn't really need to change, but some terminal emulators my be different. Maybe.

max_col=80 ;80 characters
max_row=25 ; in 25 rows

base_delay=0.035   ;change this if the timing is a worry.


;sort out the parameter line
if isdefined(param1) then
	if !isnumber(param1) then
		console_text_grabber=param1 ;return the text to the variable named in param1
		if isdefined(param2) then from_row=param2
		if isdefined(param3) then from_column=param3
		if isdefined(param4) then to_row=param4
		if isdefined(param5) then to_column=param5
	else 
		console_text_grabber= "console_text_grabber";return the text to a variable hipscreen
		if isdefined(param1) then from_row=param1
		if isdefined(param2) then from_column=param2
		if isdefined(param3) then to_row=param3
		if isdefined(param4) then to_column=param4
	endif
endif


bad_screen=@FALSE
;check to see if there's a valid window hanging around anywhere.
if !winexist(screen_name) then  ;must exist
	bad_screen=@TRUE
else
	if winstate(screen_name)==-1 then  ;if it's hidden, it's no good
		bad_screen=@TRUE
	endif
	if  !winisdos(screen_name) then ;just in case it doesn't pick up a GUI window by mistake
		bad_screen=@TRUE
	endif
endif

if bad_screen then
		message("Idiot!","It might help to have a console to work with, you know!")
		return
endif


screen_name=winidget(screen_name) ;gets a pseudo window handle, in case the name of the window changes.
x=winstate(screen_name)
if winstate(screen_name)<2 then winzoom(screen_name)


x=winposition(screen_name)


;calculate the character size.

scr_height=itemextract(4,x,",") - 31 -   itemextract(2,x,",")
scr_width= itemextract(3,x,",") - 12 -   itemextract(1,x,",")

row_const=16 + itemextract(2,x,",")
col_const=9 +  itemextract(1,x,",")

char_height=1.0*scr_height/max_row 
char_width=1.0*scr_width/max_col 

;need to check all of the variables

;default to the first 60 characters of row 23
if !isdefined(from_column) then from_column=1
if !isdefined(from_row) then from_row=23
if !isdefined(to_column) then to_column=60
if !isdefined(to_row) then to_row=23


;check the minimum/maximum bounds, change to defaults
if from_column > max_col then from_column=max_column
if to_column > max_col then to_column=max_column
if from_row > max_row then from_row=max_row
if to_row > max_row then to_row=max_row

if from_column <1 then from_column=1
if to_column <1 then to_column=1
if from_row <1 then from_row=1
if to_row <1 then to_row=1


;get number of row, number of columns

num_rows=abs(to_row-from_row+1)
num_col=abs(to_column-from_column)


;row virtual pixel addresses
start_row_address=int((from_row * char_height) + row_const) ;a formula!
end_row_address= int((to_row  * char_height )+ row_const)

;column virtual pixel addresses
start_col_address= int((from_column-1) * char_width +col_const) ;another formula!
end_col_address=  int((to_column-1) * char_width + col_const)


got=@FALSE
while !got
	if isdefined(times_executed) then times_executed=times_executed +1  ;for debugging
	WinActivate(screen_name) ;helpful.
	MousePlay("%start_col_address%  %start_row_address%", "", "", 0, 0) ;moves the mouse to the start row/col
	MousePlay("%end_col_address%  %end_row_address%", "", "", @MPLAYLBUTTON, base_delay) ;drags the mouse to the end row/col
	timedelay(base_delay * 2)
	this_pos=mouseinfo(2)    
	this_col=itemextract(1,this_pos," ")
	this_row=itemextract(2,this_pos," ")
	
	if abs(this_row-end_row_address)<6 then got=@TRUE			  ;checks for reasonableness of mouse position
	if abs(this_col-end_col_address)<6 then got=@TRUE
	if strsub(wingetactive(),1,6)!="Select" then got=@FALSE 	  ;check to see if console is draggable
	if isdefined(reps) then 	  ;a counter to tell the caller how many times it failed internally. For debugging.
		if !got then reps=reps+1  
	endif
endwhile

sendkeysto(screen_name,"~") ;sends an Enter to the screen. This copies the selected test to the clipboard.

get_screen=clipget() ;get the clipboard text 
get_screen=strreplace(get_screen,@CRLF,@CR) ;give it a delimiter that itemextract() can use



%console_text_grabber%=strtrim(get_screen)	  ;populates [return variable] 
return		;pops back to the calling program.


Test Grab.WBT

;test the screen grabber
numreps=80
reps=0

screen_name={insert screen name here}
start_time=timeymdhms()
times_executed=0



for i=1 to numreps
	call("_console_text_grabber.wbt","givemeit 1 1 25 80")
next



endtime=timeymdhms()
time_taken=abs(timediffsecs(start_time, endtime))

message("%numreps% Repetitions",strcat("Reps: ",reps,@CRLF,"Numex: ",times_executed,@CRLF,"Time: ",time_taken,@CRLF,"Per: ",1.000* time_taken/numreps))
exit


Other Alternatives:

  1. If the program has a "Edit Select ALL" Edit Copy" menu item where you can select the whole screen and copy it to the clipboard periodically, then you can get the clipbord contents with a ClipGet() and examine that every minute or so.

  2. There is a screen reader prorgram called Kleptomania where you can capture the contents of the screen, then OCR them back to text. For the really stubbron cases.

    From http://www.structurise.com


Article ID:   W14840
File Created: 2003:01:09:11:55:08
Last Updated: 2003:01:09:11:55:08