2013-05-17 35 views
1

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

更新:我知道我可以在这两个脚本读取/写入一个临时文件之间传输数据,但我宁愿不同的东西......

我不知道是否Perl代码是有关我的问题,但启动AHK的行是:

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

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

+0

你能更具体?从中尝试检索文本的窗口的确切类型是什么?你怎么打开这个窗口呢?你还可以提供相关的Perl代码吗? – MCL

+0

看来,你希望你的AHK脚本写入标准输出。这可以通过[FileAppend](http://www.autohotkey.com/docs/commands/FileAppend.htm)来实现。例如:'FileAppend,这是对stdout的一个测试,*'因为我对Perl不太了解,所以我不知道是否或如何捕获一个进程的stdout。另外,一些ERP系统提供了将内容输出到剪贴板的功能。具体来说,SAP在某些情况下可以做到这一点。如果您已经使用窗口间谍分析了ERP窗口,我认为您没有太多可以做的事情。 – MCL

+0

FileAppend是1/2解决方案!值得一提的是,我的Perl代码中的变量'$ data'接收所有的stdout。不是SAP,在从ERP系统导出数据时没有这样的运气。目前,我有一个解决方案使用[OCR插件](http://www.autohotkey.com/forum/viewtopic.php?t=74227) – shaun5

回答

1

shaun5

如何捕捉不可选择的文本(如在命令 提示符窗口的内容,但我没有像在命令提示符编辑=>标记选项 ) ...

我有几个例子:


检索多行:

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