Hi, is there a way to set “C” as the default after I select it from user input, rather than it going back to “A” every time the macro runs?
Store your "list" as a global variable in the form A|A|B|C
. Then, every time the user makes a selection and hits "OK", update the first item of the list to whatever they selected. The trick to it is adding a |
at the start of the field, before your variable token:
The first item is the default selection and KM is smart enough to realise it's a duplicate and not to be included at the top of the pop-up.
Your Variable "test" is a Global Variable, meaning it will still hold its last value after the Macro runs.
So, change you list of choices from
A|B|C
to
%Variable%test%|A|B|C
This will have the effect of making whatever the last value selected when the Macro was run, the default choice.
Thanks man it was so simple OMG
Nice...
Do watch for side effects -- obviously, if you change the value of test
during this (or any other) macro then that new value will be part of, and the default for, your pick list.
Calling a KM Variable something like test is not a good idea for a number of reasons. The main being that if you give a Variable a name that is too generic there is a chance (as @Nige_S is pointing out) that you will have used that same name for a Variable in another Macro and each time any Macro changes its value that will have effects on all the Macros that use it causing all kinds of problems.
In this sense KM is different to other programming environments, like for example AppleScript, where the Variables only last for a single run. KM's Variables are global by default and their contents are saved on your Mac for future use even between startups. (Which, by the way, is a great feature and allows you to do things like saving default values - as you wanted to do in your question.)
Another reason it's not a good idea to name a Variable something like test is that it makes it hard to spot your Variables when you are editing a Macro. Once you have many Actions that use Variables it is good to be able to quickly spot them at a glance. In this case "OK" "A|B|C" "test" "Cancel" are all similar in look - but only one of them is a user defined Variable.
And, you don't want to accidentally name you Variable using a name that Keyboard Maestro has used for one of its special Tokens or Functions (kind of placeholders for thing like dates etc). This is more likely to happen if you use a generic name like test or date for your Variable.
So, good practice when naming Global Variables is to come up with a name that is unique (even for a short test Macro) and a name that is easy to spot in the forest of text in all the Actions.
Keyboard Maestro has a neat feature with the names of Variables. Any text at the start of the Variable name that is followed by two underscores will be part of the Macro name but will not be shown in User Prompts.
So, you can name a Variable something like Counter__Test (if you were making a Macro that counts things). And although the variable is called Counter__Test in the Prompt it will appear to the user as Test.
Variables can also include spaces in their names which can make them nicer to read in prompts. i.e. you could make a Variable called COUNTER__Test Number
And since the text before the double underscore remains hidden to the user of the Macro, it can be written as Caps COUNTER__Test Number which makes spotting Variables in Actions easy when editing them. This -
Would display in the priompt as -
I haven't even mentioned Local Variables, which don't persist like Global Variables...
There is great subtlety to this Scoping through naming and beginning to understand it will give you a leap up in your use of Keyboard Maestro. Have a read of these two pages -
- This one on Prompts:
- And this one on Variables:
Lots of love for the above, but especially:
Taking it up a level -- there are two main reasons for using global variables:
- Data persistence -- saving a value for future executions of a macro
- Data sharing -- to make a value available to other macros
These aren't mutually exclusive. And how you handle a global within a macro will very much depend on what you are doing.
A good rule of thumb is to copy your global value into a local variable as soon as possible -- except when you always and only want to access the very latest value, regardless of how it has been updated.
Using the above dialog as an example, consider the following time line where the same macro is executed twice:
| instance1 launched
| dialog presented and "A" selected
| 'test' set to "A"
T some instance2 launched
i stuff dialog presented and "B" selected
m happens 'test' set to "B"
e here quick stuff happens
| that display value of 'test'
| takes
| time
↓ display value of 'test'
...and you can see that instance1
will display the value that was set in instance2
-- probably not what was wanted!
So I'd do something like this:
...making the chosen item Local
immediately, then updating the Global_defaultChoice
to match. Yes, it's some extra work for KM -- but actions and local variables are cheap compared to the time spent debugging "unexpected" values in variables.
Getting in before someone says "But Nige, it's a million-to-one chance I'd run the macro twice like that!":
- As Terry Pratchett was always reminding us, million-to-one chances crop up nine times out of ten
- This is about developing a general habit of "protecting" your variables through Scoping -- a habit that will serve you well in KM and in any other "programming" you might do
You shouldn't always and only use local variables. But it's good practice to default to a local then expand its scope if you need to.