IF THEN vs Switch

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.

2 Likes

Like @noisneil, I too use them when there are multiple options that dictate what I want the macro to at a certain point.

A simple example is a user prompt with multiple buttons. The switch action can do different things depending on what option was selected from the user input prompt.

See Example Action Screenshot Here (click to expand/collapse)

3 Likes

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?

Exactly.
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.

After looking at @cdthomer example, I can see how Otherwise can be used, as the last condition. I was looking at it as the first condition and it wasn't making much sense.

1 Like

Why do you label your buttons Option 1/1?
Why the / and then the extra number?

It’s just an example. In real life, I never label them that way.

The forward slash allows you to assign a key to a button. So in this case, the /1 means the 1 key on the keyboard will select that option.

In the following screenshot, the Continue button is assigned the `return` key (although that is by default), and the Cancel button is assigned the `escape` key. (click to expand/collapse)

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)

1 Like

Interesting, that thing about the assigned keys. Didn't know that and it seems like a good feature. Thanks for sharing!

So whatever is after the slash (including the slash) is invisible and it assigns that button to a key.
On the first example, though, it's a bit different. Can you clarify?

I think you can read about the details of these options on this page:

https://wiki.keyboardmaestro.com/action/Prompt_for_User_Input

3 Likes

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.

2 Likes

Worth pointing out that you do need to if you edit them. For example, if you add another button and want it to Cancel/Continue instead of the default one, you'll need to assign /. or / respectively.

3 Likes

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.

2 Likes

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.

Here's the macro:

One Handed Key Combination Reassignments.kmmacros (11 KB) (v10.2)

[BTW, what's the trick to get this image to collapse/expand?]
Keyboard Maestro Export

Adding a new entry is simple:

  1. First add the new Hotkey Trigger.
  2. Enable the Display Text action at the top.
  3. Type the trigger key.
  4. See the text display window.
  5. Copy the text in the window.
  6. Close the window.
  7. Disable the Display Text action at the top.
  8. Add a new Switch option at the bottom.
  9. Change the condition to "is".
  10. Paste in the copied string.
  11. Add a Type a Keystroke action.
  12. Type the desired keystroke or select it from the dropdown list.

Done

Hi, @iamdannywyatt. Others have made great points above comparing Switch/Case to If Then Else. I'd like to add two points:

  1. If a Switch/Case can be used, it will likely be faster.

  2. 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.

Download: Switch-Case vs If Then Else.kmmacros (14 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 14.2 (23C5030f) PRE-RELEASE SEED SOFTWARE
  • Keyboard Maestro v11.0.1

2 Likes

I always use Switch/Case (and then with groups in each switch) for my Prompt With List actions, because it makes them clean and easy to read:

-rob.

5 Likes

:point_up_2: This is a good example of the readability factor I was trying to describe. :+1:t3:

2 Likes

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)

Screenshot 2023-11-15 at 12.10.39 PM

I write my code something like this:

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.

3 Likes

For all you Switch/Case experts:

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.

The first.

From the Switch/Case wiki page...