How to List Lines to a File?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ZhuangQu
Posts: 15
Joined: 06 Feb 2022, 20:54

How to List Lines to a File?

Post by ZhuangQu » 08 Feb 2023, 22:07

Although we have `ListLines` function, we can use it only when script is running. When script crashing or looping endlessly, I need `ListLines` to debug can't access it. Can I `ListLines` to a log file?

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to List Lines to a File?

Post by Rohwedder » 09 Feb 2023, 01:57

Hallo,
try:

Code: Select all

#InstallKeybdHook
Loop
{
	SoundBeep, 4000, 20
	Sleep, 1000
}
Return
q:: ; `ListLines` to Log.txt
ListLines, Off
FileAppend,% ScriptInfo("ListLines"), Log.txt
Return

ScriptInfo(Command)
{ ;lexikos: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=9656
; Returns the text that would have been shown in AutoHotkey's main window
; if you had called Command, but without actually showing or activating the window.
; Command must be "ListLines", "ListVars", "ListHotkeys" or "KeyHistory".
    static hEdit := 0, pfn, bkp
    if !hEdit {
        hEdit := DllCall("GetWindow", "ptr", A_ScriptHwnd, "uint", 5, "ptr")
        user32 := DllCall("GetModuleHandle", "str", "user32.dll", "ptr")
        pfn := [], bkp := []
        for i, fn in ["SetForegroundWindow", "ShowWindow"] {
            pfn[i] := DllCall("GetProcAddress", "ptr", user32, "astr", fn, "ptr")
            DllCall("VirtualProtect", "ptr", pfn[i], "ptr", 8, "uint", 0x40, "uint*", 0)
            bkp[i] := NumGet(pfn[i], 0, "int64")
        }
    } 
    if (A_PtrSize=8) {  ; Disable SetForegroundWindow and ShowWindow.
        NumPut(0x0000C300000001B8, pfn[1], 0, "int64")  ; return TRUE
        NumPut(0x0000C300000001B8, pfn[2], 0, "int64")  ; return TRUE
    } else {
        NumPut(0x0004C200000001B8, pfn[1], 0, "int64")  ; return TRUE
        NumPut(0x0008C200000001B8, pfn[2], 0, "int64")  ; return TRUE
    } 
    static cmds := {ListLines:65406, ListVars:65407, ListHotkeys:65408, KeyHistory:65409}
    cmds[Command] ? DllCall("SendMessage", "ptr", A_ScriptHwnd, "uint", 0x111, "ptr", cmds[Command], "ptr", 0) : 0 
    NumPut(bkp[1], pfn[1], 0, "int64")  ; Enable SetForegroundWindow.
    NumPut(bkp[2], pfn[2], 0, "int64")  ; Enable ShowWindow. 
    ControlGetText, text,, ahk_id %hEdit%
    return text
}

Post Reply

Return to “Ask for Help (v1)”