%PromptButton% Token Not Working

What am I doing wrong? I also tried with If actions and got the same result. I think I'm following token:PromptButton [Keyboard Maestro Wiki] correctly.

I also checked the advice from here %PromptButton% Is Always Empty - #12 by macdevign_mac . I don't have a "PromptButton" variable.

Switch-Case.kmactions (1.9 KB)

Solved it, but I would suggest an edit to the Wiki so it makes more sense.

This post About prompt user input - #2 by kcwhat gave me the answer: test if Text %PromptButton% contains the button name.

@peternlewis Is that something obvious to everyone except me--that a Token should be evaluated as a Text condition rather than a variable condition? If other people have the problem, I humbly suggest editing the wiki entry on %PromptButton% to explain that, preferably with an example If action.

1 Like

I will agree that this is one of those things that takes a while to understand. However, it actually is mentioned in the Wiki, right near the top (emphasis mine):

Tokens

Keyboard Maestro provides two means to get information about objects (like web pages and windows) in the user's environment:

  1. Tokens (returns text, used in text fields)

  2. Functions (returns numbers, used in numeric fields )

You can enter a Token in any text field in an Action, and when the Macro is triggered and the Action is executed, the token will be replaced by the value that is returned at run time. This allows you to easily combine static text with dynamic data that is supplied at run time, in most any Action that has a text field, like Display Text and Set Variable to Text.

But no, it is NOT obvious to everyone but you. It takes a while to sink in.

With that said, there are visual cues. In a variable field, there's a "V" in the upper right:
image

And if you click the drop down, it shows variable names:
image

In a text field, it shows a "T" in the upper right:
image

and if you click the drop down, it shows tokens:
image

Mind you, I'm not saying this should have been obvious to you. But it might be helpful in the future.

4 Likes

Hopefully. Hopefully this is a basic fact of how Keyboard Maestro works, that tokens are Tokens and variables are Variables, and that they are not the same thing.

But even if this is not known to people (and obviously, it is something that people have to learn as they use Keyboard Maestro, just like every other facility in Keyboard Maestro has to be learned at some point), this would not be the place to try to teach them, because then this would have to be the same thing taught in every single reference to tokens all across the wiki. There isn't anything special about the PromptButton token in this regard.

2 Likes

If you make a Variable and say call it "MyBrilliantVariable" you can actually access it in a Text Field by writing it as %MyBrilliantVariable%. In other words the percent signs are what allows Keyboard Maestro to know that either a Variable or a Token is what you want when entered in a field that would normally just expect actual text.

Which means if something has a % each side it is to be used in a Text Field. (That's my way of remembering it anyway.)

So, both of these would work:

image

If the "thing" you are trying to use has percent signs each side of it, it can be used in a text field.

So... how do we know if %PromptButton" is a Token or a Variable (since it is perfectly possible for you to make your own Variable called "PromptButton" and enter it as %PromptButton% ?
The answer is, you can't know the difference - which leads to one of the best bits of advice I was given on the Forum which is to always quote your Variables with the notation %Variable% at the front of their name so that you can see at a glance what are Variables and what are Keyboard Maestro Tokens when looking through or debugging your Macros.

%Variable%MyBrilliantVariable% and %MyBrilliantVariable% will both work but the former is a clearer way of working and allowing you to tell the difference.

This is the better way:

image

And... it's a very good practice to have some standard naming convention. I've copied a convention used by many here to put a capital letter acronym followed by two underscores in front of all my Variable names.

VAR__MyBrilliantVariable instead of MyBrilliantVariable (where VAR could be anything that means something to me). In the context of a Macro to work in Calendar I might call a Variable CALENDAR__MyBrilliantVariable. Or if I want a Variable that disappears once the Macro has run I would write is as LOCAL__MyBrilliantVariable.

And... why the two underscores? That is because in Prompt for User Input Fields, Keyboard Maestro hides anything before two underscores. Example:

image

Gets shown as:

image

Circling back to your initial confusion (which we have all had by the way) it was the % sign each side of %PromptButton% which was the clue to use it in a Text Field rather than the dedicated Variable Field. The dedicated Variable Field is there for you to use your own Variables without the % signs. The list of possible entries in the Dropdown List has all the various forms of data that can be used:
image

Notice that is has "Variable Condition" but there is no "Token Condition". There is also no "Dictionary Condition" and that is why you are left with using "Text Condition" and using the % signs to tell Keyboard Maestro that the "text" is something special like a Token or a Variable or a Dictionary Entry.

It took me years before I dipped into using Variables and Tokens and Dictionaries (and actually understanding what they were rather than copying and pasting bits of other people's Macros) but now the whole thing seems quite logical and simple :slightly_smiling_face:

As well as lots of excellent Posts explaining all this, and the Wiki etc I see that this has just been posted by @macdevign_mac in Tips and Tutorials:

4 Likes

@DanThomas Thank you very much for the time to put that tutorial together. I sincerely appreciate it.

1 Like

Thank you for all the great information, @Zabobon, and for your time explaining it so well.

I always use the notation in Variables (e.g. %Variable%MyBrilliantVariable%). I honestly didn't know KM would work without the %Variable% prefix.

I have a pretty good variable naming scheme, but I didn't know about the double underscore naming trick for User Input Field. Thanks for that.

Notice that is has "Variable Condition" but there is no "Token Condition". There is also no "Dictionary Condition" and that is why you are left with using "Text Condition" and using the % signs to tell Keyboard Maestro that the "text" is something special like a Token or a Variable or a Dictionary Entry.

That makes a lot of sense. It's also where my confusion came from because the token was offset with % but "Token" wasn't listed in the KM UI. Thus I thought "token" meant something akin to "built-in variable" and should be treated as a variable.

Thanks for pointing me to @macdevign_mac's tutorial!

4 Likes