Dynamically terminating scripts? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Dynamically terminating scripts?

Post by kunkel321 » 16 Oct 2021, 13:14

You can see what I'm trying to do in the code... I have a couple homemade tools that I might share with coworkers. Personally I like to have a master autocorrect script always running in the background. My different tools get called from the master script with run, otherTool.exe or such. My friends at work will likely wants the tools to run as stand-alone apps though.

My tools are pretty rudimentary, so I can make them "stand alone" by adding a hotkey:: and changing all the ExitApps into Returns. It's a bit of a hassle though. The technique below might make it so that the change can be made in one place.

When I run the below code, it merely exits at the end.

Is this even possible? Thoughts?

Code: Select all

#NoEnv ; For security
#SingleInstance force
#Persistent

!^+x::
closeMode := "Return"
MsgBox the app is running.
IfEqual,%closeMode%, "Return"
	return
else
	ExitApp
EDIT: Actually, I see that my biggest script only has ExitApp in three locations. And I *think* I could do a find-n-replace and change them to Return and the code would be correct... That probably makes more sense than my "dynamic" idea...
Still... I'm curious if this could be made to work.

EDIT2: The first edit was wrong... In addition to the find-n-replace, Gui, Destroy needs to go in a spot or two.
ste(phen|ve) kunkel

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

Re: Dynamically terminating scripts?  Topic is solved

Post by mikeyww » 16 Oct 2021, 16:13

Code: Select all

closeMode := "Return"

!^+x::
MsgBox the app is running.
If (closeMode != "Return")
 ExitApp
Return

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

Re: Dynamically terminating scripts?

Post by kunkel321 » 17 Oct 2021, 12:21

Yep -- That works. Thanks Mike!
ste(phen|ve) kunkel

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

Re: Dynamically terminating scripts?

Post by kunkel321 » 17 Oct 2021, 13:23

Strange bug in my code... I'm not even sure if this is related to the "dynamic terminating" part. My script does a check to see if an additional GUI should be used. You guys helped me in the help thread "RegEx to match broken ini key." The regexes are working, and everything is good when the script exits after each use and gets run from another master script. If I set closeMode := "Return" and have a hotkey at the top though, (so it stays in RAM), then there are problems. I think I've isolated the problem (or at least part of it) to this chunk of code.

By the very nature of IF statements, either one of the MsgBoxes should pop up, but never both -- correct? I should point out that ContinueScript: is below this chunk of code, and the script does go there, but is also goes to the second msgbox. As a result, the last part of the code is actually executed twice.

Code: Select all

;OOOOOOOOOOO check for sub GUI options OOOOOOOOOOO
if !RegExMatch(MyEntry, "s)\R\V*\boptional\b\V*=\R")
{
	MsgBox This means regex 'optional' not found, so gosub continue.
	goSub, ContinueScript
}
MsgBox and THIS means that the script kept going anyway.
The entire code is hidden below. Also the needed ini files and icon are zipped up and uploaded in the above-mentioned thread about broken ini keys. If anyone is curious...
Spoiler
ste(phen|ve) kunkel

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

Re: Dynamically terminating scripts?

Post by mikeyww » 17 Oct 2021, 15:50

Incorrect. Following the If block, the script continues at the next command. Adding Else will execute one or the other, but not both.

Gosub returns to execute the next command when the called subroutine finishes.

Your "continue" subroutine executes the first time with Gosub, and the second time after the second MsgBox is displayed, because that is what the script is programmed to do.

With If... Else, exactly one of the blocks will execute. Following that, the script continues normally. Follow your script line by line, one line at a time, to see what happens.

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

Re: Dynamically terminating scripts?

Post by kunkel321 » 18 Oct 2021, 11:25

I think you were correct about using the Else. I also found another flaw in my thinking though... It seems I've been mis-understanding what Return does....

Given the code below, my thought was that after the MsgBox "End of Script," the script would "return to a resting state in RAM, waiting for me to press the hotkey again." Instread what it seems to be doing is, "return to the point in the script we were previously at before the goSub. "

When the below code is run, the needle is not found (so !RegExMatch is true), so you get the following messages:
1 This means regex needle 'optional' NOT found, so gosub continue.
2 End of Script.
3 just before ContinueScr lbl
4 End of Script.

This is why I still consider myself a newb after tinkering with Autohotkey for like 10 years or so. LOL

Code: Select all

!^+w::  ; Alt+Ctrl+Shift+w 
MyEntry = "Text that has the work neXXedle in it."
if !RegExMatch(MyEntry, "needle")
{
	MsgBox This means regex needle 'optional' NOT found, so gosub continue.
	goSub, ContinueScript
}
else
{		
	MsgBox and THIS means that the needle 'optional' WAS found.
	Return
}		
MsgBox just before ContinueScr lbl
ContinueScript:
MsgBox End of Script.
Return
ste(phen|ve) kunkel

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

Re: Dynamically terminating scripts?

Post by mikeyww » 18 Oct 2021, 12:30

Return:
Returns from a subroutine to which execution had previously jumped via function-call, Gosub, Hotkey activation, GroupActivate, or other means.
If you trigger a hotkey manually, then the Return returns control to the script ("waiting" mode). If the Return ends a subroutine called via Gosub, then the script continues to execute following the Gosub command. A single Return statement can serve either role, depending on the circumstance-- as in your script. Thus, as noted earlier, Gosub calls the subroutine. After it finishes, the script continues after the Else block, so that subroutine executes a second time. The Return inside the Else block ends the hotkey subroutine, but only if the Else block executes.

The following script demonstrates how the flow can occur without the duplication.

Code: Select all

MyEntry = "Text that has the work neXXedle in it."
!^+w::  ; Alt+Ctrl+Shift+w 
If Instr(MyEntry, "work")
     MsgBox, 64, Success, Found!
Else MsgBox, 48, Failure, Not found!
MsgBox End of Script.
Return

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

Re: Dynamically terminating scripts?

Post by kunkel321 » 18 Oct 2021, 17:21

I see. This is also related to another error in my thinking.... For some reason I thought Exit and ExitApp were the same. They are not. The setup I need is like this (an alteration of your/Mike's code):

Code: Select all

closeMode := "stay"
!^+x::
MsgBox the app is running.
If (closeMode != "stay")
 ExitApp
Exit
When I use that in my above mentioned project, it works as desired. It does not "Return" to previous points in the script, but it's also not a complete ExitApp either.

All is well. :bravo:
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v1)”