New Switch/Case Action Concept

Just wanted to throw an idea out there for what I think is the missing flow control action in shortcut.

Switch/Case is a common programming concept where lots of conditions can be listed and the code block of the first one that matches is executed.

In this example, if the input doesn’t contain “Hello World” but does have a value, case 2 would be run.

Having something like this in Shortcuts (a mix of a Choose From Menu action and an If action) would greatly reduce the number of nested Ifs required and make menu creation much, much easier - you could lift, drop and rearrange all the routes with ease.

If the cases were collapsible it would be even better, but let’s not get ahead of ourselves…

Any thoughts?

5 Likes

Love it! The only thing I’m confused by is “collapsible” cases. Can you expand on that,

I just edited the photo to add chevrons on the right of the case bars. Like other actions that have a ‘Show More’ twirl down, these could be collapsed and uncollapsed to make complex shortcuts more manageable.

Oooh! Literally collapsible. I thought you meant like collapsing cases in to each other. (Instead of and/or conditions in a case have multiple cases execute the same logic)

1 Like

@mralexhay could you add to Toolbox something that is like this but orchestrating kicking off a different shortcut depending on the input value?

Doesn’t get all the way as context would be lost. Maybe you have this already.

Wouldn’t that just be using Run Shortcut actins instead of Comment actions looking at the screenshot above?

If that were the only action to be done on an input, you might well be able to take the input as a dictionary key and then use the value as the name of the shortcut to run.

1 Like

And my thought is most case usages are really at the beginning - as a palette. Mine are - kicked off by NFC tags.

I like the switch-case concept a lot.

But a try/except/finally may be better for error handling.

1 Like

I disagree. There is no concept of an error in Shortcuts. Doesn’t really make sense to add control flow logic for handling something that doesn’t exist as something the user can handle.

I get errors whenever I have my podcast shortcut try to play an Overcast playlist that doesn’t have any podcasts in it’s queue. I’d love to be able to surround that in a try/catch block. Not sure if it’s registered in Shortcuts as an exception in the typical programming language sense, but it would still be great.

1 Like

would be a great option to put in! Should save a lot of nested if-then’s

Ah! Completely agree. I definitely think there should be a story about errors in shortcuts.

Hi, @dustinknopoff. Errors do happen; but I am not sure how many exceptions are built-in. I imagine some things exist (like division-by-zero error). Also, see @motopascyyy’s comment below, where he mentions that attempting to play a podcast with an empty queue presents with an error. So error handling may not be well developed in Shortcuts – i.e. there are definitely no equivalents for specific error types like SyntaxError, TypeError, ValueError, ZeroDivisionError, etc. – but perhaps some kind of generic error exists (or could be easily implemented in Toolbox Pro).

1 Like

@cavalierex @motopascyyy I was maybe a bit too vehement in my disagreement with @motopascyyy. What I mean to say is that handling errors seems like a core concept that shortcuts should define rather than leaning on a 3rd party to handle. (And/or if it’s even possible for a 3rd party shortcut to handle)

1 Like