Referencing variable used in If Else Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Referencing variable used in If Else

24 Jun 2021, 12:07

I have a script that checks the clipboard for a social security number. If found, it is set as the value for the variable SSN. If not, the else portion of the script displays a Gui to enter the SSN and then sets it as the value for the variable SSN.

Whether the value is found on the clipboard or has to be entered, how would you reference that variable outside the if else? I'm wanting to insert this value into a specific cell on a spreadsheet.

My current script:

Code: Select all

if (regexmatch(clipboard, "\d{9}"))
{
	SSN := clipboard
	MsgBox % SSN
}

else
{
	
	Menu, tray, Icon , search.ico, 1, 1
	gui, destroy
	Gui, +AlwaysOnTop
	Gui, Add, Text,, Enter SSN:
	Gui, Add, Edit, vSSN ym
	
	Gui, Add, Button, x65 y40 w35 h20 +default, OK
	Gui, Add, Button, x115 y40 w35 h20, Close
	Gui, Show,, SSN
	return
	
	GuiClose:
	ButtonClose:
	ExitApp
	ButtonOK:
	Gui, Submit
	
	MsgBox % SSN
	
	return
}
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Referencing variable used in If Else

24 Jun 2021, 13:05

its not in a function you could reference it anywhere the data is in the variable SSN
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: Referencing variable used in If Else

24 Jun 2021, 13:24

AHKStudent wrote: its not in a function you could reference it anywhere the data is in the variable SSN
It does not work outside the else portion. So if I place the MsgBox % SSN at the end of the script (not included in the if or else portion) and the else portion is run, I enter the SSN and nothing is displayed.
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Referencing variable used in If Else

24 Jun 2021, 13:40

It is not correct that it wouldn't work outside the else section. I'm sure you aren't seeing any MsgBox at all, right? That doesn't mean the SSN variable isn't available outside the else. That just means you aren't even executing the MsgBox because any code below the else block doesn't get executed because you have returns inside the else block. The script below shows that it works if you actually execute some code outside the else, which in this case is by pressing the Tab hotkey at some point after you manually enter a SSN:

Code: Select all

if (regexmatch(clipboard, "\d{9}"))
{
	SSN := clipboard
	MsgBox % SSN
}

else
{
	
	; Menu, tray, Icon , search.ico, 1, 1
	gui, destroy
	Gui, +AlwaysOnTop
	Gui, Add, Text,, Enter SSN:
	Gui, Add, Edit, vSSN ym
	
	Gui, Add, Button, x65 y40 w35 h20 +default, OK
	Gui, Add, Button, x115 y40 w35 h20, Close
	Gui, Show,, SSN
	return
	
	GuiClose:
	ButtonClose:
	ExitApp
	ButtonOK:
	Gui, Submit
	return
	
}
return

Tab::MsgBox % SSN

Putting those labeled routines inside the else isn't the best approach, and you're seeing one reason why.
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: Referencing variable used in If Else

24 Jun 2021, 13:46

Thanks. And you're correct... I knew it was being stored. Just not displayed. When running the script, it was telling me I had to include the return in the else portion.

Is there a better way to do what I'm wanting? I will almost always just copy the SSN but just in case, be prompted if not and then be passed to the SSN variable to then be inserted into an Excel spreadsheet.
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Referencing variable used in If Else  Topic is solved

24 Jun 2021, 13:51

Here's a more proper way to structure your code. Don't put the GUI creation or the labeled subroutines inside the else. This lets you put the MsgBox immediately after the else and it will display the value of SSN no matter which branch was executed (without a hotkey):

Code: Select all

Menu, tray, Icon , search.ico, 1, 1
Gui, +AlwaysOnTop
Gui, Add, Text,, Enter SSN:
Gui, Add, Edit, vSSN ym
Gui, Add, Button, x65 y40 w35 h20 +default, OK
Gui, Add, Button, x115 y40 w35 h20, Close

if (regexmatch(clipboard, "\d{9}"))
{
	SSN := clipboard
}

else
{
	Gui, Show,, SSN
	WinWaitClose, SSN ahk_class AutoHotkeyGUI
}
MsgBox % SSN
return

GuiClose:
ButtonClose:
ExitApp

ButtonOK:
Gui, Submit
return
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Referencing variable used in If Else

24 Jun 2021, 13:54

jarhead wrote: When running the script, it was telling me I had to include the return in the else portion.
This is one a a few reasons why you shouldn't put those labeled subroutines inside of your else block.
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: Referencing variable used in If Else

24 Jun 2021, 14:04

Thank you for the explanation and code. Much more cleaner and makes total sense.
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Referencing variable used in If Else

24 Jun 2021, 14:11

No problem. You might want to consider this as not only a little cleaner, but it captures only the SSN from the clipboard in case the clipboard contains a nine-digit number as well as other stuff:

Code: Select all

Menu, tray, Icon , search.ico, 1, 1
Gui, +AlwaysOnTop
Gui, Add, Text,, Enter SSN:
Gui, Add, Edit, vSSN ym
Gui, Add, Button, x65 y40 w35 h20 +default, OK
Gui, Add, Button, x115 y40 w35 h20, Close

if !regexmatch(clipboard, "\d{9}", SSN)
{
	Gui, Show,, SSN
	WinWaitClose, SSN ahk_class AutoHotkeyGUI
}
MsgBox % SSN
ExitApp
return

GuiClose:
ButtonClose:
ExitApp

ButtonOK:
Gui, Submit
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy and 394 guests