1

我从 Perl 脚本调用 AHK。如何从使用 AHK 的窗口捕获不可选择的文本(如命令提示符窗口中的文本,但我没有命令提示符中的 Edit=>Mark 选项之类的内容)并将值返回给 Perl 脚本?

更新:我意识到我可以在读取/写入临时文件的两个脚本之间传输数据,但我更喜欢不同的东西......

我不确定 Perl 代码是否与我的问题相关,但启动 AHK 的行是:

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

“窗口”是我无法直接查询的企业 ERP 系统。显示“窗口”中的信息,但不可选择。

4

1 回答 1

1

肖恩5

如何捕获无法选择的文本(例如命令提示符窗口中的文本,但我没有命令提示符中的 Edit=>Mark 选项)...

我有几个例子:


检索多行:

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

检索指定的行:

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
于 2013-05-19T10:50:30.260 回答