This variable appears to never be assigned a value.

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
CPager
Posts: 10
Joined: 13 Apr 2022, 15:45

This variable appears to never be assigned a value.

26 Apr 2023, 09:54

I am trying to make a Gui containing 3 Tab like this:
Tab := MyGui.Add("Tab3","Choose1", ["AHK Desktop Folder","AHK Documents Folder","Python Folder"])
Each of these Tabs would correspond to a corresponding "Loop Files".
By choosing one of these Tabs, I could thus choose one of the files contained in these Folders.
Thereafter, I could "Double Click" or "Click OK" to open the chosen file.

In the current state of the script, only the 1st Tab displays the result of "Loop Files".
The following two Tabs contain absolutely NOTHING, and additionally I get this warning:
Warning: This variable appears to never be assigned a value.
Specifically: local LB

039: MyGui.Show()
042: {
▶ 043: if MsgBox("Would you like to launch the file or document below?`n`n" LB.Text,, 4) = "No"
044: Return
046: Try

I've tried various ways to fix this warning, but I still can't fix it.
Your help would be greatly appreciated. Thanks in advance.

Code: Select all

#Requires Autohotkey v2
#SingleInstance Force

MyGui := Gui()
RetrievedTitle := MyGui.Title
MyGui.SetFont("s12")  ; Set a font size (11-point).
Tab := MyGui.Add("Tab3","Choose1", ["AHK Desktop Folder","AHK Documents Folder","Python Folder"])
MyGui.Add("Text",, "Pick a file to launch from the list below.")
Tab.UseTab(1)
LV := MyGui.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
        LV.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
        LV.OnEvent("DoubleClick", LaunchFile)
        Loop Files, "C:\Users\UserName\Desktop\AutoHotkey\*.ahk"  ; Change this folder and wildcard pattern to suit your preferences.
            LV.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)
        ;MyGui.Add("Button", "Default", "Launch File").OnEvent("Click", LaunchFile)
Tab.UseTab(2)
LV := MyGui.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
        LV.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
        LV.OnEvent("DoubleClick", LaunchFile)
        Loop Files, "C:\Users\UserName\Documents\AutoHotkey\*.ahk"
            LV.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)
        ;MyGui.Add("Button", "Default", "Launch File").OnEvent("Click", LaunchFile)
Tab.UseTab(3)
LV := MyGui.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
        LV.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
        LV.OnEvent("DoubleClick", LaunchFile)
        Loop Files, "C:\Users\UserName\Desktop\Script Python\*.py"
            LV.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)
        ;MyGui.Add("Button", "Default", "Launch File").OnEvent("Click", LaunchFile)
MyGui.Show()

LaunchFile(*)
{
    if MsgBox("Would you like to launch the file or document below?`n`n" LB.Text,, 4) = "No"
        return
    ; Otherwise, try to launch it:
    try Run(LB.Text)
    if A_LastError
        MsgBox("Could not launch the specified file. Perhaps it is not associated with anything.")
}
User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: This variable appears to never be assigned a value.

26 Apr 2023, 13:39

The error message points you to the problem. Have you defined LB somewhere?

Getting text: https://www.autohotkey.com/docs/v2/lib/ListView.htm#GetText

Code: Select all

#Requires AutoHotkey v2.0
gui1 := Gui(, 'Tabs'), gui1.SetFont('s10')
Tab  := gui1.AddTab3(, ['A', 'B'])

; LV1
LV1 := gui1.AddListView(, [1, 2])
LV1.Add(, 3, 4)

; LV2
Tab.UseTab(2)
LV2 := gui1.AddListView(, [5, 6])
LV2.Add(, 7, 8)

gui1.Show

; Events
LV1.OnEvent('DoubleClick', LV_DoubleClick)
LV2.OnEvent('DoubleClick', LV_DoubleClick)

LV_DoubleClick(LV, row) {
 txt := LV.GetText(row, COL := 2)
 MsgBox 'On tab ' Tab.Text ', you double-clicked row number ' row '.`n`nText: ' txt
}
CPager
Posts: 10
Joined: 13 Apr 2022, 15:45

Re: This variable appears to never be assigned a value.

26 Apr 2023, 14:41

Thank you for that answer.
It was indeed my mistake, since at the beginning of the script I was using a "ListView" (LV),
But that in lines 43 and 46 I was calling a "ListBox" (LB) before RunFile.
A copy/paste error, since I am trying to join two scripts to form a single one.

Now with the first tab ("AHK Desktop Folder"), I can see the result of the "Loop Files" in the "ListView".
With a "DoubleClick", I manage to launch the chosen script, which is the goal sought with this Gui, a kind of "Quick Launcher Tool".

On the other hand, the two other Tabs remains empty, despite the fact that I "Click" on them.
How to link the 2nd Tab and the 3rd Tab to the "Loop Files" associated with it?

I also changed the entire "LaunchFile" section to:

Code: Select all

RunFile(LV, RowNumber)
{
     FileName:=LV.GetText(RowNumber, 1); Get the text of the first field.
     FileDir:=LV.GetText(RowNumber, 2); Get the text of the second field.
     try
         Run(FileDir "\" FileName)
     catch
         MsgBox("Could not open "FileDir"\"FileName".")
     MyGui.Destroy()
}
User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: This variable appears to never be assigned a value.

26 Apr 2023, 15:00

Please post your complete revision in a new reply below.
CPager
Posts: 10
Joined: 13 Apr 2022, 15:45

Re: This variable appears to never be assigned a value.

26 Apr 2023, 15:41

When I wrote my 2nd question, I had not yet seen your answer posted at 2:39 p.m.
I am currently modifying my script with the recommendations you sent me.
I'm looking to learn how to do this, and your help is much appreciated.
At this point, the Gui opens with an empty ListView, but when I Click on the 2nd or 3rd tab, then go back to the 1st tab, I quickly see (like a flash) the result of the first Loop .
But here is the script where I am now:

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force

Gui1 := Gui()
RetrievedTitle := Gui1.Title
Gui1.SetFont("s12")  ; Set a font size (12-point).
Tab  := gui1.AddTab3(, ["AHK Desktop Folder","AHK Documents Folder","Python Folder"])
Gui1.Add("Text",, "Pick a file to launch from the list below.")

; LV1
LV1 := Gui1.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV1.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV1.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV1.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV1.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\UserName\Desktop\AutoHotkey\*.ahk"  ; Change UserName
        LV1.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

; LV2
LV2 := Gui1.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV2.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV2.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV2.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV2.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\UserName\Documents\AutoHotkey\*.ahk"  ; Change UserName
        LV2.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

; LV3
LV3 := Gui1.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV3.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV3.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV3.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV3.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\UserName\Desktop\Script Python\*.py"  ; Change UserName
        LV3.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

gui1.Show

; Events
LV1.OnEvent('DoubleClick', LV_DoubleClick)
LV2.OnEvent('DoubleClick', LV_DoubleClick)
LV3.OnEvent('DoubleClick', LV_DoubleClick)

LV_DoubleClick(LV, row) {
 txt := LV.GetText(row, COL := 2)
 MsgBox 'On tab ' Tab.Text ', you double-clicked row number ' row '.`n`nText: ' txt
}
CPager
Posts: 10
Joined: 13 Apr 2022, 15:45

Re: This variable appears to never be assigned a value.

26 Apr 2023, 16:37

mikeyww wrote:
26 Apr 2023, 15:00
Please post your complete revision in a new reply below.
I succeeded. My Gui works as I wanted.
Thank you for your help.
Voici le code final:

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force

Gui1 := Gui()
RetrievedTitle := Gui1.Title
Gui1.SetFont("s12")  ; Set a font size (12-point).
Gui1.Add("Text",, "Pick a file to launch from the list below.")
Tab  := gui1.AddTab3(, ["*   AHK Desktop Folder   *","*   AHK Documents Folder   *","*   Python Folder   *"])

; LV1
Tab.UseTab(1)
LV1 := Gui1.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV1.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV1.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV1.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV1.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\UserName\Desktop\AutoHotkey\*.ahk"  ; Change UserName
        LV1.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

; LV2
Tab.UseTab(2)
LV2 := Gui1.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV2.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV2.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV2.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV2.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\UserName\Documents\AutoHotkey\Lib\*.ahk"  ; Change UserName
        LV2.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

; LV3
Tab.UseTab(3)
LV3 := Gui1.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
		LV3.ModifyCol(1, 280)  ; Auto-size each column to fit its contents.
        LV3.ModifyCol(2, 270)  ; Auto-size each column to fit its contents.
        LV3.ModifyCol(3, 70)  ; Make the Size column at little wider to reveal its header.
        LV3.ModifyCol(4, 50,)  ; Make the Size column at little wider to reveal its header.
			Loop Files, "C:\Users\UserName\Desktop\Script Python\*.py"  ; Change UserName
        LV3.Add(, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, A_LoopFileExt)

gui1.Show

; Events
LV1.OnEvent('DoubleClick', RunFile)
LV2.OnEvent('DoubleClick', RunFile)
LV3.OnEvent('DoubleClick', RunFile)

RunFile(LV, RowNumber)
{
    FileName := LV.GetText(RowNumber, 1) ; Get the text of the first field.
    FileDir := LV.GetText(RowNumber, 2)  ; Get the text of the second field.
    try
        Run(FileDir "\" FileName)
    catch
        MsgBox("Could not open " FileDir "\" FileName ".")
    WinMinimize()
}
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: This variable appears to never be assigned a value.

05 May 2023, 18:17

mikeyww wrote:
26 Apr 2023, 13:39
The error message points you to the problem.
Thank you @mikeyww for this simple point; I was about to post a thread about this error message, but upon reading this, I realised that the error message also is telling you that the "variable" may not even exist.

My issue:

I had a #Include filepath.

I tried to call a function from within that file, but the error message appeared.

I took a look at the the filepath again and realised that I had added the function to a file with the same name but in a different location, so it was telling me the function didn't exist.

Such a simple oversight.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: bob65536, Draken, FanaticGuru and 34 guests