Help Randomizing a List of Words

Hello,

I'm trying to randomize a list of comma-delimited words copied from a cel in excel, and then storing that list in a clipboard or variable to be pasted later in the workflow. I've been trying to kludge something together from what I found elsewhere, but I only know a little about keyboard maestro and NOTHING about applescript.

I've been trying to use this

set myList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
set randomizedList to randomizeList(myList)

on randomizeList(theList)
    set listCount to count of theList

    set newList to {}
    repeat listCount times
        set subListCount to count of theList
        set r to random number from 1 to subListCount
        set end of newList to item r of theList

        -- remove the random item from theList
        if subListCount is 1 then
            exit repeat
        else if r = 1 then --> first item
            set theList to items 2 thru end of theList
        else if r = subListCount then --> last item
            set theList to items 1 thru -2 of theList
        else
            set theList to items 1 thru (r - 1) of theList & items (r + 1) thru -1 of theList
        end if
    end repeat

    return newList
end randomizeList

Which I found here: http://stackoverflow.com/questions/15458773/it-there-a-simple-way-to-shuffle-a-list-in-applescript


I've tried this:

tell application "Keyboard Maestro Engine"
	set fruit to value of variable "fruit"
end tell

set myList to fruit
set randomizedList to randomizeList(myList)

But that returns

as: a, r, a, ,, g, ,, p, b, e, a, e, n, a, , o, a, p, n, l, n,

I've tried other equally fruitless (get it?) things with varying degrees of failure, and thought I'd take a short break from banging my head against this problem to come and ask here :slight_smile:

Thank you

This should work:

MACRO: Randomize List [TEST]

Randomize List [TEST].kmmacros (7.1 KB)

3 Likes

This is perfect! Thank you!

May I ask if this is easily adapted to other delimiters (for example “-” or “;”) ?
Or even complete sentences on a new line like:

The World is your garden.
Happiness is an empty head.
etc…

I have (plain) text files which contain words on a new line and text files which contain sentences which could maybe be randomized by this macro.I could read them to a variable. I do see some comma’s in the script but are a complete noob to try and experiment with it. Thanx for any input in advance, this forum is amazing…

1 Like

You can probably just replace the parameter in splitStrToList. Change it to “-” or “;”.

For sentences, you can perhaps use “\n” or “\r”, I’m not sure.

Alternatively, you can replace the returns in Keyboard Maestro with some other character like “;” or “•” and then split by that.

2 Likes

Thanx Peter, I will try that :slight_smile:

(Update: I can confirm Peter’s suggestion does work, I’m a happy camper!)

1 Like

(Edit: So the short version of below: How to randomly select a few items outof a list)

Just to follow up on this (and i’m not sure if this should be a separate post)
If i have a plain text file, comma delimited (or newline), like this:

Beelzebub, coyote, enki, shub niggurath, shaitan, loki, toth, etc…

And the goal would be to take, say 4 (or more, or less) Gods/Devils ‘randomly’ from this plain text file and put them in their own variables?

I tried searching through the forum for random and the function RAND but i just don’t know where to start…There are a few topics but nothing that comes together as described above. So while the above macro nicely juggles all existing items randomly, how to choose but a few items randomly from a huge growing list?

Things that seem obvious to me are:

  • the plain text file grows (changes and is updated from time to time) so should be checked for size/counted
  • with this number the RANDOM part can come in (help!:wink: )
    picking 4 random items, checking against themselves if they are not double and therefore 4 unique items
  • Putting them in their own variables

Probably asking a lot here, but i can’t seem to bridge the gap. As i said i don’t know where to start, which could also mean that this is more a ‘scripting stuff’ type of thing. Of which i am of course a complete nob-head.
Any pointers would be greatly appreciated.

If you have less that 4 items, you cannot choose 4 items and have them all unique.

Like all problems, just break it down into component parts. You already have (from @JMichaelTX’s script) a way of picking a random item.

So a “simple” brute force solution is:

  • change the script into a sub-macro that returns one item at random in a variable.
  • Execute the sub-macro, and transfer the result into Variable1
  • Until
    • Execute the sub-macro, and transfer the result into Variable2
    • Until Variable2 is not equal to the value of Variable1
  • Until
    • Execute the sub-macro, and transfer the result into Variable3
    • Until Variable3 is not equal to the value of Variable1 and not equal to the value of Variable2
  • Until
    • Execute the sub-macro, and transfer the result into Variable4
    • Until Variable4 is not equal to the value of Variable1 and not equal to the value of Variable2 and not equal to the value of Variable3
  • Use the four random results.

Note that, as said above, if the list has less than 4 entries, the macro will never finish. You should set a timeout on each of the Until loops.

I see where you go with this Peter, i’ll try your suggestions, thanx for the insight :thumbsup:

Your suggestions are clear but the thing where i get stuck is your first suggestion:
“change the script into a sub-macro that returns one item at random in a variable.”

I guess i really need to start learning applescript haha!

Edit: Okay i found my solution

  1. first randomizing with the above script
  2. Choosing the first position to get one random item with the macro from this topic:
    https://forum.keyboardmaestro.com/t/get-item-from-list-by-position-example/5024
2 Likes