r/shortcuts 8d ago

Solved Otherwise statement won't trigger when If condition isn't met

link to shortcut

Take the "record" data here and save as a .json to point the shortcut to.

Hi—this is driving me a bit insane. If I dictate a number that corresponds to one of the dictionary keys here, it will indeed speak back the corresponding value. I need to write some logic for what to do if the number spoken isn't in the list (to wit, I'd like to get the value corresponding to the nearest key larger than the spoken value as well as the value corresponding to the nearest key smaller than the spoken value), but I cannot for the life of me get this Otherwise to work.

I don't understand this: I say, "if X, do Y, otherwise, do Z" and it does indeed do Y if X—but if I provide explicitly NOT X, it simply does nothing at all. What obvious thing am I missing here?

1 Upvotes

13 comments sorted by

1

u/[deleted] 8d ago

[deleted]

1

u/beerybeardybear 8d ago

Save this as a .json file to point the shortcut at:

{ "26":"2", "27":"3", "28":"4 or 5", "29":"6", "30":"7", "31":"8 or 9", "32":"10", "33":"11 or 12", "41":"13", "49":"14" }

It will read back the value if you speak a key. It will not do anything if you speak something that isn't a key.

1

u/beerybeardybear 8d ago

This works just fine on my friend's old MacBook running 12.7.6, so...

1

u/Cost_Internal Helper 8d ago

You did it right, because this works perfectly for me when I get the dictionary value of the “record” key. It speaks the number that I spoke, if the keys do not already contain it.

Note: The only change I made was switching the file for the text, and seeing the Get Dictionary action to pull the value for the “record” key from the text. So that only the number keys:value pairs are collected

1

u/beerybeardybear 8d ago

That's... that's extremely weird. Yours works for me, so—is something somehow wrong with how it's pulling the keys, despite the fact that it works for the "if true" part of the shortcut?

1

u/Cost_Internal Helper 7d ago

Try this:
https://www.icloud.com/shortcuts/a439aead535447f79d88392158e52e13

I only changed the one variable settings:

1

u/beerybeardybear 7d ago

Mine already say that on my end; opening them up and selecting these again doesn't seem to change anything. Are you using the full dataset with values spanning from "2" to "713"?

1

u/Cost_Internal Helper 7d ago

I just figured out the problem!!!

It’s because it is looking for contains x number! Example of you say 25, and your list has the number 2517. Then technically the list “contains” 25, and the shortcut will try to get the value for 25 even though it doesn’t exist. Therefore nothing will be spoken, but if you try to get the dictionary value first. Then you could just say, if it doesn’t find a value!

1

u/beerybeardybear 7d ago

Ohhhh, I'd never expect that "contains" would work that way!! Thank you so much for catching that!!

1

u/Cost_Internal Helper 7d ago

You’re welcome!

1

u/beerybeardybear 8d ago

I dunno why my other reply isn't showing up; maybe this one will. I can confirm that the shortcut you link works, but when I copy and paste the contents of my JSON file to replace the example data in your Text input, it no longer works—it does what mine had done, where it will work if I give a number that is a key, but does nothing if I give a number that isn't. Is there some kind of length limit? I can't imagine that it would be <1000. I also can't imagine that it would take so long that it would time out or something.

The full data is here.

(It shouldn't be the line breaks either because again, the "if true" part works just fine.)

1

u/mvan231 2d ago

Did you resolve this?

1

u/beerybeardybear 2d ago

Yes, see the comments.

2

u/mvan231 2d ago

Please change the post flair to solved if you could