Random Text from a list of 10 or so choices

KM has a couple of actions that helps with regard to processing tokens, variables and calculations:


There's also another (older?) way to dereference a variable: using AppleScript. The method is different depending upon whether your variable is local or global. You didn't indicate whether your variable was local or global. The following example shows how to assign a value (the parameter passed to the macro) to a local variable called LocalValue.


Those are your options. But your scenario involves finding the variable(s) in the line and replacing them with their values. That may require some extra coding. But I want to get some breakfast soon so for now I'll just leave you with this lead.

Thanks for your answer :slight_smile:

My objective is not to filter these sentences which contain variables put I just want KM to replace the variables inside those sentences by a specific text which I indicate at the beginning of the macro.

The variables are local ones, I set them as follow:

That is very easy to do:


Did you see the solution I offered above to select a random sentence?

Yes I know, that's what my examples were meant to show. I wasn't "filtering" anything, that's just the name of the KM action (and it's a bit of a misnomer for the example I provided.) In particular the first example shows how to replace a variable name with its contents. That's what you are asking for.

Then you said your variables are "local ones" but your example showed a "global variable". Are you familiar with the difference between local and global variables?

JM gave another answer. His advice is almost always great. In this case, I don't think it is because you want the variable evaluated not when the sentence is assigned to a variable, but at a future time. That's what my advice was attempting to solve for you. My advice was about letting you store variable names as part of sentences and evaluating them later. JM's advice was about evaluating a variable name at the time it is assigned to a variable, which I think is not what you want. But if I'm wrong, that's fine.

@Sleepy thanks for your answer.

I checked the difference between local / global variables and yes my variables are global ones.

If I have followed correctly, by integrating the AppleScript, I'll be able to assign the variable which is set at the beginning of the macro and then use it within the shell script ?

@JMichaelTX thanks for your solution !

If I have understand well what your macro is doing:

  • I set a list of sentences
  • The macro create a temporary txt file
  • The macro processes the actions I have defined until every one of the sentence has been used (and they are used once they have been removed from the list

Is that it ?

  • I set a list of sentences -- :white_check_mark:
  • The macro create a temporary txt file -- NO
  • The macro processes the actions I have defined until every one of the sentence has been used (and they are used once they have been removed from the list -- :white_check_mark:

Have you tried running the macro? I think that will answer all your questions.
It does NOT require a file, but you can optionally get your question list from a file using this Action:

Does that work for you?

Thanks ! Looks like it runs and does what i'm looking for :slight_smile:

Now I have to find a way to integrate it to my whole macro. I will keep you update about that.

@JMichaelTX Your macro is working but there is one thing which does not fit exactly my needs.

Indeed, the macro is set to loop until the list is empty while I only need the macro to select a random line within the list, remove it so when it's processed again, the previously chose line is not available.

In that sense, I would need the macro not to loop the actions until the list is empty.

Thanks !

That's easy enough. Just take the Actions out of the Repeat Action, and change the "Local__Master" list to a global variable like "DND_RT__MasterList". Of course you need some way to initialize that variable.

1 Like

So, I just took out the actions from the Repeat Action as you suggested, it worked perfectly. However, I did not change the "Local__Master" list to a global variable. Is it really necessary ?

1 Like

Depends on your use case. Local variables cease to exist when the Macro terminates, so each time you trigger the Macro it will start with the same data. If you want the line removed to NOT be in the data for the next trigger, then you would need to use a global variable.

Understood !

Then, I need to tweak it because I have interlocked many macros within macros to save time when editing the general one.

To give you a better idea of what it looks like, here's the structure:

  • Connect to Profile 1
  • Execute the macro named RUN 1
    • within RUN 1 macro, there is your macro list which allows me to pick one sentence randomly
  • Quit Profile 1
  • Connect to Profile 2
  • Execute the macro named RUN 1 (exact same macro, not a duplicate, every profile is linked to this macro)
  • Quit Profile 3
  • Connect to Profile 3
  • Execute the macro named RUN 1

With your explanations, It seems that I really need a global variable.

1 Like

@JMichaelTX So, I have transformed the Local variables into Instance ones. But it does not work as it's not writting anything when it should be.

I've done the following modification to your script:

I've placed the 2 first actions (Replace with your... + Set Variable "Instance__CurrentList") before connecting to the first profile (i'm referring to the scheme I previously posted)

Then, in each "RUN 1" macro, at some point the following actions are taking action to insert a random sentence

Are my modifications correct ? Would you script work the way I arranged it (or should all the actions of the script be at the same place to work properly) ?

Well, it is hard to tell just from looking at your screenshots.
The best way to find out is just to actually test the macros. Trigger them like you would for your workflow.

Yes I ran test with the instance variables and it did not work. However, I figure out that by using a global variable, it works. Maybe the instance variable is not "wide" enough, not integrating all my sub-macros levels.

So, the solution for the ones looking to do something similar:

  • Place the 2 first actions at the beginning of your macro + use a global variable instead of the local one, be sure to change every variable
  • Define all the actions you need in between
  • Each time you need to enter a random sentence, place the script + an "insert text" variable + append variable

Thanks again for your great macro @JMichaelTX

That is correct. Instance variables can only be shared/seen by the main macro and any sub-macros it calls. They cannot be seen by other macros triggered independently.

See Variables -- KM Wiki.

I ran Peter's "Paste Random.kmmacros 18 (3.5 KB)" and it didn't work. Used as is, it just keeps choosing the last item in the list. In this case, it was a routine day, over and over and over.
Maybe it needs to be updated for KM 9.2?