Last February, AutoHotkey Release 1.1.28.00 Included Important New Hotstring Capabilities
A few months back while working on my latest book, Jack’s Motley Assortment of AutoHotkey Tips, I encountered a new x option for Hotstrings. The x option enables the running of functions and commands rather than replacing text. This new feature surprised me since, in the past, a one-line Hotstring would only do text replacement.
I immediately recognized that this approach to one-line action Hotstrings could immediately shorten a long list of function Hotstrings. However, closer scrutiny of the new AutoHotkey release revealed further (and possibly more important) new Hotstring functionality. In addition to the x option, the February AutoHotkey release includes:
- A new Hotstring() function which adds dynamic capabilities to Hotstrings.
- A new method for creating function Hotstrings by defining the function immediately after the Hotstring(s).
These added tools deserve special attention. In the next couple of blogs, I plan to examine the implications of these changes for AutoHotkey Hotstrings.
Shortening Hotstring Code with the X Option
One of the primary benefits of the Hotstring x option includes shortening AutoHotkey code. When we convert the following three-line Hotstrings (taken from the AddNextWeekDay.ahk script)—each of which calls a function:
:c*:dsun1:: Weekday(1,1) Return :c*:dmon1:: Weekday(2,1) Return :c*:dtue1:: Weekday(3,1) Return
we rewrite them as:
:c*x:dsun1::Weekday(1,1) :c*x:dmon1::Weekday(2,1) :c*x:dtue1::Weekday(3,1)
The x option disables the text replacement to the right of the second double colon and runs the function or command located in that spot. Now, working in a manner similar to Hotkeys, we can write a one-line action Hotstring by adding the x option.
Using an AutoHotkey Command in a One-Line Hotstring
When my kids were young, I led them to believe that I watched everything that they did on their computers via the network. This worked out pretty well until they figured out I had lied. If I had added a script similar to the one below to their computers, it would have reinforced my deception:
:x:shoot:: :x:fudge:: :x:heck::MsgBox, , MsgBox Pop-up,We don't use language like that!
Before AutoHotkey Release 1.1.28.00, AutoHotkey assumed that the text appearing on the same line as the Hotstring definition represented replacement text. To create an action Hotstring, you needed to start the commands on the following line. Rather than replacing text, the x option tells AutoHotkey to run the command or evaluate the expression.
Tip: Testing indicates that only the last Hotstring which executes the command requires the inclusion of the x option, although, for consistency, you may want to add it to each item in the stack.
Using Label Subroutines in Hotstrings with the X Option
While the x option in Hotstrings does not directly accept Label subroutine names, you can call any subroutine using the GoSub command:
:x:shoot:: :x:fudge:: :x:heck::GoSub, MsgBoxTest MsgBoxTest: TextInput := substr(A_ThisHotkey,4) MsgBox, , MsgBox Pop-up,We don't use language like "%TextInput%" either! Return
This approach uses the single-line action feature to call another subroutine which manipulates the data before displaying it.
For the last Hotstring, we could have written the subroutine an action Hotstring but by creating a Label subroutine, rather than needing to stack the Hotstring definitions (as shown above), we can place our Hotstrings anywhere in the script—as long as, we use the GoSub command. This also helps us later when we create Hotstrings on-the-fly using the new Hotstring() function—a topic for next time.
Using Expressions in Hotstrings with the X Option
Although the documentation mentions using expressions in one-line Hotstrings, it did not include any examples. The following snippet offers a couple of statements where Hotstrings execute expressions:
:x:geez::MsgBox, % (xyz = "test")?"Yes " . xyz:"No " . xyz :x:geez1::xyz := "test" . 1 :x:geez2::xyz := "test"
The first line forces an expression (% ) using the ternary operator (?:) which pops-up a message box whenever typing the text “geez” followed by a space or punctuation. Initially, the variable xyz contains no value. Type the keys “geez1” followed by a space and AutoHotkey stores “test1” to the variable xyz. Type the keys “geez2” and AutoHotkey stores “test” to the variable xyz. Type “geez” to check the result.
In effect, the above creates Hotkeys which activate when typing a series of text characters. You could always implement this type of action Hotstrings by starting the code on the line immediately following the definition. With AutoHotkey Release 1.1.28.00, we can now define it with one line of code. The significance of this techniques may become more apparent when we take a look at the new Hotstring() function which allows the creation, modification, and deletion of Hotstrings after loading the AutoHotkey script.
Major New Hotstring Features in AutoHotkey Release 1.1.28.00
- Added Hotstring X option to execute a same-line action instead of auto-replace.
- Added Hotstring() function.
- Added function Hotstrings.
I plan to dive deep into the Hotstring() function which adds power and flexibility to your Hotstring scripts not previously seen.
Click the Follow button at the top right of this page for e-mail notifications of new blogs.
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)
Interesting. Thanks!
LikeLike
Hello Jack. This is an interesting topic and one I think of using to expand the tool I created (https://github.com/LevDoesCode/magic/)
I see you show :c*x:dsun1::Weekday(1,1) which looks like a function taking 2 parameters, but AHK gives me an error (2nd parameter must not be empty).
LikeLike
I’m not sure why you’re getting that error. The function does call two parameters.
LikeLike