Use of %user_input% vs %variable%user_input%

I discovered this gem a couple of months ago and have slowly been converting my variables to this format. :+1:t2:

2 Likes

Great ! Thanks very much.

1 Like

Sorry I am confused...

  • When you write %Variable%Local__Name of Variable% , the actual variable is "Local__Name of Variable", not "Name of Variable" isn't it?

  • I don't understand the rationale of double underscores how it improves the user input display of the variable.

Sorry to test your patience and thank you.

The variables that are prepended with double underscores omit anything behind those underscores when they are displayed in a prompt. So even though the variable is still Local__Name of Variable, the only portion that is visible in the prompt is Name of Variable which makes the prompts look nicer.

2 Likes

Now i understand. Thanks very much.

I wrote to the developer of Typinator (Gue, very nice). The longer version of the variable (using %variable% as a prefix) can be created by text expansion, simply by putting the copying the variable to the clipboard and using the following as an expansion:

%Variable%{clip}%

2 Likes

Very interesting. Thank you. I have karabiner elements.

You’re welcome, and I like it! I've talked with Gue a number of times and yes, he is very friendly and helpful.

1 Like

All of this inspired me. In Typinator \b(\w+)//v%Variable%varName% where "varName" is the last typed word. No need to copy or even select. You could even have it add the "local__" if you want.

Bummer is: I know so much more now than when I started with Keyboard Maestro (heh, just typed 'kbm') that I need to go back and refactor every macro.

1 Like

Hi @Zabobon, thanks for sharing your naming conventions.

I use a lowercase local with two underscores for variables that are used in Prompt For User Input dialogs since, when rendering a dialog, the Keyboard Maestro engine automatically excludes any prefix that includes two underscores.

But for all other local variables I use one underscore. That helps me easily identify the two groups. I find that the underscores also serve to make the variables easier to read.

For global variables and dictionaries, I also have conventions:

Naming Global Variables and Dictionaries

For persistent data, I've generally moved away from storing persistent information in global variables in favor of dictionaries. However, when I do use a global variable, I name them as follows:

jsXXX_YYYY where: 1) js are my initials; 2) XXX is a short acronym or abbreviation for the macro; 3) YYYY is a description for the variable. For example, suppose this macro was mine, I would name the global variable: jsSEP_SearchWebsites

For global variables used in Prompt For User Input dialogs, since, when rendering a dialog, the Keyboard Maestro engine automatically excludes any prefix that includes two underscores.

This convention provides the following:

  • It's highly unlikely that my global variables will collide with variables from another macro.

  • In the Keyboard Maestro Preferences > Variables all of my global variables are grouped, first by me (js), then by macro. This grouping makes it very easy for me to clean up the global variables if I decide to retire a macro (or upgrade one by moving from global variables to a single dictionary).

  • Since dictionaries are also global (even if they are harder to see—since they are not in the Preferences), I use a similar naming convention. For example for this macro, I'd use: jsSEP_dict and have a key named SearchWebsites.

1 Like

I know that feeling...

1 Like

Hello @devoy,

Thank you for your very interesting suggestion. If I can make it work, it seems like the most efficient method in terms of workflow.

It does not work and I am trying to figure out what I am doing wrong.

  1. I created the typinator abbreviation varxx and expansion \b(\w+)//v

  2. I typed a variable Input__File_Path (note 2 underscores between input and file)

  3. I typed varxx which expanded to \b(\w+)//v , not to %Variable% Input__File_Path%

Your explanations are very good. It's just that there must be something I don't understand. I suspect that typinator may not understand it is a regular expression, but I can't find where to set regular expression for the expansion.

Thanks very much

image

Another reason I like to use this naming convention (some kind of acronym or Local or Instance followed by two underscores) is that it make bulk find and replace of Variable names in an existing Macro quite straightforward.

I still use this Macro by @JMichaelTX almost daily. He said it was a BETA but I have never had a problem with it.

He said it was designed to change Variable pre-fixes but in fact it can be used to bulk change the whole Variable name every time it is quoted throughout a complete Macro. As it is simply looking for text matches it works well (and safely) if the Variable names it is looking for are completely unique.

Example:

Change all references to a Variable named:

TEMP__Date

to

LOCAL__Week Day

In the above example I might have used a Global Variable when I was writing the Macro and then wanted to change to a local variable with a slightly different name. @JMichaelTX's Macro will search and replace every single instance of TEMP__ Date and change it to LOCAL__Week Day even if they are quoted as %Variable%TEMP__Date% or even if they are mentioned in a comment. Basically, TEMP__ Date gets universally changed to LOCAL__Week Day

But if I had called my initial Variable simply:

Date

Then the search and replace could completely mess up the code of the Macro as it would find and replace any text of "Date" (which could include a vital bit of Keyboard Maestro code),

The Macro first makes a safety copy of the Macro you are working on so, it is always possible to undo if things go wrong.

When I had a look back at the original post that I got this Macro from (and linked to above) I noticed that the link to download the Macro seems to be broken so, I have re-uploaded the original Macro at the end of the thread.

2 Likes

Hey @Ronald, the \b(\w+)//v is a RegEx (Regular Expression) which should be placed in the abbreviation.

In @devoy's case, it looks for any word that stands alone, and when he types //v then it encloses that word with whatever he want which I imagine would be %Variable%%.

Try this in the abbreviation line: \b(\w+)//v
Try this in the expansion line: %Variable%$1%
Make sure it is part of a RegEx set.
Type out a word you want to change to a variable, then type //v in front of it.
Watch the magic happen.

If that doesn’t make sense or doesn’t work for some reason, let us know and we can help further.

-Chris

EDIT: I just saw where you mentioned you weren't sure if Typinator recognized it as a RegEx... it needs to be placed in a RegEx set by you... see the screenshot below to see what I'm talking about.

Screenshot (click to expand/collapse)

Also, see the two smaller screenshots to see how I use Typinator to create variables; no RegEx needed. They're regular abbreviations that create either %Variable%% or %Variable%local__% and then place the cursor where it needs to be to type the variable name. Just another method, neither better nor worse than @devoy's, just different.

My variable abbreviations screenshot (click to expand/collapse)

Screen Shot 2022-03-02 at 07.18.28
Screen Shot 2022-03-02 at 07.18.36

1 Like

Yes, I've used that macro a few times myself, but I've always been concerned that something would break and I might not discover the problem immediately. Thus I've always saved the backups. Also, I'm concerned that this is one of those macros that could break when KM gets upgraded.

Lately I've mostly moved to using Dictionaries rather than global variables.

Near the top of the macro I set the dictionary name...

Then throughout the macro when I'm reading or writing...

This technique makes it easy to change the name which I need to do if I'm running multiple copies of a macro that differ only be persistent settings.

These images are from a macro that I used to share this approach: Dialog with Dynamic Popup List.

Hum, I just noticed that here's a case where I'm not using the full %Variable%local_DICT% notation, but for some reason it seems appropriate in this context. Other might disagree. :grinning:

1 Like

now I understand. thanks very much Chris. I have to be in a regex set !

1 Like

very interesting. I was not aware of Dictionaries and will look into it.

@cdthomer's got it. (Thanks for the asist!)

1 Like

I see it @_jims. But you've translated the problem. Using dictionaries in this way gives you an easy way to modify what-is-essentially-the-variable-prefix, (which is what JMichael's macro does for us) but doesn't help you if you need to modify the variable name.

I suspect @Zabobon was suggesting that JMichael's macro could be extended to search/replace any part of the variable name in the macro's XML or JSON representation. That's a guess, but I'm about to download it to find out!

2 Likes

This is a discussion about Variable naming and @_jims is just saying that he likes to use Dictionaries instead of Global Variables. It’s my fault for complicating things by starting to talk about renaming :slightly_smiling_face:

The good thing is that when these threads go off-topic I always learn new things!

And I really like @_jims’ use of Dictionaries.

Yes, it works that way and I use it for that all the time.

1 Like