Page 1 of 1

Reading text within an app

Posted: 13 Apr 2024, 13:11
by ElsePoem
Hello! A pretty new programmer trying to do something somewhat simple. I have been trying to get a script that can read text on screen within an app. Upon detection of that specific text I wanted it to pop up with a MsgBox letting me know that the specific text has been sent! I’ve tried everything i could but to no avail.
(I hope this code pastes correctly since i’m on mobile)
——————————————————————————————————————————

Code: Select all

Loop {                                                  ; Start Loop
    if (WinActive("ahk_exe *program*.exe"))               ; program is open
        {                                               
        WinGetText, text, ahk_exe program.exe           ; Get Text from program if open
        IfInString, text, "Warning!"                  ; Check if text is Warning!
        {                                           
            MsgBox "Warning DETECTED!"               ; If so, stop app
            ExitApp                                     ; Stops app
——————————————————————————————————————————
Please don't be afraid to tell me that this is complete gibberish if you know any help for the solution :lol:


[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]

[Mod action: Moved topic to the v1 section since this is v1 code. The main section is for v2.]

Re: Reading text within an app

Posted: 13 Apr 2024, 17:06
by mikeyww
Welcome to this AutoHotkey forum!

Perhaps:

Code: Select all

#Requires AutoHotkey v1.1.33.11
#SingleInstance
winTitle := "ahk_exe Notepad.exe"
find     := "Warning!"
Process Priority,, B
Loop {
 WinWait % winTitle
 SetTimer Go, 200
 SoundBeep 1500
 WinWaitClose
 SetTimer Go, Off
 SoundBeep 1000
}
Go:
WinGetText txt, % winTitle
If InStr(txt, find) {
 MsgBox 48, Warning, Warning detected!
 ExitApp
}
Return
If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.

Re: Reading text within an app

Posted: 13 Apr 2024, 17:46
by ElsePoem
Thank you!! it works perfectly! However I think now it seems to just be a problem with the api of the app I'm using or I'm not inputting the correct file name into my code, will check it out now! :D

Re: Reading text within an app

Posted: 13 Apr 2024, 18:17
by mikeyww
Window Spy will show whether WinGetText can retrieve the text.

Re: Reading text within an app

Posted: 13 Apr 2024, 18:45
by ElsePoem
I looked at what it was viewing and not as much as i was hoping for, I found “Chrome.ahk on github so maybe that can read more than what it can now

Re: Reading text within an app

Posted: 13 Apr 2024, 19:09
by mikeyww
Probably. I doubt that you would have much luck with WinGetText in a Web browser. UI Automation is available, with scripts posted on the forum.

Re: Reading text within an app

Posted: 13 Apr 2024, 19:12
by ElsePoem
Ill go search for some! thank you!