Just would like to learn a bit more about the Switch action (I read the Wiki page, but I'm curious to know if there are other differences).
Other than a more limited amount of conditions, the Switch seems to be like the IF THEN, but it's more strict.
For example, in the IF THEN, if I check a variable for a string containing "A" and add an action to be performed, then for the ELSE I add another action, if the variable does not contain "A", it will run the actions under ELSE, no matter the string found.
So basically the Switch (if I use 2 conditions) seems to be like having 2 IF THEN one after another, where it needs to match the first condition, without any ELSE actions. Right?
If so, other than that, is there any other useful applications for Switch, any creative things that can be done other than just this?
The equivalent is "otherwise" in a Switch/Case action.
I often use it rather than If/Else when there are more than two possibilities or I want the macro logic to be clearly readable. It's easier to see how a condition is being satisfied when it's written above the actions being run.
But in that case you need 2 conditions, right?
One that checks the variable/text/etc, then the "otherwise" after that.
Yes, because the IF THEN, is just "either A" or "every thing else that isn't A", which is where nested IF THEN comes into play (not always ideal). Now that I know about the Switch, I can see how the macros can be cleaner.
But isn't the IF THEN the same thing? Or am I missing something?
The IF THEN is just either A or everything that's not A, which sometimes is not ideal.
With Switch you can say "either A" or "B", but exclude "C" for example.
I think I just have Switch actions, because of some user here shared a macro with them, but I never used it myself, because I didn't know what it was. Today I was checking some other macros and decided to check what it does and it seems to be a great alternative for some IF THEN scenarios, especially with nested IF THEN (messy)
Thanks for the macro example. It makes perfect sense and I yeah, I have a macro that uses the PromptButton with a Switch. Someone shared it here.
In this example, the button (R)eveal is assigned to the `R` key, and (O)pen is assigned to the `O` key. Additionally, I put those letters in parenthesis just to visually indicate that those are the assigned keys in the actual prompt. (click to expand/collapse)
As @Airy said the Wiki entry is enlightening. Basically Continue and Cancel have buttons assigned by default, so there’s no need to explicitly set them. All other key/button combos must be explicitly set.
Potentially, yes, but it's often more readable If you can explicitly see the options on either side of the switch; like a reminder of what the conditions might be without having to scroll up and find out.
Coincidentally, just this evening I created my first Switch-based macro, without having seen this thread yet.
I had a situation where some repeated actions had me changing hand positions a lot, back and forth. I was pressing a function key to run a macro and then typing ⌃→ to move to the next Desktop Workspace, then pressing the function key again, and my left hand was doing a lot of work because my laptop doesn't have a Control key at right, near the arrow keys. It occurred to me that if I defined ⌥→ to be the ⌃→ "keystroke", then I could keep my hands in the same positions and not have to move my left hand back and forth between the Control key and the Function keys.
At first this was a one-trigger macro, but then I realized that if I did a Switch on the %TriggerValue%, I could let the same macro handle a bunch of key reassignments.
Matching the %TriggerValue% was a little iffy at first, but then I hit on the idea of just displaying it in a window. I copied the text out of the window, and pasted it in as the text to be matched.
If a Switch/Case can be used, it will likely be faster.
The Switch/Case is Keyboard Maestro is more powerful than in many other programming languages in that the condition can be more than just a simple check of a value. For example, the KM Switch/Case includes matches (matches the specified regular expression).
Here's a macro that demonstrates the speed difference.
I often use switch actions to implement state tables. For example, if I have four blocks of code that can branch to each other in a variety of ways, such as this... (assume the names of the state bubbles from top left to bottom right are A1, A2, A3 and A4)
This is a use for the Switch statement that I think makes my code clean and easy to understand. It's unfortunate that some tasks in life look like "spaghetti code", but a well written Switch action can really help (a lot) to make things clear.
If more than one option matches, is that an error, is the first one executed, or are all matches executed?
On a related note, can the otherwise option go anywhere in the list, like at the top, or must it be at the bottom? It would be useful for readability, sometimes, to have the otherwise option at the top, but that's not going to work, I think, if only the first match is executed.