user626963
user626963

Reputation:

Capture and return unselectable text

I'm calling AHK from a Perl script. How do I to capture unselectable text (like the text in a Command Prompt window, but I don't have something like the Edit=>Mark option in Command Prompt) from a window using AHK and return the value back to the Perl script?

Update: I realize I can transfer data between the two scripts reading/writing a temporary file, but I would prefer something different...

I am not sure if Perl code is relevant to my question, but the line that initiates AHK is:

 $data = `autohotkey.exe script.ahk data1 data2`;

The 'window' is a corporate ERP system that I cannot directly query. The information in the 'window' is displayed, but not selectable.

Upvotes: 1

Views: 699

Answers (1)

Grey
Grey

Reputation: 339

shaun5

How do I to capture unselectable text (like the text in a Command Prompt window, but I don't have something like the Edit=>Mark option in Command Prompt)...

I have a couple examples:


Retrieve multiple lines:

ptr:=A_PtrSize ? "Ptr":"UInt", suffix:=A_IsUnicode ? "W":"A", numReaded:=data:=""
INVALID_HANDLE_VALUE:=-1, STD_INPUT_HANDLE:=-10, STD_OUTPUT_HANDLE:=-11
VarSetCapacity(buffer, (size:=1030)*(A_IsUnicode ? 2:1))

Run, % "cmd.exe",,, procID
WinWaitActive, % "ahk_pid"procID
;~ Input, dummyVar, % "I", % "{vk20}" ; wait a space button to press
;~ WinGet, procID, PID, A
SendEvent, % "{Raw}TEST WRITE TO CONSOLE"
If !DllCall("AttachConsole", "UInt", procID, A_PtrSize ? "UInt":"")
{
   WinClose, % "ahk_pid"procID
   MsgBox, 262192, % A_LineNumber, % "Fail attach to console", % 2.5
   ExitApp
}
If (hConsole:=DllCall("GetStdHandle", "Int", STD_OUTPUT_HANDLE
                                    , A_PtrSize ? "Ptr":""))=INVALID_HANDLE_VALUE
{
   DllCall("FreeConsole")
   WinClose, % "ahk_pid"procID
   MsgBox, 262192, % A_LineNumber, % "Fail retrive a handle of console", % 2.5
   ExitApp
}
If !DllCall("ReadConsoleOutputCharacter"suffix, ptr, hConsole
                                              , ptr, &buffer
                                              , "UInt", size
                                              , "UInt", 0 ; begin read from first row
                                              , "UInt*", numReaded
                                              , A_PtrSize ? "UInt":"")
{
   DllCall("FreeConsole")
   WinClose, % "ahk_pid"procID
   MsgBox, 262192, % A_LineNumber, % "Fail get data from console", % 2.5
   ExitApp
}
; line width is 320 pixels (property/layout/screen buffer size),
; here I cut the unnecessary white spaces in each row
Loop, % numReaded
   Mod(A_Index, 320) ? data.=Chr(NumGet(buffer, (A_Index-1)*(A_IsUnicode ? 2:1)
                                              , A_IsUnicode ? "UShort":"UChar"))
                . "" : data:=RTrim(data)"`r"
MsgBox, 262208, % A_LineNumber, % RTrim(data) ;, % 2.5
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID

Retrieve specified line:

ptr:=A_PtrSize ? "Ptr":"UInt", suffix:=A_IsUnicode ? "W":"A", numReaded:=""
INVALID_HANDLE_VALUE:=-1, STD_INPUT_HANDLE:=-10, STD_OUTPUT_HANDLE:=-11
VarSetCapacity(buffer, (size:=319)*(A_IsUnicode ? 2:1))

Run, % "cmd.exe",,, procID
WinWaitActive, % "ahk_pid"procID
;~ Input, dummyVar, % "I", % "{vk20}" ; wait a space button to press
;~ WinGet, procID, PID, A
SendEvent, % "{Raw}TEST WRITE TO CONSOLE"
If !DllCall("AttachConsole", "UInt", procID, A_PtrSize ? "UInt":"")
{
   WinClose, % "ahk_pid"procID
   MsgBox, 262192, % A_LineNumber, % "Fail attach to console", % 2.5
   ExitApp
}
If (hConsole:=DllCall("GetStdHandle", "Int", STD_OUTPUT_HANDLE
                                    , A_PtrSize ? "Ptr":""))=INVALID_HANDLE_VALUE
{
   DllCall("FreeConsole")
   WinClose, % "ahk_pid"procID
   MsgBox, 262192, % A_LineNumber, % "Fail retrive a handle of console", % 2.5
   ExitApp
}
If !DllCall("ReadConsoleOutputCharacter"suffix, ptr, hConsole
                                              , ptr, &buffer
                                              , "UInt", size
                                              , "UInt", 3<<16 ; skip some rows
                                              , "UInt*", numReaded
                                              , A_PtrSize ? "UInt":"")
{
   DllCall("FreeConsole")
   WinClose, % "ahk_pid"procID
   MsgBox, 262192, % A_LineNumber, % "Fail get data from console", % 2.5
   ExitApp
}
; cut the unnecessary white spaces and show
MsgBox, 262208, % A_LineNumber, % RTrim(StrGet(&buffer, numReaded
                                                      , A_IsUnicode ? "UTF-16":"CP0"))
DllCall("FreeConsole")
WinClose, % "ahk_pid"procID

Upvotes: 1

Related Questions