#include OCR.ahk
result := OCR.FromDesktop()
MsgBox "All text from desktop: `n" result.Text
MsgBox "Press OK to highlight all found lines for 3 seconds."
for line in result.Lines
result.Highlight(line, -3000)
ExitApp
예제 2 메모장에서 일부 텍스트를 찾아 마우스로 클릭&드래그하여 선택합니다.
#include OCR.ahk
Run "notepad.exe"
WinWaitActive "ahk_exe notepad.exe"
Send "Lorem ipsum "
Sleep 40
result := OCR.FromWindow("A",,2)
try found := result.FindString("Lorem")
if !IsSet(found) {
MsgBox '"Lorem" was not found in Notepad!'
ExitApp
}
result.Highlight(found)
CoordMode "Mouse", "Window"
MouseClickDrag("Left", found.x, found.y, found.x + found.w, found.y + found.h)
#Requires AutoHotkey v2
#include OCR.ahk
CoordMode "Mouse", "Window"
Loop {
ib := InputBox("Insert search phrase to find from active window: ", "OCR")
Sleep 100 ; Small delay to wait for the InputBox to close
if ib.Result != "OK"
ExitApp
result := OCR.FromWindow("A",,2)
try found := result.FindString(ib.Value)
catch {
MsgBox 'Phrase "' ib.Value '" not found!'
continue
}
MouseMove found.x, found.y
result.Highlight(found)
break
}
예제 5 텍스트를 기다리는 방법, 결과 개체에서 키워드를 검색하는 방법, 결과를 클릭하는 방법을 보여줍니다
#Requires AutoHotkey v2
#include OCR.ahk
Run "https://www.w3schools.com/tags/att_input_type_checkbox.asp"
WinWaitActive "HTML input type",,10
if !WinActive("HTML input type") {
MsgBox "Failed to find test window!"
ExitApp
}
예제 6
검색 영역의 창을 특정 좌표로 제한하는 방법
result := CropResult(OCR.FromDesktop(), 0, 0, 500, 80)
for line in result.Lines
ToolTip(line.Text), result.Highlight(line)
CropResult(result, x, y, w, h) {
result := result.Clone()
croppedLines := [], croppedWords := [], text := ""
for line in result.Lines {
croppedWords := [], lineText := ""
for word in line.Words {
if word.x >= x && word.y >= y && (word.x+word.w) <= (x+w) && (word.y+word.h) <= (y+h)
croppedWords.Push(word), lineText .= word.Text " ", ObjAddRef(word.ptr)
}
if croppedWords.Length {
line := {Text:Trim(lineText), Words:croppedWords}
line.base.__Class := "OCR.OCRLine"
croppedLines.Push(line)
text .= lineText
}
}
result.DefineProp("Lines", {Value:croppedLines})
result.DefineProp("Text", {Value:Trim(text)})
result.DefineProp("Words", OCR.Prototype.GetOwnPropDesc("Words"))
return result
}