Use of %user_input% vs %variable%user_input%

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