Script help - Randomized key press interval

Hello all! I have a script help request that I'm hoping those of you more versed than I can help with. Here is the use case:

I have a program that will time out and I need to be able for a script to press the spacebar at random intervals. Ideally somewhere between every 1-4 minutes it will just press the spacebar. It sounds simple in concept, but I'm finding the execution not working despite spending hours working on this.

I would appreciate any help! This program is amazing!

KM has two random functions for calculations: RAND(), RANDOM().

While I don’t think it is possible to do a calculation in a trigger(?), you could launch the Space-key macro periodically every 2 minutes or so, with a pause action with a randomized delay (calculated with one of those functions) before the actual Space-key pressing action.

Out of curiosity: Why has it to be random? Wouldn’t every 3 minutes (periodic trigger) work also?

thank you! In retrospect, you bring up a good point. It could be every 3 minutes. Random isn't necessary.

I found some areas on the forum, and I have started the following, but not sure where the error lies. I think it's with the Check Random Script macro and the environment variable. I think that may be the wrong action.

So I start with a script called: "Start Random Script"

Then It calls: "Check Random Script"

And then I have "Run Random Script"

Any thoughts? Thank you!

If randomizing isn't necessary and you can have it periodically work every 3 minutes, try this.
I just used the Podcasts app of course yours will be different.

I hope this helps.


thank you, KC! I do want to try and figure out if randomized intervals are possible (more because now I just want to see if it can be done). Any ideas? I set the nextExecutionTime variable to NOW()+random... then I have it do the check script - I think this is where my error lies. Thought is that if the now() is > nextExecutionTime then it needs to run the "run random script" which presses the key. If it's not, then it's not been long enough and it should wait until it is. I'm thinking there must be something wrong with that Check Random Script.... not sure if now() is considered an environment variable, and perhaps I have the wrong trigger selected.

Any thoughts? Thanks!

That’s what I meant in my first reply:


Macro launches every 2 minutes, waits for a random period of 0 to 119 seconds, then presses Space. So, the key will be pressed somewhere in-between every 2 minutes and 3 minutes 59 seconds.

(Well, actually more often, I think, because the next macro instance will launch 2 minutes after the previous one, independently of the random delay. I’m not good at maths. But you get the idea.)

Thank you! Clearly I was making this much more complication than it needed to be!

Ehm, that’s what I was thinking too when I saw your macro screenshots :wink:

Quoting myself:

If you want to prevent that an instance of a macro launches while another instance is still running, you have some tools for that:

I tried to showcase how these work, compared to each other, here.