Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Capturing the text in a Console Window

by slloyd (Hermit)
on Oct 13, 2005 at 13:06 UTC ( [id://499890]=perlquestion: print w/replies, xml ) Need Help??

slloyd has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to capture the text within a console window? Spy shows me that it has a class of type ConsoleWindowClass and gives me the caption but I cannot figure out how to capture the text within it (shows no children). Any ideas?

Replies are listed 'Best First'.
Re: Capturing the text in a Console Window
by BrowserUk (Patriarch) on Oct 13, 2005 at 14:30 UTC

    Assuming you have two Command windows running. One, currently idle (hence the regex '^command Prompt$'), that you wish to capture whatever is in it's buffer, and the other in which you will invoke this script (which will fail to match the regex because it will have the title text 'Command Prompt - yourname.pl' or similar).

    This will capture the contents of the idle sessions buffer and print on the console where you run this script. If you have multiple idle windows, then it will capture and dump them all. Adjust the regex to select the appropriate window.

    #! perl -slw use strict; use Win32::GuiTest qw[ FindWindowLike SendKeys SetForegroundWindow ]; use Win32::Clipboard; my $clip = Win32::Clipboard(); for my $w ( FindWindowLike( 0, '^Command Prompt$', 0, 0, 0 ) ) { SetForegroundWindow( $w ); SendKeys( '%{SPC}ES%{SPC}Ey' ); print $clip->Get(); }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

        It will/does capture content of existing (win32) console windows. It only requires you to specify a regex to match the titlebar of the existing console window and it will capture whatever is currently in the buffer (displayed and scrolled off if the buffer is larger than the screen).

        If this is not what you require, then perhaps you could clarify what you are after?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Capturing the text in a Console Window
by fizbin (Chaplain) on Oct 13, 2005 at 14:37 UTC
    It is unclear to me what you're trying to do - are you trying to capture text that's already sitting in the console window when your script starts, or are you trying to run a command from inside your perl script and then read what the command spit out to the screen? Note that the first of those two is much more difficult than the second.

    Also, if you're trying to capture text already sitting on the console, is your perl script attempting to read its own console, or someone else's? The first of those two tasks is much easier.

    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://499890]
Approved by castaway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-03-28 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found