Outputting ListVars to another variable or txt file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Outputting ListVars to another variable or txt file

17 May 2021, 18:29

Hi,
I need to do some debugging in a backup script I have here; it has two functionalities: copies files to the backup location and "FileAppends" the result — success or failure — to a log file.

The backup part is working, I checked the files, but the logging isn't; it logs only some of the backed-up items, even if they're all properly backed-up.

Anyway, I said this was about debugging, right? So I need to take advantage of the `ListVars` command. Thing is, the only thing I can have is displaying that dull window with difficult legibility, which has to be checked manually every step:
image.png
image.png (4.82 KiB) Viewed 401 times
Instead, I'd like to have the contents of that window be output to another variable, or directly onto a .txt/.log file so I can check the sequence afterwards. The test script I used was this:

Code: Select all

SetTitleMatchMode, 2
#SingleInstance force
#NoEnv

First_var := "I wanna see the 1st variable."
Second_Var := "I wanna see the 2nd variable"

Gui, Font, s16, Consolas
Gui, Add, Edit, x10 y10 w680 h330, %First_Var%`n%Second_Var%
Gui, Show, x130 y90 h350 w700, Variable contents:

ListVars


The values of the "first" and "second" variables are prettily displayed on the generated GUI, but I'm actually interested in the last window — the ListVars (first image), which will pop up after the GUI, but almost at the same time.

So, how can I go about it? :)
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Re: Outputting ListVars to another variable or txt file

18 May 2021, 02:30

mikeyww wrote:
17 May 2021, 19:00
I'm not an expert in this, but I saw the following, which might help you.

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=9656&p=244893
Hmm, interesting, but no joy. I tried integrating Lexikos' code like this:

Code: Select all

SetTitleMatchMode, 2
#SingleInstance force
#NoEnv
#InstallKeybdHook

ScriptInfo(ListVars)
{
    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
}

First_var := "I wanna see the 1st variable."
MsgBox % ScriptInfo("ListVars")
Second_Var := "I wanna see the 2nd variable."
MsgBox % ScriptInfo("ListVars")


; ListVars


...but I couldn't go much further. I expected to see the values of those first and second variables upon each MsgBox, but they came up blank.
Also I'm a total alien to DllCall()'s and COM Objects. I don't know how to fiddle with dlls, Windows API and stuff :(
just me
Posts: 9451
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Outputting ListVars to another variable or txt file

18 May 2021, 03:37

It must be

Code: Select all

ScriptInfo(Command)
{
...
}
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Re: Outputting ListVars to another variable or txt file

18 May 2021, 15:05

just me wrote:
18 May 2021, 03:37
It must be

Code: Select all

ScriptInfo(Command)
{
...
}
You got it! Almost there! I changed to the word "Command" and it worked with the MsgBoxes. However, I was unable to write it to a temporary log:

Code: Select all

SetTitleMatchMode, 2
#SingleInstance force
#NoEnv
#InstallKeybdHook

ScriptInfo(Command)
{
    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 := {ListVars:65407}
    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
}

First_var := "I wanna see the 1st variable."
; MsgBox % ScriptInfo("ListVars")
FileAppend, `nScriptInfo("ListVars"),temp_log.txt, 

Second_Var := "I wanna see the 2nd variable."
; MsgBox % ScriptInfo("ListVars")
FileAppend, `nScriptInfo("ListVars"),temp_log.txt, 

; ListVars


Please refer to the `FileAppend` commands I inserted. The (undesired) output was:

Code: Select all

ScriptInfo("ListVars")
ScriptInfo("ListVars")


And, when I tried with percent signs, I got a illegal-character-in-variable error.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Outputting ListVars to another variable or txt file

18 May 2021, 15:11

Code: Select all

FileAppend, % ScriptInfo("ListVars") "`n", temp_log.txt
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Re: Outputting ListVars to another variable or txt file

19 May 2021, 01:03

gregster wrote:
18 May 2021, 15:11

Code: Select all

FileAppend, % ScriptInfo("ListVars") "`n", temp_log.txt
That's it! Now I managed to put everything together. I was hesitant because all your inputs were remarkably valuable, but I ended up marking mikeyww's post as the answer. :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: dipahk, Google [Bot], Nerafius, RandomBoy and 179 guests