AutoHotkey Community

It is currently May 26th, 2012, 11:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Grab text from console
PostPosted: October 31st, 2009, 4:44 pm 
Hi,

i want to grab all the text from my CMD.exe console, i tried wingettext but it displayed nothing. What else can i do to grab text from cmd?

thanks


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 4:47 pm 
Screen Capture in Command Console


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 5:30 pm 
Hey thanks i got it now but another problem occur. I successfully grab all the text and wrote it to an .ini file. I check and its all there.

Code:
iniwrite, %Text%, cmd.ini, section1, key1


The text are all there, but when i tried to search for a particular word "Control" which is in the text, i can't get it. Any help?

Code:
iniread, Textfound, cmd.ini, section1, key1
IfInString, Control, %Textfound%
{
    MsgBox, The string was found.
    return
}
else
    Sleep, 1
return


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 5:40 pm 
Code:
iniread, Textfound, cmd.ini, section1, key1
IfInString, Textfound, Control
{
    MsgBox, The string was found.
    return
}
else
    Sleep, 1
return


You swapped the order IfInString expects

Quote:
IfInString, var, SearchString


HTH


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 6:25 pm 
Argh, sorry i just found out that it only work for one time like a hotkey, if you put it on a timer, it will return AttachConsole failed - error 5. It wont run for more than once?

Code:

f1::
settimer, attach, on
return

Attach:
WinGet, pid, PID, ahk_class ConsoleWindowClass
; AttachConsole accepts a process ID.
if !DllCall("AttachConsole","uint",pid)
{
    MsgBox AttachConsole failed - error %A_LastError%.
    ExitApp
}
; If it succeeded, console functions now operate on the target console window.
; Use CreateFile to retrieve a handle to the active console screen buffer.
hConOut:=DllCall("CreateFile","str","CONOUT$","uint",0xC0000000
                    ,"uint",7,"uint",0,"uint",3,"uint",0,"uint",0)
if hConOut = -1 ; INVALID_HANDLE_VALUE
{
    MsgBox CreateFile failed - error %A_LastError%.
    ExitApp
}
; Allocate memory for a CONSOLE_SCREEN_BUFFER_INFO structure.
VarSetCapacity(info, 24, 0)
; Get info about the active console screen buffer.
if !DllCall("GetConsoleScreenBufferInfo","uint",hConOut,"uint",&info)
{
    MsgBox GetConsoleScreenBufferInfo failed - error %A_LastError%.
    ExitApp
}
; Determine which section of the buffer is on display.
ConWinLeft := NumGet(info, 10, "Short")     ; info.srWindow.Left
ConWinTop := NumGet(info, 12, "Short")      ; info.srWindow.Top
ConWinRight := NumGet(info, 14, "Short")    ; info.srWindow.Right
ConWinBottom := NumGet(info, 16, "Short")   ; info.srWindow.Bottom
ConWinWidth := ConWinRight-ConWinLeft+1
ConWinHeight := ConWinBottom-ConWinTop+1
; Allocate memory to read into.
VarSetCapacity(text, ConWinWidth*ConWinHeight, 0)
; Read text.
if !DllCall("ReadConsoleOutputCharacter","uint",hConOut,"str",text
            ,"uint",ConWinWidth*ConWinHeight,"uint",0,"uint*",numCharsRead)
{
    MsgBox ReadConsoleOutputCharacter failed - error %A_LastError%.
    ExitApp
}
/* Alternate, slower method:
    ; Allocate memory to read into.
    VarSetCapacity(buf, ConWinWidth*ConWinHeight*4, 0)
    ; Read an array of CHAR_INFO structures, containing text and attributes.
    ; Note: &info+10 is the address of a SMALL_RECT containing the coords we
    ; wish to read from. On success, it will receive the actual coords used.
    if !DllCall("ReadConsoleOutput","uint",hConOut,"uint",&buf
                    ,"uint",ConWinWidth|ConWinHeight<<16,"uint",0
                    ,"uint",&info+10)
    {
        MsgBox ReadConsoleOutput failed - error %A_LastError%.
        ExitApp
    }
    ; buf should now contain an array of CHAR_INFO structures.
    ; We must decode this to retrieve readable text.
    VarSetCapacity(text, ConWinWidth*ConWinHeight)
    Loop % ConWinWidth*ConWinHeight
        text .= Chr(NumGet(buf, 4*(A_Index-1), "Char"))
*/
; Optional: insert line breaks every %ConWinWidth% characters.
text := RegExReplace(text, "`a).{" ConWinWidth "}(?=.)", "$0`n")
; Finally, display the text.
MsgBox % text
return
[/quote]


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 8:19 pm 
Maybe this posting will help you (my last one in the thread):

Clone console in/as AHK GUI issues

HTH


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 9:41 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
I use Skesoft's Text Capture Library together with the following script to capture text to clipboard from the command prompt window as well as from various other controls and windows:

Code:
;Requires Sean's COM Standard Library: http://www.autohotkey.com/forum/topic22923.html
;Requires Skesoft's Text Capture Library: http://www.skesoft.com/
;Hotkey: Win+T
;Action: Copies text to clipboard from window under mouse cursor

#t::
    COM_Init()
    tcServer := COM_CreateObject("TextCaptureLib.TextCapture")
    tcWindow := COM_CreateObject("TextCaptureLib.Window")
    COM_Invoke(tcWindow,"WindowFromCurrentPos")
    ;Clipboard := COM_Invoke(tcServer, "GetText", "+" tcWindow)
    ;RegEx below removes empty spaces\lines at the end
    Clipboard := RegExReplace(COM_Invoke(tcServer, "GetText", "+" tcWindow),"[\s\n\r]+$")
    COM_Release(tcServer)
    COM_Release(tcWindow)
    COM_Term()
Return


Skesoft's Text Capture Library has a 30-day trial period

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: hyper_, JSLover, Kirtman, Leef_me, Maestr0, Miguel, XstatyK, Yahoo [Bot] and 58 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group