Random Text from a list of 10 or so choices

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?

1 Like

I'm trying to get the path of a random file in the Finder's selection. I must be doing something wrong, because currently it outputs the entire list in its original (alphabetical) order.

Screenshot

The shell doesn't like carriage returns – change that to a linefeed.

1 Like

Brill! That works!

1 Like

@gglick

I would like to ask both of you,
instead of choosing text randomly, can we make macro to choose text one after the other.

I mean, first time I run macro, it should choose first line of text from the list. Second time I run macro, it should choose second line of text from the list. and so on

I also have asked same question here

There are lots and lots of ways to do this in Keyboard Maestro. But the key is understanding that after each Macro run one thing that can persist (and provide a count of how many times you have run the Macro) is a Global Variable.

So, you make a Global Variable of "1" and the first time the Macro is run it adds "1" to that, making the Variable "2". And the next time you run the Macro is adds another "1" making the Variable "3" etc. This is the way the Macro knows how many times it has run and can choose which bit of text to display/use. Once you reach the total of however many pieces of text you have the Variable is reset to "1" and the cycle can start again.

Here is an example, to show the concept.

(It is just an example as I do not know what form your original "lines" of data are. Whatever they are, there will be a way for Keyboard Maestro to get at each line and cycle them on each Macro run.)

EXAMPLE Cycle through list on each Macro run.kmmacros (11.0 KB)

Click to Show Image of Macro

2 Likes

Thank you so much.
Let me try this out.

Hey I tried but 'Set variable __ to calculation __' is giving me error. See the text color changed from white to orange in the screenshot

Here is a screenshot of error msg as well:
Screen Shot 2022-10-25 at 16.27.05

The orange colour is because at this point in writing the Macro, ytintro_counter doesn't exist. But on first run the Macro should create ytintro_counter.

I have tried it on my Mac and it works.

(The top of your screenshot is not showing all the conditions for the If Then. It should be set so that "any" of the following are true, not "all".)

1 Like

Thanks for the prompt reply. That really saves from getting discouraged.

Thanks for letting me know. While I was exploring, I went ahead into setting and put the value as '1' for ytintrol_counter, which was earlier showing as 'default value'.
And that worked.

And all the credit goes to you. Thank you.

1 Like