Switch Case

Dear all,

Cracking my head over this. Had a switch case block working fine up until the condition was not met. The loop exit but didn’t continue on. Initially, I thought it was because it was a subroutine and not able to return to the main marco, but I tried merging both marcos, but still, the entire marco stops when the condition is not met.
I am away from my computer now, but I will post some screenshots when I get back.

I appreciate anyone who can point me in the right direction.

Thanks!

Month Loop v2.kmmacros (27 KB)

Without seeing the entire macro, it's hard to know what you're trying to do or why, but why not just use a repeat block instead of an If/Else, with the repeat number set to 32?

Thanks for the quick response.

A repeat block will work for this use case however I want to see what was wrong with this as a while loop is an important block.
Anyway its not an if/else that fails. It’s the switch/case that was a while loop that stops the entire Marco after the variable “num” increment to be 32 which will not meet the condition.

It is a rather long Marco and I am not sure how I can screenshot the entire Marco.

In short I wanted the while loop exit to the last block in the screenshot and carry on… I read the wiki and seems to me pretty straight forward that the while loop will exit and continue but not for my case.

Yes it does and from your screenshot it looks like it should work. Therefore there’s something else going on that we can’t see.

As @noisneil suggests - post your macro not a screenshot.

This is how to do it:

1 Like

Thanks @tiffle

Pretty new to KM.. This is an awesome feature!
I edited it into my original post.

Any help greatly appreciated!

Since I’m not at my Mac all I can do is desk-analyse the image of your macro.

The flow of your macro is this:

  1. Set month to 2
  2. Perform the actions in the switch case since month < 13
    2.1. Do some stuff
    2.2. Set num to 1
    2.3. Perform while loop actions as long as num < 32, incrementing num by 1 each time
    2.3.1. Exit while loop when num reaches 32
    2.4. Set month to 3
    2.5. Exit switch case
  3. Nothing else to do

I’d say that describes exactly what you say is happening which is:

The condition not being met is num < 32

So I can’t see the issue I’m afraid.

It would really help if you explained what this macro is designed to do.

Having had a look through it...

  • Some of the logic can be simplified, although I can't test if these alterations work, as I don't know what you're trying to do.
  • The mouse clicks are using absolute position; I would suggest clicking relative to the front window corner, for reliability.

Month Loop - simplified.kmmacros (34 KB)

Macro screenshot

Dear all.

I found my own problem. It was the Switch block that should be a While block when checking month < 13.
One of those programmer thing that you are sure you checked it multiple times. Well… this started my registration in this awesome forum where responses are quick. Will spend some time here helping out when possible.

Thanks to @noisneil and @tiffle for responding so quickly.

1 Like

Take a look at KM's "For Each" action. For a "from n to m, incrementing by 1" loop it's a little more obvious than a "While" or "Until" action. So for your days you could, for example:

image

...and you won't need your incrementing action either. Whether that makes more sense to you will depend on how your head works!

In similar vein, you can zero-pad your months and days using KM's %CalculateFormat% token rather than your "If..." action. So:

image

...or even do that formatting at the point you use the number.

And I'll include my usual shout out for variable scoping. You could use local variables (and pass by parameter to your subroutine) or instance variables to avoid potential side effects, so long as you don't need the persistence of a global.

1 Like

I don't know if you saw the macro I uploaded above, but I can't see a reason not just use a Repeat action set to 31...?

Personally, I'd opt for the Format option under the gear menu, as I did in the macro above.

2 Likes

OP is "inserting" the incremented number each loop -- if you use a "Repeat n times" you'll also have to set the variable before the loop and increment it during the loop.

It's the way I'd normally do it too, but for the "special case" of a unchanging end-of-range and plus-one-per-loop, "For Each" may be more readable -- "For each number from 1 to 31" -- because all you need to know is right there, no scanning the rest of the block to find the incrementing action and the value(s) used.

I think it depends on what you're doing. If you aren't doing a calculation you'd have to add an extra Calculate + 0 action to do the formatting. And if you ever add more calculations you'll have to remember to include formatting on those, too.

My example was a bad one -- I should have either prefixed some text in there or done something else. But it's a handy token to know because you only usually care about leading padding when "converting" your number to text.

Ultimately, I think it's another "which way does your brain work?" choice. I'm used to the addition of leading 0s requiring a cast to text (AppleScript), so it feels wrong while I still want to treat it as a number.

@noisneil @Nige_S Thanks for the additional tips and tricks :rocket:

KM is a really interesting platform and definitely worth exploring more. I can see why there are so many fans of KM.

:heart:

2 Likes