Reusable Variables

In text expander, I am used to creating a snippet and then using this snippet within other snippets.

For example, I can create a snippet “Supervisor” with a drop down option selection menu and be presented with choices of supervisors. I can later on create another text snippet and refer back to the original Supervisor snippet instead of creating the same “Supervisor” snippet in a new piece of text.

In KM, I use Variable “Supervisor” with User Prompt where I have an option of Supervisor1|Supervisor2|Supervisor3

Can I re-use the same Variable in other macros or I have to copy the whole Supervisor Variable every time I want to have a drop down list?

Yes, the variables are global. Open KM Editor > Preferences > Variables: All the variables you see there are available to all macros.

OK. What I can't figure out is how to use a pop-up list menu reusable in another macro?

Assuming I created a macro with user input prompt and Supervisor variable, how do I use the same variable with pop-up menu in another macro?

Put a pipe before the variable:

|%Variable%Supervisor%

See also this KM Wiki article.

I make it work but I am confused about variable creation even after looking at the wiki.

If create a new Macro and simply set it "Set variable Apple to A|B|C" the macro has no effect on the variable content.

For example, User Prompt does not give me any options for A, B, or C.

However, if I go to preferences -> variables and set the variable Apple to A|B|C it works.

So, what is the proper way to create a new variable?

[quote=“slyfox, post:5, topic:6729”]
If create a new Macro and simply set it “Set variable Apple to A|B|C” the macro has no effect on the variable content.
However, if I go to preferences -> variables and set the variable Apple to A|B|C it works.
So, what is the proper way to create a new variable?[/quote]

Telling from your screenshot #1, it should work. Maybe you just forgot to run the action?

Your screenshot #2:

You are saving the result of the prompt (A, B or C) to the same variable that holds the prompt options (A|B|C). That is, after the prompt the value of the variable “Apple” will be A, B or C and you will no longer be able to use it to populate another prompt in another macro.

Before the prompt: Apple == A|B|C
After the prompt: Apple == A (if you have chosen A as answer)

So, to keep your variable “Apple” intact (as the one that holds the three prompt options) you have to save the result of the prompt to a different variable. See the screenshot of my last post.

Unless I have misunderstood what you are trying to achieve.

There are several problems with your macro:

  1. You must initialize a Variable before you use it
  • The Variable for the item selected in the Prompt must be different than the Variable for the item list

See if this macro helps make things clear.
If not, feel free to post your questions.

##example Output

##Macro Library

1 Like

What I am confused about is the following:

If I create a variable without any triggers and that exists by itself where
Set Apple to A|B|C" variable is set, can I then refer to this variable in other macros?

From what I see, if I create a variable in Macro A, I can sue it as much as I like within Macro A. However, when I create a completely new Macro set B, I can no longer refer back to the variable that exists by itself or within Macro A.

Thus, I cannot create a global variable Apple = A|B|C and then refer to it while creating Macros in the future. Is this correct?

You probably mean “a macro without any triggers”, right?

As your screenshot shows, you have already set up the action to define the variable.

Now you have three ways to define the variable:

  • Run the macro that contains the variable-defining action (at least once)
  • Right-click the action where you have defined your variable and select Try from the contextual menu.
  • Go to KM Editor > Preferences > Variables and define the variable there

Since the value of the variable is not meant to be changed, it doesn’t matter which option you choose.

Yes, you can. As said, all variables are global. But please see my last post: If you save the result of the prompt to the same variable that contains the prompt options, then that variable will be set to the result of the prompt (and it no longer contains the prompt options A|B|C).

So, use a different variable to save the result to. Try to build it like in my screenshot above.

I think there is a whole lot of confusion going on here, including in what you are trying to do.

Variables are global in Keyboard Maestro. So if you set a variable “Apple” to something, all macros see that value. You can look at the current value in the Keyboard Maestro Preferences Variables pane.

Setting a variable to “A|B|C” does nothing other than set the variable to “A|B|C”.

You use a Prompt For User Input action to ask the user (you) what value to use. If you set it’s initial value in the Prompt For User Input dialog to “A|B|C”, then Keyboard Maestro will prompt you with a popup menu to select among the alternatives and set the Apple variable to “B” for example.

If you want the Prompt to default to the current value, then you use “%Variable%Apple%|A|B|C”. That is because the first value is the preset value, and Keyboard Maestro reads that from the Apple variable (that is what the %Variable%Apple% token does).

If you want to reuse that Prompt, which I think is what you were asking for from the start, then you create a new macro, call it say “Prompt For Apple”, put it somewhere that is available (create a new Macro Group called Submacros for example). Anywhere you want to use that Prompt, you use the Execute a Macro action and select the Prompt For Apple macro.

@slyfox:

But you probably do NOT want to use the same Variable as the Prompt Variable and as the Prompt default list. Else, as soon as you make a choice in the Prompt, you have lost the original default list.

This, that is why I use "TEST__SelectedItem" for the Prompt Variable, and "TEST__PopupList" for the Prompt Default Value:

If you want to use the Variable "Apple" in multiple macros as the popup list of "A|B|C", then you do NOT want to "Apple" as the Prompt Variable.

Make sense?