0

Here's what Window Spy looks like, after i manually hit Alt+n:

enter image description here

Here's my script.

#IfWinActive <Redacted title> ;Private info so don't want to post, but it works
^j::
Send, !n

WinGetActiveTitle, title
MsgBox, %title% ;Message Box shows expected title

WinGetText, createNewPrescriptionTitle, title
while (Instr(createNewPrescriptionTitle, "Create New Prescription") != 1)
{
    Sleep, 1000
    FileAppend, %createNewPrescriptionTitle% blah, log.txt ;The file contains " blah blah blah..."
    WinGetText, createNewPrescriptionTitle, title
}

MsgBox, "Huzzah"

I've tried without the title as well. I tried with SetTitleMatchMode with Slow and Fast (documentation says it defaults to Fast, which matches what's in Window Spy).

Update: Looks like the Window Spy was v2, but my script was v1. I realized this once I started looking at WindowSpy.ahk, and I tried to use WinGetTextFast() from it in my script, instead of WinGetText. I got an error saying WinGetControlsHwnd wasn't recognized. I looked up WinGetControlsHwnd in the AutoHotkey.chm I'd been using, and it wasn't there. So then I googled it, and lo and behold it was a thing. That's when I noticed the version numbers on the documentation in my browser was v2, while the documentation in AutoHotkey.chm was v1. Haven't gotten it working yet, but I think I'm on the path.

2 Answers 2

0

Version differences between my script and the Window Spy I was using was the issue. My script was running v1 and the Window Spy was v2.

Unfortunately, that means I just had to unlearn a bunch of stuff I learned about v1 and learn v2, but at least v2 makes more sense.

I didn't quite get what I wanted with, which is for WinGetText to work, but I used Window Spy's "fast" version of it that I copied from WindowSpy.ahk. So this worked:

#Requires AutoHotkey v2
#HotIf WinActive("<redacted title>") ;Private info so don't want to post

^j::
{
    Send "!n"

    title := WinGetTitle("A")

    createNewPrescriptionTitle := WinGetTextFast(true)
    ;createNewPrescriptionTitle := WinGetText() 'Resulted in "Error: Failed", no other explanation

    while (Instr(createNewPrescriptionTitle, "Create New Prescription") != 1)
    {
        Sleep 1000
        createNewPrescriptionTitle := WinGetTextFast(true)
    }

    MsgBox "Huzzah"

    ;Copied from C:\Program Files\AutoHotkey\UX\WindowSpy.ahk
    WinGetTextFast(detect_hidden) {    
        controls := WinGetControlsHwnd()
        
        static WINDOW_TEXT_SIZE := 32767 ; Defined in AutoHotkey source.
        
        buf := Buffer(WINDOW_TEXT_SIZE * 2,0)
        
        text := ""
        
        Loop controls.Length {
            hCtl := controls[A_Index]
            if !detect_hidden && !DllCall("IsWindowVisible", "ptr", hCtl)
                continue
            if !DllCall("GetWindowText", "ptr", hCtl, "Ptr", buf.ptr, "int", WINDOW_TEXT_SIZE)
                continue
            
            text .= StrGet(buf) "`r`n" ; text .= buf "`r`n"
        }
        return text
    }
}

I might fiddle around with WinGetText a little more to get it to work, but in the spirit of scripting, if I can't figure it out, I'll leave my quick and dirty, but working, solution. Hopefully, someone else comes along with a cleaner solution.

Sign up to request clarification or add additional context in comments.

Comments

0

The first script (v1) likely failed because of not using variable dereferencing properly. Namely while WinGetActiveTitle, title stores the active window title in a variable named title, WinGetText, createNewPrescriptionTitle, title will try to store the text from a window literally titled "title" to the variable createNewPrescriptionTitle. WinGetText, createNewPrescriptionTitle, %title% should work though. But my recommendation is not to use the deprecated v1, and instead use v2.

I'm unsure why the second one failed, it may be a bug in AHK. If you manage to reproduce it in a program which can be shared then do post a bug report at the forums.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.