GoTo Command Versus GoSub Command (AutoHotkey Tip)

While the Online Documentation Advises Avoiding the AutoHotkey GoTo Command in Deference to GoSub or a User-Defined Function, You’ll Find Times When GoTo Works Best!

cheeseburgerwhiteicon Not everyone likes their cheeseburger prepared the same way. Some prefer fried onions while others love the crunch of raw condiments. In fact, In-N-Out Burger touts a not-so-secret menu offering cheeseburgers “Animal Style.” The preparation of this option with caramelized onions and mustard fried hamburger significantly changes the cheeseburger experience. That gives us an excuse to add flexibility to our CheeseburgerRecipe.ahk script—first discussed in the blogs “Why AutoHotkey for Chefs and Dieticians?” and “Understanding Label Names and Subroutines (Beginning AutoHotkey Tip).”

Note: I changed the CheeseburgerRecipe.ahk script in a number of ways—a few of which I plan to discuss in future AutoHotkey Tips. For now, I confine myself to highlighting the use of the GoTo command. For specific changes, see the comments at the top of the CheeseburgerRecipe.ahk script.

Adding Options to the Cheeseburger Recipe

I found another recipe at FoodNetwork.com which replicates the In-N-Out Animal-Style Cheeseburger recipe. Copying the same AutoHotkey techniques I used for the Jack Stuffed Cheeseburger, I added the new steps for this recipe to the script. Since the System Tray menu grew too crowded, I implemented submenus for each recipe. (See image below.)

CheeseburgerAnimalStyle
Recipes listed in the System Tray icon right-click menu include submenus for selecting individual steps, then continue displaying succeeding steps unless canceled.

The primary differences between the Jack Stuffed Cheeseburger and the Animal-Style Cheeseburger include caramelizing onions, mixing a special sauce, burgers with no embedded cheese, preparation of the buns, and frying the burgers with mustard. While more involved than the first recipe, the new recipe might satisfy the “Animal” in anyone. But, what if you wanted a Jack Stuffed Cheeseburger Animal Style?

You could write an entirely new recipe for a Jack Stuffed Cheeseburger Animal Style but that would include a great deal of redundancy. By using the AutoHotkey GoTo command, we combine the redundant steps of the two recipes to create a third variation.

CheeseburgerGoTo
Each column represents one recipe subroutine followed by a single Return command at the end. Each box contains the Label name for the location of that individual step within the recipe. After each step, AutoHotkey automatically drops into the Label below since no Return command intervenes until the end of the recipe. The GoTo commands in the third recipe jump to the Label name point (e.g. GoTo, Animal_Onions) in the appropriate recipe subroutine—proceeding as if a totally separate recipe.

For the new Jack Stuffed Animal Style Cheeseburger, the recipe starts at the Label point Stuffed_Animal_Ingredients: which includes the ingredients for the Animal Style Cheeseburger plus the added Jack cheese (for stuffing) and varied meat and cheese amounts. Using the GoTo command, AutoHotkey jumps directly to the Animal_Onions: pointer for caramelizing the onions.

Since the process jumps into the middle of the Animal Style Cheeseburger subroutine, when the Animal Style Cheeseburger – Onions window closes, it automatically drops into the Animal_Sauce: routine. Next, the recipe must GoTo the Jack Stuffed Cheeseburger Label named Prepare: for shaping the burgers with Monterey Jack cheese inside. In order to make this jump decision, AutoHotkey must identify the currently running recipe subroutine: either Animal Style Cheese Burger (no Goto) or Jack Stuffed Animal Style (GoTo).

The initial menu selection tells us which recipe track to follow. The menu name (A_ThisMenu) identifies the Jack Stuffed Animal Style recipe:

If (A_ThisMenu = "StuffedAnimal")
  StuffedAnimal = 1
Else
  StuffedAnimal = 0

Placing this condition into the MessageSetup: subroutine identifies the origin of any mixed-use menu steps. AutoHotkey determines the proper path by checking the value of the StuffedAnimal variable. For example, after the Animal_Sauce: window, the process uses the following check:

If (StuffedAnimal = 1)
  GoTo, Prepare

If running the Jack Stuffed Animal Style sequence (StuffedAnimal = 1), AutoHotkey jumps to the Prepare: point of the Jack Stuffed Cheeseburger routine. Otherwise, the Animal Style Cheeseburger continues dropping through its steps.

This same conditional format creates the jumps from Prepare: to the Animal_Buns: point and from Animal_Buns: to the Stuffed_Animal_Cook: point for the Jack Stuffed Animal Style steps. This process creates a new variation from older recipes without rewriting redundant steps. Often using the GoSub command (for Go Subroutine) works better, but in this situation, GoTo makes the routine act as if it’s one continuous series of steps.

GoTo Versus GoSub

megadeal180goldTo understand the difference between the GoTo command and the GoSub command, know that when a subroutine finishes, the GoSub command returns to the next line in the original calling routine, while the GoTo command does not. In most cases, since it returns to the same spot in the script, you’ll probably find the GoSub command (or a function) more appropriate. But if you know that you don’t want to return to that point, then feel free to use GoTo—but you better know where you plan to end up.

A common usage of GoSub runs the subroutine then returns to the next line after the calling line. For example, I moved the loading of the recipe variables for each step out of the auto-execute section of the script into separate Label subroutines, then replaced each with a single GoSub command. This made the script more readable by greatly reducing the length of the auto-execute section. In place of the code, I inserted the following GoSub command statements:

GoSub, StuffedCheeseburger 
GoSub, AnimalCheeseburger
GoSub, StuffedAnimalCheeseburger

However, using the GoSub command in the mixed recipe scenario for the Jack Stuffed Cheeseburger Animal Style would have caused more problems than it solved. For example, if the GoSub command appeared in place of GoTo, then each call would create a new return point. That means clicking the Cancel button would cause the script to jump to the last GoSub call rather than exiting the thread. In many cases, jumping back would cause unwanted effects (e.g. display an inappropriate step from another recipe). In fact, even running through the entire recipe without canceling would cause an untold number of displays of various steps. After encountering the last Return in the subroutine, the final Return jump executes more GoSub commands—each creating an additional Return point. The GoTo command keeps it clean, allowing an exit with any Return encountered in any recipe sequence. To the user, each recipe (including the mixed) looks like a continuous set of steps.

While probably best avoided if GoSub or a function can do the job, the GoTo command works well in scripts emulating flow charts and plays an important role in decision trees. In those special situations, knowing where you are (and where you’re going) may be more important than where you’ve been.

Why AutoHotkey for Chefs and Dieticians?

cheeseburgerwhiteicon While You Don’t Often See Television Chefs Using Computers, AutoHotkey Offers Guidance When Cooking “Jack Stuffed Cheeseburgers” at Home

*          *          *

While I came up with a pretty cool barebones recipe script for this AutoHotkey blog, I’m not sure how well computers and cooking mix. From what I’ve seen, professional chefs don’t have much time to mess with technology—except for possibly writing cookbooks—in which case, I would refer them to “Why AutoHotkey for Writers, Bloggers, and Editors?.”

*          *          *

cheeseburgercartoon

Using a computer while cooking presents a challenging task. Although looking up a recipe on a smartphone works great, poking at the device with fingers covered in cookie dough proves impractical. Ideally, any kitchen machine is lightweight and stands upright on its own.

Although a little pricey, the Microsoft Surface Pro (or another less expensive Windows ultralight laptop computer) might do the job. Large enough to read the screen, yet easy to move around a cooking area, these computers help any budding chef or dietician who needs to check recipes—as long as no one dumps boiling water on the keyboard. For size and convenience, finding equipment more practical than a lightweight laptop proves difficult. Continue reading

Change Script Features on the Fly with the Windows System Tray Icon Context Menu (AutoHotkey Tip)

Dynamically Switch the Actions of a Running AutoHotkey Script with a Simple Click of a System Tray Right-Click Menu Item

From Rick Corbett:

Hi, Jack,

Talking of “Adding Actions to Windows System Tray Icon Menu“, perhaps you would consider writing about using—for example—ToggleCheck, MenuItemName to amend a running script dynamically, i.e. change something (like toggling logging to a file versus a MsgBox), then reload.

*          *          *

AutoHotkey Library Deal
AutoHotkey Library Deal

That’s an excellent question, Rick! Often, after loading a script,  we want to either turn features off and on or change how they function. Adding separate Hotkeys or rewriting scripts becomes wearisome. But, what if we could add a feature to the Windows System Tray Icon Context Menu (right-click) which either toggles an action on and off or completely changes how it works? AutoHotkey offers a straightforward way to get it done. Continue reading

Why AutoHotkey for Grandparents?

Remember All Your Grand Kid’s Birthdays and Their Ages! There’s No Limit to the Number of Ways You Can Amuse Your Grandchildren with AutoHotkey, Plus It Gives Your Brain a Much Needed Workout!

If you only have one grandchild, then you probably won’t have much trouble recalling his or her birthday or age. In that case, you may not have much interest in the grandbotslittle AutoHotkey GrandKids.ahk script. However, AutoHotkey offers much more which can enrich your offspring’s offspring’s education and entertainment—including a one-line script which verbalizes out loud the letters and numbers on the computer keyboard. But more importantly, learning to write AutoHotkey scripts exercises your mind—something everyone needs.

*          *          *

Some of the scripts in this blog may not make AutoHotkey look easy, but you’ll find the first steps to AutoHotkey literacy quite simple. For a comfortable startup, check out this Introduction to AutoHotkey.

*          *          *
Continue reading

Tips for Optimizing the Standard AutoHotkey Message Box (MsgBox) Command (AutoHotkey Quick Reference Part Six)

AutoHotkey MsgBox Tricks for Adding Power and Flexibility to Your Scripts. If You Want a Simple, Quick Way to Display Data and Execute Action, You’ll Love These Techniques!

As I go back to work on the AutoHotkey Quick Reference script (based upon the hidden index in the AutoHotkey Web site), I ponder what enhancements I should add next. I find fourbuttonmsgboxthat I now regularly activate the lookup Hotkey combination which displays AutoHotkey command, function, or variable formats—often just to check syntax. The usefulness of this reference script motivates me to do more with it.

After adding a special section to the script which parses the math function Web page (not yet discussed) and planning a future implementation of some pop-up menus—possibly using the Input command (discussed in the Beginning AutoHotkey Hotstrings book)—I finally settled upon including a feature which inserts the text format for commands, functions, and variables from the message box directly into any editing field. Continue reading

Too Much Planning Can Get in the Way of Good Scripting (AutoHotkey Quick Reference Part Five)

While Preplanning Script Writing Can Be Useful, Don’t Take It Too Seriously—Sometimes It Only Makes Sense to Rewrite Everything

The AutoHotkey script writing process rarely runs in a straight line. Often I start with a vague concept of what I want to do then start fiddling with the tools. Unlike when building a toolshed or bookcase, I rarely begin with a complete plan or blueprint for an AutoHotkey script. In fact, the code may undergo numerous changes during the debugging and problem-solving phases.

sarcastictweetsFor anyone who builds things, this approach may be disconcerting. Afterall, you can’t afford to build a house by trial-and-error. The cost of wasted materials would be prohibitive. Traditionally, we spend a great deal of time in the planning phase to make sure we avoid expensive mistakes. Even in computer programming, large projects come together much better after extensive planning. But with smaller projects such as AutoHotkey scripts the opposite may be true. I often start a script with only a vague idea of what I want to do. As I work on it, the possibilities expand and I often change course. Continue reading

Ryan’s RegEx Tester for Building INI Data Files (AutoHotkey Quick Reference Script, Part Five)

Sometimes It’s Quicker and Easier to Use Ryan RegEx Tester Rather Than Writing an AutoHotkey Script

I used Ryan’s RegEx Tester in an earlier blog to create Web links without writing an AutoHotkey script. This time I take advantage of this powerful tool by using it to extract data for insertion into the regexrobotcartoon INI file discussed in the last blog on this topic. The fact that you can paste any text into the top of the RegEx Tester, add a Regular Expression (and a substitution expression for RegExPlace()), then extract the altered text from the bottom pane makes it a unique AutoHotkey app. This capability alone can motivate someone to learn how to write Regular Expressions.

Note: This series of blogs discusses the evolution of the AutoHotkeyQuickRef.ahk script which takes advantage of the hidden index in the AutoHotkey.com Web site.

Continue reading