Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 1—Everything Functions)

As We Await the Ultimate Release of AutoHotkey V2.0, Let’s Look at How Things Will Change

As I review the documentation for the alpha version of AutoHotkey V2.0, I can see that 95% of the code in a V1.1 script needs to change to run under the new version. AutoHotkey V2.0 offers a more consistent scripting environment, but you will experience a slight learning curve. Standard Hotstrings offer the only syntax which continues untouched. (Hotkey syntax also stays intact but you will need to update the commands within any Hotkey routines.) That means your AutoCorrect script will likely run fine under both versions of AutoHotkey—unless your Hotstrings execute custom actions.

To get ahead of the curve and give you a chance to make better-informed decisions about ever upgrading to V2.0 (once released), I offer this series of observations comparing V2.0 with V1.1. Digging into V2.0 requires a slightly different way of thinking yet it remains all AutoHotkey. You’ll find the overall script structure and how AutoHotkey processes a file unchanged. Any understanding you already possess about how AutoHotkey works will serve you well. In these blogs, I focus on converting from a language running with commands to one which uses corresponding functions.

Commands Vs. Functions

JacksLibraryBookDeal250AutoHotkey V1.1 offers a fairly loose (and forgiving) command structure. Delimited with commas, parameters often represent text, sometimes variables, and in other instances, expressions. As long as you get all the commas in the right place, it works—and works well. However, you must refer to the documentation to ensure you include the right number of commas in the right place.

In AutoHotkey V1.1, if you want to substitute a variable value, you can insert the %var% operator. Or, you can force an expression with the % operator. You have the option to assign values to variables with either the single equal sign = operator or the colon equal sign combination := operator. V2.0 cleans up these ambiguities—although sacrificing V1.1 compatibility.

In AutoHotkey V2.0, all V1.1 commands turn into functions. Not only that but, rather than calling Label subroutines, they usually refer to other functions. (You can continue to call Labels with certain functions, such as GoSub and GoTo, however, the subroutines must include V2.0 functions.) This has two immediate implications: rewriting syntax to fit the function format and recognizing local variable isolation within functions.

Lose the Commas

In AutoHotkey V2.0, if not present, you can assume the existence of the parentheses associated with all functions—even though you might not see them. While some V2.0 functions look eerily similar to the matching V1.1 commands, they aren’t. For example, the V2.0 documentation appears the same for both Send commands:

Send Keys

But in AutoHotkey V2.0, you’ll discover the following as a more accurate representation:

Send(Expr)

You’ll find the parentheses optional with certain (not all) V2.0 function formats (e.g. when beginning a line of code). Outside of those parentheses (even if invisible), V2.0 prohibits the comma. You must follow the function name with either a space (for invisible parentheses) or the open parenthesis ( character—not both—which can cause unpredictable results.

Plus, the V2.0 function now uses expressions as input parameters. (Note: This explains the dropping of the forced expression operator (%). AutoHotkey V2.0 has no need to force an expression when everything already behaves as such.) That means you must enclose any plain text with quotation marks. Everything else gets evaluated:

Send("This is a test!")

or

Send "This is a test!"

but not:

Send, "This is a test!"      ; no comma allowed

Even sending Hotkeys modifiers requires quotes:

Send "^c"          ; CTRL+C to copy selected text
Send "^v"          ; CTRL+V to paste from Clipboard

When sending variable data, we merely insert the variable—no double percent signs required (e.g. don’t use %var%):

Test := "This is a test!"
Send Test

In fact, although you can continue using the double percent marks for evaluation, it takes the form %expr% rather than %var%. In most cases, the V2.0 expression looks for a variable name rather than variable contents. Referred to as a double-deref (or, as I call it, a two-deep variable), it may come in handy in special situations. If used as part of a pseudo-array (e.g. Var%A_Index%), the operator continues to work in V2.0 as it does in V1.1—merely appending its value (usually a number) to the variable name (e.g. var1, var2var3…).

In most cases, you won’t need to enclose anything with percent signs. For example, the following V1.1 code:

^#!F2::     ; Add today's date formatted
  FormatTime, TimeString, %A_Now%, MMMM d, yyyy
  SendInput %TimeString%
Return

becomes:

^#!F2::    ; Add today's date formatted
  TimeString := FormatTime(A_Now,"MMMM d, yyyy")
  SendInput TimeString
Return

In AutoHotkey V2.0 the FormatTime() function which corresponds to the FormatTime command in V1.1 requires the parentheses as used above. (Tip: You can only omit the parentheses when the function appears first on the code line.) While everything gets moved around when making the V2.0 format conversion, the function uses all the same pieces as V1.1.

AHK Format V2
In V2.0 the old OutVariable tuns into the assigned variable, the A_Now input variable loses the percent signs, and the built-in data variables get enclosed in quotes.

Other than the syntax changes, almost everything else about FormatTime (e.g. built-in variables) remains the same. You’ll find this true for most of the new V2.0 AutoHotkey functions.

Functions Use Local Variables

Another aspect of functions which affects your ability to convert your AutoHotkey V1.1 scripts to V2.0 involves their inherent protection which isolates variable names within a function—thus, allowing them to run locally without affecting other variables of the same name in the main script. Functions do not naturally see or use external variables. For the most part, you don’t encounter this issue in V1.1 because it heavily depends upon Label subroutines which don’t isolate local variables.

Since V2.0 uses functions, you will need to declare many variables Global making them available within the called functions. The easiest method for making outside variables operable inside a function involves simply placing the word Global inside the function. Then the function behaves in the same manner as Label subroutines in V1.1 treating all variables as global.

Test := "This is a test!"
Function()

Function()
{
   Global      ; Make outside variables available
   MsgBox Test
}

When calling functions which carry out actions, you’ll find it important to understand how local variables work. Next time, I convert a simple GUI pop-up to V2.0.

jack

2 thoughts on “Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 1—Everything Functions)

  1. Thank you so much for this writeup! I am just now returning to AHK after a year long hiatus. I couldn’t remember if I was using V1 or V2 previously.. but your post made me realize I was using V1, which is what I am going to stick with for now. I do appreciate your other post about Test Driving V2 (which is written at the perfect level of detail for me, so thank you!), because someday I want to have both versions installed on my machine, where they don’t interfere with each other, and wont hurt my existing scripts.

    Like

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