How can I get OnError to repeat the line/command? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
awkwrd
Posts: 15
Joined: 26 Feb 2023, 21:04

How can I get OnError to repeat the line/command?

06 May 2024, 02:57

In this example, var1 is initially unset, so msgbox var1 throws an error.
I use OnError to automatically detect that var1 was unset and then give it a value of "hey".
But I still need the script to actually show a messagebox with "hey". Instead, the script proceeds to the next line, msgbox "box2". So no messagebox "hey" was shown.
I understand that this is the intended behaviour of OnError, but is there a way to repeat msgbox var1 anyway?

Code: Select all

myfunc()

myfunc()
{
	static var1
	OnError ErrFuncObj
	msgbox var1
	msgbox "box2"
	ErrFuncObj(ErrorObj, Mode)
	{
		if ErrorObj is UnsetError
		{
			msgbox "UnsetError yo"
			var1 := "hey"
		}
		else
		{
			throw ErrorObj
		}
		return -1
	}
}
(Ideally, I want to use OnError in my actual script, so that when a variable is unset, I'll use the ErrFuncObj to call an InputBox to manually key in and assign a value to the unset variable. When this happens, I'll need the script to re-run the same line, but do it with my newly set variable.)
User avatar
Seven0528
Posts: 393
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How can I get OnError to repeat the line/command?  Topic is solved

06 May 2024, 03:25

 Would this method also be effective in your case? I'm nor sure...

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
UserData := UserData_Base()
UserData.propA := "A"
loop 2
    msgbox UserData.propA
loop 2
    msgbox UserData.propB

class UserData_Base
{
    __get(name, params)    {
        if (!this.hasOwnProp(name))    {
            value := inputBox("UserData." name " := ").value
            this.defineProp(name, {value:value})
            return value
        }
    }
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: How can I get OnError to repeat the line/command?

06 May 2024, 05:21

@awkwrd
Based on the example of your code, the purpose of such complication is not clear. Your specific example can be made much simpler:

Code: Select all

#Requires Autohotkey v2
MyFunc()

MyFunc(param := unset) => MsgBox(param ?? 'hey')
Or so:

Code: Select all

#Requires Autohotkey v2
var1 := unset
MyFunc(var1 ?? 'hey')

MyFunc(param) => MsgBox(param)
Please post your script code inside [code] ... [/code] block. Thank you.
awkwrd
Posts: 15
Joined: 26 Feb 2023, 21:04

Re: How can I get OnError to repeat the line/command?

09 May 2024, 04:04

95% of the time, my variables would be assigned values from my spreadsheet, then the script helps me to fill forms. However, maybe 5% of the time I won't be running my script through the spreadsheet; in this scenario I just want to input values manually through InputBox and run the script anyway (because I don't want to ditch the whole script and have to fill the form manually :cry: )

@Seven0528 yes, thank you. I've tried it and it works for my script. With this, I can write my functions without having to check whether my variables are unset. Otherwise I would have to write checks for every function.

@vmech thank you, your code works, but it was what I was hoping to avoid. I didn't want to check if my variables are unset for every function that I have. Also I figured that I was likely to forget to add this check when I add new functions to my script in future, so it's easier if I can offload the check to something that continuously works passively in the background like OnError (or __get as Seven0528 demonstrated)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], Draken, kunkel321, reddyshyam, Tvlao, zhang and 28 guests