Randomly Generated Script

I am addicted to a particular game, and I want to create a macro that will pick at random any of several scripts to run when this game is active to remind me to get back to work. I have written all the reminder scripts (based on McSparky's Twitter Timer), which are all different (to keep the variety and maintain the element of surprise). But now I want to create a master script triggered by the game itself that will randomly choose one of the reminders and run it.

Variables? Random function?

This should be fun.


Many thanks ahead of time.

My ears perk whenever someone uses the word "random". In your case, I can tell you don't need "truly random," nor do you need any special "probability density functions." All you need, I think, is moderately random results. In that case, one of the simplest methods using KM would be this structure:


You would replace the "Speak" action with your various macro calls.

This is not the only cool way to address this problem. I have other cool ways, which I might post.

Here's another cool way to do it with a SINGLE action. You put the names of your macros in the first box, and copy the remainder of this action exactly. In this example, the three macros I want to call are named "One", "Two" and "Three."

choices=`cat /dev/stdin`
choice=`echo "${choices}" | sort -R | head -n 1`
km="osascript -e 'tell application \"Keyboard Maestro Engine\" to do script \"$choice\"'"
eval "$km"

Ah, Sleepy,

Thanks for the reply. I am reminded of a college schoolmate of mine who was an engineering major. I was taking calculus at the time, and I asked him once to explain the formula for something called Taylor series so I could understand it, and he replied, "I don't understand why it works, myself. I just memorized the formula. And that's what you should do." I suspect that the code you have offered is JavaScript or Python or something, which I think is cool. But I do like to try to understand why or how something works. I am pretty much a novice at programming (and I am retirement age), and KM suits me better than the more esoteric languages. If there is a way to achieve my goal that involves a concept that I could then transfer to another similar situation, I would be ecstatic. You may notice that my avatar is a baby elephant. Elephants are supposed to have good memories, so maybe they are good students. But this is a baby elephant. Just learning to be a good student. That's me.

I will try both of your suggestions, though, and I am grateful for your response.

The second solution is not Javascript or Python, it's Shell Script. I guess the second solution was over-the-top for this problem.

Just stick with the first one. Do you understand how the first one works? If not, what part of it do you not understand? It creates a random number and executes an action that corresponds to that number.

The second one has four lines, which do the following:

Line 1) read the names of the macros from the input box and store in a variable called choices
Line 2) sort the names in random order onto separate lines and select only the first one, store in a variable called choice
Line 3) create and save into a variable called km the AppleScript command that will call and run the chosen macro
Line 4) execute the AppleScript command stored in km

I am trying to understand the first option that seems to involve a "Switch of text" action which I cannot find. The open and closed bracket icon suggests that this action can be found in the Control flow category, which lists actions: Group, Until, While, Repeat, If Then Else, and so on. Which of these is the one you are using? Sorry to be such a newbie and to need so much hand-holding, but I do appreciate your help.

This refers to Switch/Case Action

There's no reason to be embarrassed about not understanding. Any honest question is a good question. You should hear the kind of questions my mother asks me about computers. :flushed:

Did @macdevign_mac answer you satisfactorily? I can't tell. Do you know where to find the Switch action in the KM Editor? Just drag it into your macro and change the default "System Clipboard" option from:

to the "Text" option and it will look like this:

Then enter the phrase "%Calculate%RAND(1,3)% into the box.

Then change the "contains" option to the "is" option. Then press the green PLUS button until you get as many choices as you need.

If that doesn't clarify this for you, just ask again.


I do like your first solution. It works fine now, thanks to your patient instruction. Now if you wouldn't mind going a little deeper with me, I have two new questions.

First, where can I find the phrase "%Calculate%RAND(1,3)%" in the manual? It's that syntax that I would like to have more facility with. I suspect it applies to other functions as well, hence my interest and curiosity.

Second, I had given my six (there were six of them) timer macros more meaningful names than just 1, 2, 3, 4, 5, 6. But I felt it would be simpler for the purposes of this solution to rename them with just the numbers; I don't know why, except that I thought it would be less cumbersome. Am I right? But I would like at some point to return to their original names to retain their meaningfulness to me. Again, I suspect this will have something to do with variables, with which I have little facility but lots of interest. The fact that variables might come into play is exciting to me, as I know that an actual application of them in my "real world", as opposed to a merely theoretical discussion, will more likely cement the concepts in my brain.

In addition, as I was constructing solution 1 as per your instructions I copied and pasted the steps one by one. And as I was doing so I thought there must be a way to simply "call" a previously written macro rather than copying and pasting it step by step into another larger macro. I know there is a way to do this, and I suspect your second solution (which I confess I have not yet digested) actually does this. It's variables, isn't it? I just know it is. I can hardly wait!

Your patience and understanding is very much appreciated.

And my own understanding, if it had a name (maybe a variable?) would say a big thank you.

I am answering your questions in a different order.

You can name them what you want. I think it's sloppy to call your macros numbers, because it's better to use names that describe what the macros do. But that doesn't make it wrong to use numbers as names.

Indeed you can store a macro name in a variable and call it. But if I may say, I think you're diving in too deep. I was probably just showing off a little when I showed my second macro. Once you master the first one, perhaps then you can delve into the second one. You may not like this advice, but I think you have to add before you can multiply.

I think that sounds like a compliment, so thanks. I don't quite understand your point.

The official answer to your question is here:


Basically, this "token" called "Calculate" is just a way to use a numeric expression inside a text string. For comparison, you could also find it useful here:


That's important to realize because if you said this:


... then the output would be "1+2" instead of "3".

As I'm answering your question here, I realized I'm ignorant of the fact that I could have done the Switch statement this way:


This would have been exactly the same as the Switch statement above using %Calculate%% but I guess I failed to think of that. I feel a bit foolish for missing that/

Another glitch has shown up. A simple one to fix, I'm sure. When I quit the game (remember the game?) before the time limit is up, and then restart the game, two timer macros are now triggered simultaneously. This is only briefly amusing, but more annoying. How can I abort the macro if the game is quit before the timer goes off?

I haven't seen the macros, so I can't fix them.

I would love to show you the macros if I knew how. You seem to do it so easily. Is it just click, copy and paste?

Pause for 180 Seconds
Hide 2048 Game
Switch of Text “%Calculate%RAND(1,6)%”

As I write this, it doesn't look like I did anything.

By the way, the "understanding" comment was, in fact a compliment. I was merely playing with the two meanings of the word, the first being "sympathy" and the second being "Oh, now I get it".

There is a menu item in the KM Editor under "File" that lets you "share" your "selected macro" onto this website. Try using it and see what happens. Although I think this website is the BEST website I've ever seen for posting and replying to comments, the process for uploading isn't as simple as it could be (it's not BAD, but it takes experimentation. You need to experiment. Maybe there should be an actual forum called Test to allow people to experiment.)

Make sure you select a macro with your mouse before you upload anything. Because if you select an action instead, I think it uploads only a single action. And it's not obvious, but the group name you use is the name people will see, so I recommend using a group name that you don't mind other people seeing. Maybe "MyGroup" or something that describes the macro(s) you upload, even though the macro already has a name that describes it.

P.S. Thanks for the compliment. Sorry I didn't quite understand it.

This is what I get when I try to share a macro. It wants to start a new topic.

Screen Shot 2021-12-18 at 11.13.47 PM

I'm doing something wrong here.

I don't know if I'm doing it the right way, so my advice here may not be the best advice, but if you do press New Topic in your option above, it will start to create a new topic, but you can easily enough cut the text from that New Topic, then go back to the original topic, then paste the same strings in there. Voila, it's done.

Mind you I'm not sure if I'm doing it the RIGHT way like that, but it works for me.

Timer Randomizer Macro (v10.0.2)

Timer Randomizer.kmmacros (8.3 KB)

Did I do it?

@Sleepy, @Polyvox I'll admit I didn't read every post in this thread. But here's an alternate version that makes it really easy to add or remove messages - all you have to do is change the first action. The rest is sort of a "black box" that just does the work, and you don't have to understand it if you don't want to:

Get Random Line Example.kmmacros (9.7 KB)


Here's the entire script:

FWIW, the above script would be much simpler if KM had a built-in way to get the "nth" line in a list of lines.

I also have a version that's written in JavaScript (JXA), but after I wrote it, I thought that maybe a pure KM version was more appropriate.

It does!

But now that I know all his actions are the same thing, here's how I would have solved his problem:


This is a very good example of why people should post the actual problem rather than "how do I get my code to work?"

I just noticed that he wanted to DISPLAY and SPEAK the same words simultaneously. I didn't notice that until now. There are several ways to do that, but my preferred way is as follows. I just save the output to a variable and then DISPLAY & SPEAK the text in separate actions.

1 Like

I'm still trying to navigate my way around this forum. Saw DanThomas' @Sleepy, @Polyvox and tried to @Sleepy but instead seemed to have sent a personal message outside the thread. Apologies. I think you mentioned that this forum might benefit by having its own navigation manual.