Error: This variable has not been assigned a value. Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Error: This variable has not been assigned a value.

Post by kunkel321 » 05 Feb 2023, 14:22

Hi Folks, Newbie on v2 here... I'm trying to update the AutoCorrect.ahk script for v2. RaptorX's converter did a lot of the work for me. I had to tweak the InputBox, but I think I got that working. The original code by Jim B has a loop that puts the carret at a certain location of the InputBox. (bottom of below code). The default value of the InputBox is taken from A_Clipboard. When AHK2 first runs the code, though, that value is not yet known by AHK2. (At least I think that's what's happening).

The error:
Spoiler
Any ideas how to solve this?

Spoiler
Thanks.
ste(phen|ve) kunkel

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Error: This variable has not been assigned a value.

Post by mikeyww » 05 Feb 2023, 15:35

Since your variable has not been assigned a value, assign it to a value. If it's not a global variable, then it's local to the function, and you can define the variable within a function. Accessing any undefined variable in v2 is an error.

Read here: https://www.autohotkey.com/docs/v2/Concepts.htm#variables
Uninitialized Variables: To initialize a variable is to assign it a starting value. A variable which has not yet been assigned a value is uninitialized (or unset for short). Attempting to read an uninitialized variable is considered an error. This helps to detect errors such as mispelled names and forgotten assignments.
I imagine that you want to decide whether your variable "holds" a string or an object such as an InputBox. It can't do both at the same time.

V2 eliminated ErrorLevels. See the doc for :arrow: InputBox. That will make your life a lot easier as you fix the script! Example

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Error: This variable has not been assigned a value.  Topic is solved

Post by neogna2 » 05 Feb 2023, 15:50

When you set variable NewHotStr inside hotkey !#h::'s function it defaults to a local variable. Which means it is not readable in your other function MoveCaret().

One solution is to declare it global
!#h::
{
global NewHotStr


If you (not in this case but other cases) want to not only read that global variable but also change it in another function you'll have to declare it global in that other function too.

Dealing more with local/global variables is one of the things that can take some time to get used to when switching from v1 to v2.

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Error: This variable has not been assigned a value.

Post by mikeyww » 05 Feb 2023, 18:15

Here is some stuff on scope in case it helps. viewtopic.php?p=501239#p501239

User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Error: This variable has not been assigned a value.

Post by kunkel321 » 06 Feb 2023, 13:14

Thanks for the replies guys.

Adding global NewHotStr after the hotkey and first brace seemed to work.

For experimental purposes, I tried defining the variable (NewHotStr := "") in that location, but that didn't work.

Thanks for the references Mike, I am exploring those too.

EDIT: Interestingly, it was apparently also an error for me to have HotString.Value in the loop. It needs to be:

Code: Select all

Loop StrLen(NewHotStr) + 4 
    SendInput("{Right}")
However the .Value does need to appear here:

Code: Select all

FileAppend("`n" NewHotStr.Value, A_ScriptFullPath) 
ste(phen|ve) kunkel

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Error: This variable has not been assigned a value.

Post by mikeyww » 06 Feb 2023, 13:34

As I mentioned already, some of the confusion may be that you are using a single variable name for both a string and an InputBox object.

User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Error: This variable has not been assigned a value.

Post by kunkel321 » 06 Feb 2023, 15:50

mikeyww wrote:
06 Feb 2023, 13:34
As I mentioned already, some of the confusion may be that you are using a single variable name for both a string and an InputBox object.
Indeed! I noticed the difference when I tried to make the Cancel button do something. 8-)
ste(phen|ve) kunkel

User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Error: This variable has not been assigned a value.

Post by kunkel321 » 06 Feb 2023, 16:08

Hey if either of you guys have Win 10 running, can you please try this version...
I have here, "AutoCorrect2.ahk" :clap:

That part with the !#h InputBox keeps activating some Web Office 365 portal thing though :crazy: It doesn't happen if I select text in Word and activate it, but it does happen in Notepad.

Note that I've changed the MoveCaret function. I made it so that the appreviation text gets selected in the InputBox, rather than placing the cursor at the beginning of the replacement text. If you comment/uncomment, so that the original version is used, then the Office thing doesn't get launched. I guess it has something to do with the Shift down and Shift up ?? Merely simulating those keypresses doesn't seem to do anything though...

Thoughts?
Spoiler


UPDATE: Experimenting... This code also launches the Office 365 thing... Can't figure out why though... I think it might have something to do with my hotkey. If I get the Win out of there, it doesn't happen.

Code: Select all

; AHK v2!
#SingleInstance
!#h::
{
Send("{HOME}")
Send("{Right}{Right}{Shift down}")
Send("{Shift up}")
;MsgBox("hello")
return
}
EDIT: The hotkey has to have ! and #. Anything else combined with h doesn't have the effect. Also though, the Send commands have to be there.... If only the MsgBox("hello") is between the braces, then the effect also doesn't happen.

Below is a screenshot of the thing that gets launched. It appears to be a portal to Office 365, though it is not a webbrowser window.
Spoiler

EDIT: I found the app. It lives in my Start menu. I can't tell if it has a Windows hotkey attached to it though... It's of no consequence anyway... I'll just use #h like the original AutoCorrect script does. Problem solved. :HeHe:
ste(phen|ve) kunkel

User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Error: This variable has not been assigned a value.

Post by kunkel321 » 08 Feb 2023, 11:57

Oi. After all that effort, I noticed that "Hotstring helper" had already been updated for v2 and is posted here: https://www.autohotkey.com/docs/v2/lib/Hotstring.htm#ExHelper
ste(phen|ve) kunkel

User avatar
PENchanter
Posts: 10
Joined: 06 Sep 2021, 16:13

Re: Error: This variable has not been assigned a value.

Post by PENchanter » 27 Feb 2023, 13:45

Hi everyone! I, too, had gotten this error pop-up trying to execute an "If (var)" when the "var" had NOT been previously assigned a value (in this case, an empty string). The simple fix, as already been explained, is to set " var := "" " before the 'test' for the var containing anything. This way I do not even need to test within a loop whether or not the "var" had already been assigned any value before beginning to fill it with 'strings'. Happy scripting!!

SpeedRunner33333
Posts: 1
Joined: 12 Oct 2023, 08:02

Re: Error: This variable has not been assigned a value.

Post by SpeedRunner33333 » 30 Oct 2023, 10:10

Experimenting... This code also launches the Office 365 thing... Can't figure out why though... I think it might have something to do with my hotkey. If I get the Win out of there, it doesn't happen.
On Windows, pressing Ctrl+Shift+Win+Alt will open Microsoft 365. Weird shortcut, but it does exist. :)

Post Reply

Return to “Ask for Help (v2)”