我从Perl脚本调用AHK。如何从使用AHK的窗口捕获不可选择的文本(如命令提示符窗口中的文本,但我没有像命令提示符中的Edit =>标记选项),并将值返回到Perl脚本?
更新:我意识到我可以在读取/写入临时文件的两个脚本之间传输数据,但我更喜欢不同的东西......
我不确定Perl代码是否与我的问题相关,但启动AHK的行是:
$data = `autohotkey.exe script.ahk data1 data2`;
'窗口'是我无法直接查询的企业ERP系统。显示“窗口”中的信息,但不能选择。
答案 0 :(得分:1)
shaun5
如何捕获不可选择的文本(如命令中的文本) 提示窗口,但我没有像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