How I simplify text entry of existing or new variable name (%Variable%...)

Something like this might already be up here somewhere, but it occurs to me that some of you might find this useful. This is a fairly simple macro that I wrote to ease the entry of variable names within the KM Editor. This should therefore be in the Keyboard Maestro macro group. A trigger of your choice (I use control-v) invokes a search box (Prompt with List) that searches a list of all KM Variables. Typing a few keystrokes will allow the user to quickly choose the variable of interest. Hitting return then types the variable notation for that variable. For example choosing ThisOne from the list results in a text entry of %Variable%ThisOne%. If no search keystrokes are entered in the search (i.e., if the default is used, which I have as NewVarName), then a Prompt for User Input dialog is invoked, allowing the user to type the new variable name. In the case of a new variable name, the variable notation is typed, plus the new variable's name is appended to the list of variables (and therefore will be searchable in future uses of the macro). Set up is very easy. Simply open the Variables window in KM (Preferences), select all of the variable names in the list, copy and paste into a txt document. Add NewVarName to the list (I have that at the top, but it doesn't really have to be). Be sure to add a carriage return (i.e. one blank line) at the bottom of the list. Then save the file as whatever you want it to be called (I use KMVariables.txt). Then open the macro in the KM Editor and choose your file in the three (yellow) actions of my macro that call for the file.

What I like about this is that:

  1. It avoids the errors that can result from using KM's autocomplete (and it will restore a type of autocomplete functionality to those who have disabled autocomplete).

  2. The list will remain current as you add new variables.

  3. It's no problem to edit the text file, because it won't harm anything.

  4. Even though the growing list could be sorted, the fact that it is not sorted is actually nice, because:

    a. the search functionality of Prompt with List doesn't care about sorting of the list, and
    b. keeping it unsorted gives you a record of the reverse oder in which you added variables (should that ever become useful).

Keyboard Maestro 8.2.4 “Variable-Name Input” Macro

Variable-Name Input.kmmacros (6.2 KB)

Hey @thoffman666,

Try Cmd-Ctrl-V in the Keyboard Maestro Editor.

-Chris

Chris,

The Cmd-Ctrl-V shortcut enters only the name of the variable, not %Variable%NameOfVariable%.

Ty

The effect is the same though, isn't it? %Variable%NameOfVariable% is identical to %NameOfVariable%

And is there a difference when clicking the small down-arrow next to the Variable entry field? There you can choose %NameOfVariable% from the dropdown.

It's true that %NameOfVariable% is identical (at least in effect) to %Variable%NameOfVariable%. However, the built-in shortcut doesn't enter either of these. It enters NameOfVariable, not %NameOfVariable%, and when NameOfVariable is entered into a text field without the % signs, it is treated as text, not as a variable. NameOfVariable = "NameOfVariable", whereas %NameOfVariable% = the value of the variable called NameOfVariable.

1 Like

Hey @thoffman666,

True – but the built-in function is live and finds any variable.

I have another macro that encloses the text in a variable wrapper.

I do wish Peter would allow Option-Return or Shift-Return to automatically emplace the wrapper though.

-Chris

Mine finds any variable. It finds all the variables that are listed in Preferences, and it even finds ones that have been deleted (allowing you to use them again after deletion), because the list is always updated.

It's not live, so it won't find anything created after you populated your text file.

If you want it to be live you can make it so with a little AppleScript.

set AppleScript's text item delimiters to linefeed
tell application "Keyboard Maestro Engine"
   set varNameList to (name of variables) as text
end tell

-Chris

It is live. It appends the list every time you enter a new variable name. Creating the list just loads the list (one time only) with the existing names. After that, all new names are appended.

I think the issue Chris @ccstone is referring to is that if you create variables outside of your macro, then your macro will not find these variables. Thus, it is "not live", meaning that it does not draw from the actual source of KM variables.

1 Like

That is correct. However,

  1. After having made the macro, I never use any other method of entering variables in token notation, so it's not an issue for me personally.

  2. If I stray and enter a new variable name some other way, nothing breaks. I can choose to enter it via my macro the next time, in which case it will be added, or I can choose to keep it off the list.

  3. I don't want the separate list (my txt file) to be identical to the live list, because I want variable names that have been deleted from KM to still be available for quick re-use via my list. It is sometimes useful to delete variables from the Preferences pane. In that case, they're still in my list, available for re-entry.

  4. If any user finds that not being live in your sense is problematic, than by all means those users should modify it or refrain from using it.

Thanks for noticing and clarifying what Chris meant, and thanks for your many helpful posts elsewhere.

Ty

I get that. The common use case where I would want access to ALL variables, is where I have entered ONLY the Variable name in an Action, like in a Set Variable to Text. So later, in the same macro, I might want to enter the token for that variable.

I can see that, and in such cases (i.e., when I'm entering variable names unadorned), I have gotten in the habit of entering the variable in a Variable text box, immediately selecting all (Cmd-a), copying (Cmd-c), and running my macro outside of a text box to simply paste it into the list. This applies only to NEW variables, of course, because existing variables are still accessible in unadorned form via Cmd-Ctrl-v. The steps of copying a newly unadorned name could easily be added as an option within my macro, of course. I think it makes more sense, however, to simply make a separate macro that selects the unadorned name and appends it to the list.

Now that I've rethunk it, I might duplicate the macro, give the second one a different but related trigger (like control-shift-v) and change Action 3 (Insert Text) to the unadorned version, and start using that for entry into Variable-only fields. That way, a new name still gets appended to the list.

OP, I think this is a great idea to create a macro for - to make it easier to type variable names.

Using Cmd-Ctrl-V would be even more powerful. I thought about it, but am not sure how to capture the output from Cmd-Ctrl-V selection and paste it as %Variable%OutputFrom-Cmd-Ctrl-V-Selection%.

That would require a macro to simply select the newly selected variable name (from cmd-ctrl-v), copy, then Insert by Typing %Variable% then paste (the copied name), then type %. That's no problem. However, one of the primary reasons I wrote the macro (i.e., one of the primary reasons I don't use cmd-ctrl-v) is that cmd-ctrl-v does not allow me to enter a new variable name. It allows me only to choose from existing names. My macro allows both.