I work with a lot of variables and fields. To manage these better and work faster, I decided to use the shortest variable names possible that are still easily searchable by name (so no single-letter variables). I decided to begin with a0, a1, etc. and after a9 move to aa, ab, ac, etc., and after az move to b0, b1, etc. and so on.
Unfortunately, this doesn't seem to work correctly. All of these used with the shortcut variable writing method (e.g. '%a0%') produce symbols as an output instead of the variable contents.
I checked the KM documentation for variables and it said:
Anywhere an Action requires a Variable, you can enter any name you like (even though often a default name is provided), as long as it conforms to these rules:
Variable names must start with a letter, and then can contain letters, numbers, spaces, or underscores.
Some Variable kinds require a specific prefix (see Scope below).
Variable names are case insensitive, but their case is remembered.
Variable names should not include a function or operator name with spaces around it.
(eg “ MOD ” , so “A MOD B” would not be a valid variable, although “MODULE” would be fine).
I've named them per #1. I generally only work with global variables, so per #2 scope I don't need a specific prefix. #3 and #4 are irrelevant to the problem at hand.
Reading the rest of the documentation variables page, it suggests it's better to use the longform variable name (%Variable%a0%) which yes works but it also cumbersome when the point of using the shortest variable is to write as little as possible. Also, the documentation says this: ' You can also use a short form of just %Variable Name% to include variables as long as the variable exists and has a value and there is no corresponding text token, although generally it is better and clearer to use the longer form %Variable%Variable Name% .'
So the variable exists and has a value. The only other possible problems would be text tokens per the documentation, and I checked the complete list of text tokens and see none with these sort of names.
Will someone explain to me what is going on here? Thanks!
Macros are always disabled when imported into the Keyboard Maestro Editor.
The user must ensure the macro is enabled.
The user must also ensure the macro's parent macro-group is enabled.
Keyboard Maestro v11.0.2
I have it assigned to vv, and in a group that's only active when Keyboard Maestro is frontmost. I type vv, then my variable name, and I'm done. (And yea, I can't type "savvy" in Keyboard Maestro's editor, but I don't think I've ever ran into that issue :)).
If I could ask about the globals—do you have to keep these values around at all times? Or do you use globals just for the shorter typing requirement?
If you wish to use variables, you need to use the Variable token.
If you use the short form %varname%, then you need to ensure your variable cannot be confused with an existing token, which is impossible to do in general since Keyboard Maestro could add a token named anything at any point, so it really isn't recommended to use the short form. About the only exception I would make would be local variables with names like Local VarName because tokens wont have spaces in them and are unlikely to start with “Local”.
Thanks, that solves the mystery then. Since my goal is the shortest typing string possible when I want to insert a variable, perhaps I can find and use the two (or three) letter combinations that aren't hex codes and use them... even if it is a bit dangerous down the line because of possible new token names. I've used Keyboard Maestro for years and never used the longform version such as %Variable%a0%. Besides it being longer to type, it also takes up precious space on the visible part of regex fields such as on the search variable action. I will consider and keep in mind what you've said though and may change the way I approach this.
Thanks for the macro; I'm going to look into that. As for the globals, I have the same set of variable names around all the time that I re-use for all my macros. What I've done is create a set of 'erase these variables' macros (in which the macro contains a bunch of set variable to text actions for each pertinent variable setting each one to nothing i.e. emptying it) of which I insert as an execute macro action into the beginning of every macro that uses any of these variables. So, each pertinent variable is emptied before each macro performs any other actions. Thus the variables are constantly being emptied. When I need the values of variables to not be erased between macros or each time a macro runs, I give those variables unique names and generally leave them with their values except that every so often I'll go through the variables list and empty or delete ones that aren't needed anymore. All of this has worked well enough for me and what I've done for years, but I'm not sure if it's the most efficient approach or not.
I remember that when I first learned Keyboard Maestro (and regex in general) I just went with global variables for, yes, the shorter typing requirement. Since then I actually mostly forgot about the other types and just made the global variables work for me in every situation. Now I'm going to reconsider it all, but I know the shorter typing requirement will be one of my top variable priorities regardless.
You could modify my macro to insert %Variable%local_% and then position the cursor between the _ and %—you'd then only have to type four characters to use locals instead of globals. The advantage is you'd never need to clear them out, as they vanish after the macro runs. This also makes it much easier to debug any global variables that you do need, as they'll be easier to find in a smaller list :).
That definitely sounds intriguing. Thanks! And yes, I know what you mean about a long global variables list, lol. That's one reason I was going to change all the ones I'm constantly re-using to a0, a1, etc., so that then I could position all the reusable ones together at top and all the unique ones together at the bottom.
One thought about using local variables is that I often need to debug something in a macro after I've made it and it's hit some snag, and I often do this by looking at what's been put in each variable it's using. That's one reason I position the execute 'erase these variables' macro action at the start of each macro instead of the end, so that the variables will still have their contents to help me discern what to correct. So if local variables delete immediately after a macro runs then that could be a drawback.
Why does the insert text on the macro for the variable contain so many percent symbols with an l in the middle? I understand the macro except for this part. I would've supposed that since the variable form should be for example %Variable%a0% that the insert text would've been only %Variable% and then I'd have to write a0% after.
%Variable% - this part is pasted into the input box
So much for the easy part :). In a KM text box, %I% is a token for "put the cursor right here." But if I just insert %I% into the string, like this:
Then what comes out is this:
That's because KM just interprets what's between the % signs for the %Variable% section as pure text, it spits it out again. To make KM interpret it, you have to double the percentage signs, which leads to the final ugly form. Here it is with some spacing to make it clearer:
%Variable% %%I%% %
The end result is the text is pasted, and the token interpreted, so the cursor winds up between the final two percentage signs.
Thank you. That makes perfect sense and now I understand it. I'm going to play around with this and try it out. So if I'm deciphering this correctly, it means that if I'm writing a regex with variables, I'd type the hotkey (e.g. vv) then my variable name (e.g. a0), then the right arrow key to move past the last percent symbol.
Yes, that's exactly how it'd work. (I usually have the variable on the clipboard, so I hit vv-Command-V, then right arrow if needed. (The variable isn't always on the clipboard, though, which is why I don't have Paste in the macro.)