Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 4—Fixing %Var% Variable Replacements)

With the Removal of Most Forms of %Var% Variable Replacement from AutoHotkey V1.1, Expressions in V2.0 GUI Functions Require Special String Concatenation Attention

In the previous part of this series on “Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 3—RegExs for Converting to V2.0)“, I introduced Regular Expressions (RegEx) for converting certain AutoHotkey V1.1 commands into V2.0 functions. While generally effective, the conversions left any %var% variable replacements untouched.

Converting %Var% for AutoHotkey V2.0

Library BenefitsMost of the reasons for enclosing variables with percent signs for evaluation disappear in AutoHotkey V2.0. While the method still exists, it no longer works in the same manner—except in pseudo-arrays (e.g. Array%A_Index%) and two-deep variables (double-deref). In V2.0, AutoHotkey looks for a variable name %expr% rather than a replacement value %var%. As a general rule within V2.0 functions conversion, we should remove the percent signs and concatenate the strings of text for evaluation.

For example, in the last blog, we converted a V1.1 Gui command line which included a couple of %var% replacements in the options parameter:

Gui, Add, Edit, w200 %x% %y% vFirstName ym,

which for V2.0 the RegEx converted it to:

Gui.Add("Edit","w200 %x% %y% vFirstName ym","")

In the AutoHotkey V2.0 function, as it stands above, the percent signs turn into mere-mortal plain text percent signs and generate an options error. The options parameter in the Gui.Add() function must use a concatenated expression to pass the string of mixed text and variables:

Gui.Add("Edit","w200 " . x . " " . y . " vFirstName ym","")

or

Gui.Add("Edit","w200 " x " " y " vFirstName ym","")

In the ultimate result, GUI options require at least one space between the parameters. The order of appearance of the option items makes no difference.

AutoHotkey concatenation allows the dropping of the dots between text and variables as long as it does not cause ambiguity.

From the online documentation:

Concatenate. A period (dot) with at least one space or tab on each side is used to combine two items into a single string. You may also omit the period to achieve the same result (except where ambiguous such as -y, or when the item on the right side has a leading ++ or –). When the dot is omitted, there must be at least one space or tab between the items to be merged.

You might also assign the new string expression to a variable inserting that same variable in the function:

GuiOptions := "w200 " . x . y . " vFirstName ym"
Gui.Add("Edit", GuiOptions, "")

Manually replacing all the percent signs with quotation marks and dots gets a little tedious. Perhaps a RegEx will help speed up the process.

A Regular Expression for Fixing the %Var% Problem

Cleaning up deprecated %var% replacements when translating V1.1 into V2.0 with a new RegEx requires a second pass. (Maybe you can find a Regex which can do it all in one pass, but that gets way too complicated for me. Sometimes it’s just easier to loop through the file again.) Using the RegExReplace tab of Ryan’s RegEx Tester, the following codes remove percent signs, then replacing them with double quotes and concatenating dots.

V2 RegEx Percent Convert GUI
This RegEx locates percent signs % enclosing variables, then replaces each with concatenation dots and double quotation marks.

Even though not required for the evaluation of the concatenated string, I make a point of adding the dots to the Replacement Text. This saves concerns about possible ambiguous operators in the expression—although, in this situation, I’m not sure how they would get there.

The Regular Expression:

%(.+?)%

converts all variables found between percent signs with the Replacement Text:

" . $1 . "

as shown in the image above. This RegExReplace maintains all of the required spacing without adding any extras. If one of the %var% expressions appear at the beginning or end of the options string, the RegEx adds an extra set of quotes and dots (e.g. "". or ."", respectively). They don’t impact the evaluation of the concatenated expression, but, if you like, you can either delete those extras by hand or run more AutoHotkey replacements to remove them.

For more information on how Regular Expressions work, see the “Regular Expressions (RegEx) – Quick Reference” in the AutoHotkey online documentation or my book Beginner’s Guide to Using Regular Expressions in AutoHotkey.

Tip: If you use the above code with the V1.1 RegExReplace() function, ensure you escape each double quote in any text with another double quote:

NewStr := RegExReplace(Haystack, "%(.+?)%" , """ . $1 . """)   ; V1.1

Note: Escaping double quotation marks in V2.0 no longer uses the awkward double, double quote found in V1.1 (""). You can either use single-quotes to enclose any strings containing double quotes (e.g. 'He said, "Yes."'):

RegExReplace('Gui.Add("Edit","w200 %x% %y% vFirstName ym","")'
               , "%(.+?)%" , '" . $1 . "')    ; V2.0

or, escape the internal double quote marks with the standard escape tick ` character (e.g. "He said, `"Yes.`""):

RegExReplace("Gui.Add(`"Edit`",`"w200 %x% %y% vFirstName ym`"
              ,`"`")", "%(.+?)%" , "`" . $1 .`"") ; V2.0

These two techniques apply to escaping double quotes in all AutoHotkey V2.0 function parameters.

You might discover a problem when using the V1.1 single percent sign % forced expression operator. Since virtually everything in V2.0 already uses expressions, you don’t need to force expressions in V2.0. Merely delete the single % character before running the above RegEx. Otherwise, the %var% RegEx replacements get thrown off.

Note: I see six new alpha versions releases of AutoHotkey V2.0 occurred in January. That may be an indicator that things are picking up toward a beta release and eventually a general release. For information on how to test AutoHotkey V2.0 for yourself, see “How to Fool Around with the New AutoHotkey Version 2.0.”

jack

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s