How to create a counter?

Tried to read threads and the help to find it, but... need some help here.
I created a macro that repeats itself.
I want to create a counter that all the time after the 5th time, when the macro ends, before it starts again, it will do a specific action.
How do I do that? (I'd like to use "pure" KM because my knowledge of Apple Script is even smaller)... :slight_smile:
Thanks in advance for any help.

If you want us to modify your code so that it works, we would need to see your code. If you want us to tell you the best way to achieve what you are trying to achieve, that would most likely mean making changes to your macro to make it work using our recommended method. Which option do you prefer?

1 Like

Here's how I would implement what you are asking for, but there's no guarantee I'm understanding your problem correctly. Let me know why this doesn't work for you:



Hi sleepy.
Thanks for your reply. Sorry it took some time to get back, just needed to try and adjust it to my macro.
It worked and did exactly what I needed!
Very much appreciate your help!
Have a great day.

Wow. I'm not usually that good at helping. Thanks for telling me.


Well, you see? There will be always someone who can use your help... :blush:

OK, I think it's time for you to admit that you can, indeed, help people. :smile: I haven't been around much lately, but I've seen you help people several times. So quit being so surprised that you actually helped solve a problem. :stuck_out_tongue:

And it's awesome you're helping. If you think you can help someone, go for it. Even if it's only to get someone to clarify what they're asking. One of the things I hated doing, when I was a forum regular, was trying to figure out what someone was really asking. I just don't have the patience for that. So when someone else managed to get down to the actual question, even if they couldn't answer it, I was always thrilled.

So the bottom line is, I love that you're trying, and I'm 100% sure the other regulars appreciate it too. Don't stop. Especially when you experience the inevitable: someone else points out that your answer was either wrong, or way more complicated than it needed to be. Remember I said "inevitable". It WILL happen. And that's cool too, because how else do we learn? No shame there.

Even with all the experience I have with KM, this past couple of weeks while I've been active here again, I've learned a TON of things I never knew before. @JMichaelTX and others are such a wealth of information, and I don't know what this forum would be like without them. Or you.

And judging by how you responded to one of my other comments, I suspect the rewards of helping people solve their problems will be more than worth it to you.

Sp, keep it up, man! :+1:


Well said Dan (each and every word) :slightly_smiling_face:
And while we are at it, I’d like to add, from my personal experience.
I am using KM just thanks to Peter and the many wonderful members of this great forum, that are devoting their time and contributing their vast knowledge and experience to answer all my beginner questions (I don’t wanna say stupid because once I read that those are only the questions that were not asked)... :blush:
I’m still scratching the surface but I’m using it all the time.
So... thanks to all of you guys, I’m sure you are not thanked enough! :clap::heart:


That was generous of you to say. Thanks. I do enjoy it. In fact sometimes I come to this website sad because there aren't enough people needing help. However my interests blow with the wind and sometimes I might work on other things. So I still think of myself as more of an irregular than a regular.


Better an irregular than a not-at-all. :smile:


What is the purpose of using the While action?
Since you have used a calculation that will ALWAYS be true, what benefit does it provide?

To satisfy that requirement, all you need is the Repeat Action block, followed by whatever "specific" action you want done.

When I read the original post, "I created a macro that repeats itself." I thought he meant that the "5+1 action set" repeats itself indefinitely. Maybe I was wrong. But the individual said my idea worked so maybe I was right. Isn't he the final arbiter of whether I interpreted his problem correctly?

OK, if that was your objective, then I understand the While Action.

However, I have to add a ==strong caution== to all readers: This is a very dangerous macro that does not provide for any exit. IMO, all loops should always have some form of exit, even if it is a timeout (exit after N seconds).

For this type of macro, it would probably be better to NOT have the While Action, and to trigger the macro every so often using a Periodic trigger.

Perhaps @Saul can shed some light on how he plans to use this method.

At least with the WHILE method there's a way out if you map a "Cancel All Macros" action to a key, as I do (even if you don't, the KM menu usually suffices). With the periodic method, perhaps with a 1 second period, for a macro that moves the mouse, that doesn't give you enough time to be able to move the mouse to stop the KM engine. That's extremely dangerous. I've had to press the Mac's "Power Off" button for 7 seconds many times to stop the KM engine using that approach.

But yes, I use the periodic approach too. When it suits. Actually I usually use a hybrid. I use the periodic trigger to start a macro with a semaphore (with a short timeout) at the start which is immediately followed by an infinite while loop. That works much better than the regular periodic method because this way I have control over how often it executes code. The periodic trigger is limited to 1 second, which is sometimes inadequate.

Wouldn't you also call the "Launch Engine at Login" option in KM "extremely dangerous", since using it could restart a periodic macro that grabs the mouse and makes it impossible to shut off the engine? I imagine you might have to boot in safe mode to fix that. I think macOS has a safe mode boot.

Sorry for arguing, but this is how I see it.

Hi JMichaelTX and Sleepy.
Thanks for your help.
In my macro I have in my loop a "Prompt for User" action after every action and that's how I stop the loop.
Now I see the benefits of pasting the macro here in the forum when you ask a question.
I think it's too late/unnecessary now but next time I will.

It would be helpful to future readers who have the same or similar questions/issues, if you could upload your final macro.

1 Like

Will do. No problem. :+1: