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.

Understanding Label Names and Subroutines (Beginning AutoHotkey Tip)

Once You Know How AutoHotkey Label Names Work, You Can Add More Tricks to Your Bag of AutoHotkey Skills

cheeseburger-ingredientsWhile in the past I often used the terms AutoHotkey Label and subroutine interchangeably, that’s not exactly accurate—although the two terms maintain a close relationship. Misunderstanding how Label names work can be a source of errors for new AutoHotkey script writers. However, knowing the differences between what constitutes a Label and a subroutine opens up new scripting opportunities. The CheeseBurgerRecipe.ahk script uses little-known Label behavior to either walk through all the steps or jump to individual sections of the recipe for a “Jack Stuffed Cheeseburger.” Continue reading

Why AutoHotkey for Students?

Ten Reasons Why Every Student Should Use the Free AutoHotkey Windows Tools! AutoHotkey Helps You So Much Academically That You’ll Think You’re Cheating! Plus, It’s Delicious Brain Food and Helps Get You a Job!

Using AutoHotkey should be mandatory for every student from high school on through college! Not merely because learning AutoHotkey invigorates the brain cells (which it does!), but robotsexismit makes all your Windows tasks so much easier that it seems unsporting. AutoHotkey takes the grunt work out academia making learning fun…almost.

(If you know a student who uses a Windows computer, then do him or her a favor by sending him or her a link to this article. If he or she doesn’t already know about AutoHotkey, he or she will thank you profusely.)

For those unfamiliar with the free open-source scripting language, AutoHotkey offers simple, yet powerful, commands for adding enhancements to any Windows computer. In addition to their simplicity, the little apps work in any Windows program or on any Web page. For a brief overview, see this “Introduction to AutoHotkey: A Review and Guide for Beginners.” Continue reading

Why AutoHotkey for Artists and Graphic Designers?

While Using AutoHotkey with Windows Makes Sense for Most Professions, It’s Not So Obvious for Artists and Graphic Designers

If you work in the graphic arts on computers, then you know that a multitude of programs exists for creating designs and original art pieces. Each one works a little differently with artistcartoonspecific strengths and weaknesses. You may even use a couple of particular software packages for certain projects. Wouldn’t it be nice to own a few tools which work in every one of those programs?

When producing ads and capturing screenshots, even I use a number of disparate apps for designing and finishing the artwork. (I have a preference for the free Paint.Net program.) However, no matter how powerful and feature-filled the software, I always want the tools to do a little more while working in each program. That’s where AutoHotkey comes in. Continue reading

Why AutoHotkey for Writers, Bloggers, and Editors?

If You Write or Edit For a Living (or Fun) and Use a Windows Computer (Most People Do), Then You Should Use the Free AutoHotkey Software

I’m starting this series I call “Why AutoHotkey?” to illustrate the many reasons for Windows users to install and learn AutoHotkey.

Since I spend most of my time writing, it only makes sense that I start off with why wordsmiths should use AutoHotkey on their Windows computers. There exists a ton of tools for bloggers and editors which include built-in spell checkers and grammar checkers. AutoHotkey does not replace any of these but rather augments them with those extras which add an edge when writing. Best of all AutoHotkey works anywhere and everywhere on a Windows computer. Continue reading

New Hotkey Book! (AutoHotkey Tips and Tricks)

AutoHotkey Techniques and Best Practices E-Book for Automating Your Windows Computers with Hotkey Combinations—Includes Something for Everyone!

Whether you’re a noobie to AutoHotkey scripts or an advanced programmer, of all my books, AutoHotkey Hotkeys may be the most important for new little-known tricks and useful ideas. It’s not that the other books don’t cover significant features of AutoHotkey, but this book includes some of the most practical tips for adding power to your scripts. I didn’t plan it that way. Continue reading

Windows Programming Made Easy (Why AutoHotkey?)

Never Programmed Before? You’ll Find No Better Language for Learning Than AutoHotkey!

Note: A free scripting language for Window computers, AutoHotkey provides simple techniques for controlling and adding power to your PC. Since today (and into the foreseeable future) Windows PCs continue to represent 90% of the desktop and laptop computers, knowing how to write simple AutoHotkey scripts adds significant experience to anyone’s résumé. Do it for fun and profit! Continue reading