Use of %user_input% vs %variable%user_input%

Hello,

Let's say that i have a variable called user_input.

I am trying to understand when I simply use %user_input% vs %variable%user_input% in actions.

Thank you very much

Hi @ronald - the direct answer to your question is you can use %user_input% and %Variable%user_input% interchangeably. In other words, they both produce the exact same result: the value of the variable in question. (IMPORTANT: you must capitalise the "V" in "Variable".)

The alternative answer is this: you should never use %user_input% to get the value of your variable. You should always use the form %Variable%user_input% instead.

Why is that?

Well, as you know KM provides many in-built text Tokens such as %SafariURL% and %TriggerValue% and %UserName% - see the whole list of them in the KM wiki

Because of this, it would be easy to confuse your use of the variable token %user_input% with a KM text token - simply because of the way they look similar. Several KM experts in this forum have said that it is always best to keep potential confusion at a minimum and make sure that when you want to get the value of one of your variables you use the full form which is %Variable%user_input%

It is up to you which form you use, though. Personally, I always use the longer form, %Variable%user_input% even though it means more typing!

I hope that clarifies things for you a bit.

6 Likes

You could make a macro to insert %Variable%% for you automatically. :wink::laughing:

I actually did this with Typinator. It puts in %Variable%local__% and places the cursor after the underscores so I never have to type all that out. :sweat_smile:

2 Likes

I don't think you'd be the first to do that :wink:

3 Likes

No doubt. I can’t remember but I probably got the idea from somebody here. :joy:

1 Like

@tiffle @cdthomer

Thank you very much @tiffle for your crystal clear and educational reply. I will always use the longer form.

Thank you @cdthomer for your suggestion. I tried Typinator and it works, but would prefer to use to use a KM macro (with its keyboard shortcut) instead.

The objective is to convert selected text (namely the variable itself) to the longer format.

I was wondering if either of you would know how I can fix this macro.

If I type user_input → select text → run the macro, I get a blank, clearly because there is something wrong with the format of %Variable%%SystemClipboard%%, most likely because there are so many % that the KM engine gets confused.

Thanks so much !!

Hi @ronald. Like @tiffle I always use the longer form:

%Variable%local_MyVar%

Also, during development and debugging I often use the following in Display Text (display text in a window) actions:

local_MyVar : [%Variable%local_MyVar%]

and

local_MyVar
⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯
%Variable%local_MyVar%

I use a KM macro to do all three. The steps are simple:

  • select local_MyVar.
  • press Hyper+v, Hyper+w, or Hyper+x, respectively.

(For those not familiar with Hyper, that's short for ⌃⌥⇧⌘. Many people using Karabiner-Elements to remap their Caps Lock key to ⌃⌥⇧⌘. I think I first heard about this from Brett Terpstra: A Hyper Key with Karabiner Elements, full instructions. All Mac automators need to check out Brett's site.)

If you are interested in this macro, it's available here: Augment or Filter Text

As the name implies, it's more general purpose (and can be configured), but the three expansions mentioned above are configured out of the box.

This macro may be of no use for anyone that has Typinator or TextExpander, but for those that don't, it can save a lot of keystrokes.


BTW, this topic came up during a recent Automators podcast. I wrote about it here and here.

1 Like

Hi @cdthomer. Based on information from you and others on this forum, I decided it was time to check out Typinator. Also, it turns out I had an ancient license (from a software bundle purchased years ago), so I was able to purchase an upgrade at a discount.

I've not given it much time, but I must say that the interface is unique. What's the best way to get started?

1 Like

Hi @ronald - your macro to expand is almost correct. Here's the working version:

Test Expand to Variable.kmmacros (1.8 KB)
Keyboard Maestro Export

In the text
%%Variable%%%SystemClipboard%%%
KM interprets %% literally as a single percent sign % and does not treat it as the beginning of a text token.

EDIT: I used the KM Copy action instead of the Cut action - you can stick with Cut or use Copy - both work.

1 Like

Hey Jim, that's great! For a little more background on my Typinator experience, I downloaded it, used it very little for a number of months and then phased it out as I built my Keyboard Maestro library. But then as my Keyboard Maestro library started to become too large I realized I needed to transition back to a dedicated text expander app for a lot of my simpler macros.

As for how to get started with it, besides downloading my awesome macros?! Haha, just kidding, forgive my shameless self-promotion. :laughing:

One thing I really appreciate about Typinator is the great user guide which outlines a wide variety of things you can do with it. Some of my most commonly used expansions are used to input my email address, phone number, favorite emojis etc.

Lately I have been converting a lot of my work-related expansions into RegEx expansions which allows me to expand them using a variety of abbreviations, much like Keyboard Maestro does with it's typed string trigger. This is extremely helpful for complex medical terms that I might misspell when taking notes.

That should get you started and of course I can share more things I do with it if you'd like.

2 Likes

Just while we're on the subject of good standard practice -

I always put the two underscores before the name part of the Variable:

LOCAL__Name of Variable

%Variable%LOCAL__Name of Variable%

Or

VAR__Name of Variable

%Variable%VAR__Name of Variable%

Or

CALENDAR__Name of Variable

%Variable%CALENDAR__Name of Variable%

This way a user friendly version is displayed in User Prompts:

image

3 Likes

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