Formatting Text for Various AutoHotkey Tools Can Get Tedious—Use AutoHotkey Section Continuation to Simplify the Work
In the MouseMeasure.ahk script (discussed last time), I added a pop-up window displaying instructions on how to use the app. I formatted the text with a number of new lines and tabs. Usually, this would have required careful placement of the special escape characters, but not this time.
When adding instructions to a MsgBox, we often resort to adding returns (`
r), newlines (
`n), and tabs (`t) to create the desired layout. While this works, it often produces perverse effects—especially when using individual line continuation techniques to wrap the code for display purposes. However, AutoHotkey offers an alternative type of code continuation which makes text formatting quick and easy— continuation sections (Method #2).
Multi-line continuation sections offer a number of advantages over using escape sequences to format text:
- Continuation sections maintain the same format as originally entered including all tabs, returns, and spaces—What You See Is What You Get (WYSIWYG).
- Continuation sections work either in replacement text or expressions.
- Continuation sections allow variable text replacement (
- Continuation sections ignore the effects of special symbols without requiring escaping (i.e. quotation marks, commas, etc).
- Continuation sections offer special options for removing extra space, joining lines, trimming text, and more.
Adding Multi-line Text
In the MouseMeasure.ahk script, I assigned a list of text instructions to a variable using this section continuation technique, then displayed the text in a MsgBox—no need to use returns (`
r) or tabs (`t). To do the same thing using those escape characters would require at least 14 returns (`
r) and six tabs (`t). Even then, the command code would either appear as one long line or, if using the individual line continuation (Method #1), likely leave an extra space (or two) before each punctuation mark.
Assign Multiple Lines of Text to a Variable
The following snipped assigns a formatted piece of text to a variable creating a WYSIWYG multi-line text entry:
Help = ( To Calibrate Range: 1. Cursor at start point — CTRL+LButton and hold. 2. Drag to end of scale and release. 3. Enter length of scale and units i.e. 10 miles. Manual Calibration: CTRL+ALT+Z To Measure Distance: 1. Cursor at start point — SHIFT+LButton and hold. 2. Drag to end of measurement and release. 3. Distance displayed and saved to Clipboard. CTRL+ALT+F12 for this Help Message. ) MsgBox, 0, MouseMeasure Help, % Help
The MsgBox command displays the contents of the variable Help exactly as originally typed.
Note: You can also use the
:= type of variable assignment, but you must enclose the section in quotes:
Help := " ( . . Multiline Text . . )"
… although to display a single double-quote mark (
") and prevent runtime errors, you must now escape any quotation marks in the text with a preceding quote (
Using Multi-line Continuation in Commands
Any AutoHotkey command which accepts plain text can use the same type of multi-line continuations. For example, in the same MouseMeasure.ahk script, the updating ToolTip command displaying the current pixel distance and the converted unit length (see image) uses the following formatting:
ToolTip [%dx%:%dy%]`n%dist% px`n%distCalibrated% %units%
Using section continuation, you can replace the above line with the following WYSIWYG formatted section:
tooltip, ; comma required ( [%dx%:%dy%] %dist% px %distCalibrated% %units% )
Note: You can include the traditional double-percent-sign text replacement (
%var%) inside multi-line continuation sections.
While you can use this technique with any command that uses plain text parameters (or expressions), inserting so many lines in the middle of a command tends to make the code less readable. You may find it easier to read by saving the multi-line text to a variable, then using that variable as either a text substitution (
%var%) or forced expression (
Multi-line Section Continuation Options
AutoHotkey offers options w