
How AutoHotkey Processes a Script
by
Jack Dunning
New
to AutoHotkey? See our Introduction to AutoHotkey!
* * *
Ron
Cerrato and I exchanged e-mails about problem of using AutoHotkey
for logging in when there is a new Web page between the username and
password. While the method of using MsgBox command
worked for me on my bank site, the loss of focus caused by MsgBox prevented complete input
for Ron. Eventually, he went back to using the Sleep command. I had avoided the
Sleep command because the Web page loading times are hard to determine.
Ron solved the problem by using very long Sleep times (7.5 seconds and
5.0 seconds). Here's Ron's final e-mail (found in the free introductory book
AutoHotkey Tricks You Ought To
Do With Windows):
Thanks
a lot!!! This works great:
:*:a1@:: Run, https://mybank.com/obc/forms/login.fcc Sleep, 7500 SendInput, username{return} Sleep, 5000 Sendinput, password{return} Return
I
now have five green "H's" in the System Tray: Login, Autocorrect,
Reminder, Dictionary, StripAllReturns.
This
last comment about five separate AutoHotkey scripts running
simultaneously brings up an important question. How many is too many?
If all the green icons in the System Tray don't bother you, then there
is nothing technically wrong with running a number of separate scripts.
One advantage of doing this is that each app is less likely to conflict
with another. However, if you want to combine scripts, then there are a
few things that you need to understand about how AutoHotkey processes
scripts.
Combining
AutoHotkey Scripts
There
are two ways that AutoHotkey processes code when it reads a script.
First are those actions that it executes immediately when loading. The
second are those actions that it stores in memory for later execution.
Note: For more detail on processing AutoHotkey scripts, see the series
Beginner’s Guide to How AutoHotkey Scripts Work found at Jack's
AutoHotkey Blog.
The
Auto-execute Section of a Script
Understand
that when an AutoHotkey is loaded every line of code is read. However,
only certain commands are immediately executed (the auto-execute
section and # commands) while others are stored for later activation in
memory (hotkeys, hotstrings, labels, and functions). If errors are
found in the script (misspelled commands, missing labels, etc.), an
error message will be displayed and the script will stop loading.
The
portion of a script that runs immediately when it is loaded is called
the auto-execute section. There is only one auto-execute section in an
AutoHotkey script. It must appear first in the script before any other
snippet which might interfere with these lines of code. After loading
and the initial scan of the script, AutoHotkey starts at the beginning
of the script executing commands. If a Return command is encountered, no
more code will be executed. If a hotkey snippet is encountered, the
auto-execute section is ended. If a hotstring (e.g. ::lol::laugh out
loud) is found inside that beginning code, AutoHotkey will stop
processing the remaining code. If a Label (subroutine snippet) is
found, AutoHotkey stops executing code. In other words, none of the
following should appear within any lines of code which must run when
the script first loads:
::lol::laugh out loud
^#F3:: ; Hotkey to add today's formatted date Send, %A_DDD%, %A_MMM% %A_D%, %A_YYYY% Return
TrayTextAdd: ; Label (subroutine) sending text SendInput, !{Escape} SendInput, %TextInsert% Return
If
any one of these three snippets or the Return command are located within
the lines of code designated to run as part of the setup
(auto-execute), then the script will stop processing at that point. If
you find that not all of the code that's suppose to run on loading is
running, then look for something interfering with the beginning of the
script (the auto-execute section). On the other hand, these
self-contained subroutines shown above can be located virtually
anywhere else in the script after the auto-execute section—and their
order does not matter. They have all been placed in memory during the
first scan by AutoHotkey.
The
notable exception to snippets of code interfering with the running of
the auto-execute section is AutoHotkey Functions which can appear virtually
anywhere in a script including the beginning of the script, in the
middle of the auto-execute section, at the end, or in an external file.
If a function is found while loading the script, it is loaded into
memory, but skipped over if found in the auto-execute section.
Snippets
Put Into Memory for Later Execution
In
addition to immediately running the auto-execute section at the
beginning of the script, AutoHotkey also reads through the entire file
storing to memory all hotkey subroutines, hotstrings, and labels
(subroutines terminated with the Return command), and function for
later use. For this reason, if you have scripts which are composed of
only hotkeys or hotstrings with no auto-execute section, such as Ron's
Login and Autocorrect, they can be combined by adding the snippets of
code to the end (not the beginning) of another script even if it has an
auto-execute section.
If
script does have an auto-execute section, such as Ron's StripAllReturns
which includes one-line to immediately add the feature to the System
Tray right-click menu:
Menu, Tray, Add, Strip Selected Returns, StripReturns
then
that line will need to be placed in a non-interfering place (not inside
the brackets of a Loop, If conditional, or other
operator) in the auto-execute section of the combined script. This is
because any standalone command found outside the auto-execute section
is ignored and not run. Then all other hotkeys, hotstrings, and labels
can be placed at the end of the file.
To
make the process easier, the #Include
command can be used to add the code
to a file without actually embedding the code in the file. When the
file loads, each file specified with the #Include command is processed
exactly as if it exists at the same spot where the #Include is located. For this reason, #Include is normally used at the end
of the script. (#Include can be used anywhere in the
script if it only contains functions since they can appear virtually
anywhere including before the auto-execute area.) However, if the
included file has it's own auto-execute section I will need to be added
to auto-execute section of the main script.
Multiple
GUI Windows
If
you're are combining scripts with separate
GUI windows,
then you may need to modify the scripts by setting separate names for
each window in the following format:
Gui, MyGui:Add, Text,, Text for about-box. Gui, MyGui:Show
where MyGui is a unique name for each
GUI window. (AutoHotkey basic version uses numbers in place of the
names.) Otherwise everything will default to the same GUI window likely
causing problems. In most of the examples I've used over the years, I
don't include GUI names for the sake of simplicity, because once a name
is assigned to a GUI window, it must be used with all labels including
the built in subroutines. This can cause greater confusion for the
newbie AutoHotkey script writer.
There
are other commands that are executed on load regardless of where they
are located in the script. These commands such as #IfWinActive
are
preceded with the # character. However, their position in the script
may be very important affecting all hotkeys and hotstrings below them
in a script—as is the case with #IfWinActive. Check the individual #
commands for how they affect a script.
Sometimes
new AutoHotkey script writers will place commands between hotkeys,
hotstrings, and labels thinking that they will be executed. Any
commands or group of commands not located in the auto-execute section
or within one of the hotkey, hotstring, labels, or functions are
completely ignored by AutoHotkey.
Understanding
how AutoHotkey scripts are processed will make you a better AutoHotkey
script writer. You will also find that you are debugging your scripts
quicker. If this isn't clear enough or doesn't have enough specifics,
then maybe the alternative
approach to explaining AutoHotkey scripts I used in Chapter
Thirty-seven of the AutoHotkey Applications book will be
clearer with more specifics. If you have the book, it might be
worthwhile to read both. It is
an important topic.
* * *
Find
AutoHotkey scripts at our AutoHotkey Scripts page!
For More Information
If you're interested
in testing AutoHotkey to see if it might be right for you, then go to "Installing
AutoHotkey and Writing Your First Script."
This page shows you how to get up and running with AutoHotkey,
plus it offers links to other articles on how to use AutoHotkey.
To see more of the many possible applications for AutoHotkey check out
"Free AutoHotkey Scripts and Apps for Learning."
If
you want more information in either the Amazon Kindle format, EPUB
format for use on the iPad and other types of tablet computers (or on
your
PC), or PDF for printing on notebook size paper, then check out the
following e-books by Jack Dunning:
Get this introductory
e-book, AutoHotkey
Tricks You Ought to Do with Windows!
Now
available in e-book format, Jack's A
Beginner's Guide to AutoHotkey, Absolutely the Best Free Windows
Utility Software Ever!: Create Power Tools for Windows XP, Windows
Vista, Windows 7 and Windows 8.
Building Power Tools for Windows XP, Windows Vista, Windows 7 and
Windows 8, AutoHotkey is the most powerful, flexible, free
Windows utility software available. Anyone can instantly add more of
the functions that they want in all of their Windows programs, whether
installed on their computer or while working on the Web. AutoHotkey has
a universality not found in any other Windows utility—free or
paid.
Now in its second edition (October 2013),
Jack
takes you through his learning experience as he explores writing simple
AutoHotkey scripts for adding repetitive text in any program or on the
Web, running programs with special hotkeys or gadgets, manipulating the
size and screen location of windows, making any window always-on-top,
copying and moving files, and much more. Each chapter builds on the
previous chapters. (The second edition now includes a chapter index of
the AutoHotkey commands used in the book, plus Internet links directly
to each commmand to the official AutoHotkey Web site.)
Also available at Amazon.com for the Kindle and
Kindle software.
Jack's
latest AutoHotkey book which is comprised of updated, reorganized and
indexed chapters from many of his sample applications is now available
at Amazon for Kindle hardware
(or free software) users. The book is organized and broken up into
parts
by topic. The book is not for the complete beginner since it builds on
the information in A Beginner's Guide to AutoHotkey.
However, if a person is reasonably computer literate, they could go
directly to this book for ideas and techniques without the first book.
Jack shows how to build real world AutoHotkey applications. The
AutoHotkey commands used are included in a special index to the
chapters in which they appear. Even I can't remember everything I
wrote."
Also available at Amazon.com for the Kindle and Kindle
software.
Jack's
third AutoHotkey book AutoHotkey Applications (preferred,
EPUB format for iPad, Android, and computers; MOBI for Amazon Kindle;
and PDF for printing) is an intermediate level book of ideas and
applications based primarily on the AutoHotkey GUI command. The book
emphasizes practical applications. The book is not for the complete
beginner since it builds on the information in the other two books.
However, if a person is reasonably computer literate, they could go
directly to this book for ideas and techniques without the other books.
There is an extensive index to the ideas and techniques covered in the
back of the book. (Also available from Amazon for the Kindle and Kindle
software on other devices.)
For an EPUB (iPad, NOOK, etc.) version of AutoHotkey
Applications click here!
For a PDF version for printing on letter
size paper for inclusion in a standard notebook of AutoHotkey
Applications click here!
This
Beginner's Guide to
Using Regular Expressions in AutoHotkey
is not a beginning level AutoHotkey book, but an introduction to using
Regular Expressions in AutoHotkey (or most other programming
languages). To get the most from this book you should already have a
basic understanding of AutoHotkey (or another programming language).
Regular Expressions (RegEx) are a powerful way to search and alter
documents without the limitations of most of the standard matching
functions. At first, the use of RegEx can be confusing and mysterious.
This book clears up the confusion with easy analogies for understanding
how RegEx works and examples of practical AutoHotkey applications.
"Regular Expressions in AutoHotkey" will take you to the next level in
AutoHotkey scripting while adding more flexibility and power to your
Windows apps. (This book is also available
at Amazon.com)
|