How to shuffle/randomize letters of a clipboard content

Hi.

I am trying to shuffle the content of my clipboard. I would copy the name of one country to my system clipboard, fire my Keyboard Maestro macro, and then paste the shuffled content to the frontmost window.

As, with Keyboard Maestro, one can only shuffle lines and not words, I tried some shell script. With no luck :grinning:

Any suggestion would be greatly appreciated.

I'm good with text manipulation, but I'm not sure what you are trying to do. It sounds like you are trying to randomly permute all the characters in the clipboard (based on the code you submitted.) Is that right? But then you mention "countries" and I'm not sure how that's relevant to the task of shuffling text.

I see you are using the "shuf" command. That's not part of macOS. You are also using two variables, "Shuffle" and "word", but it's not clear to me what those variables are meant to contain.

Can you explain what you mean by that? Based on that statement, you are trying to shuffle words, not "all the characters of the clipboard."

I can't write a macro to help if I don't know what you want.

1 Like

Here's a macro that will take an input containing words separate by spaces or newlines and randomly re-organize all the words onto separate lines. I'm not sure if that's what you want, but that's my best guess as to what you want.

image

1 Like

@Airy thank you very much for your help. I should have added a exemple of what I wanted to get.

I am just trying to create a faster way to create anagrams (of words, country names, ...). So, I need to shuffle the letters (not characters) of a word.

I would like to copy, for example, the word "australia" to my system clipboard. Then the Keyboard Maestro macro would shuffle this word and copy the value of the shuffled australia, for exemple "usaratail", to my system clipboard.

Hope that it makes sense now.

One approach to updating the clipboard with a character-shuffled version:

Shuffled CHARACTERS (or WORDS) of clipboard content.kmmacros (5.7 KB)


Expand disclosure triangle to view JS source
const main = () => {
    const
        src = kmvar.local_Source,
        wordOption = kmvar.local_WordOption,
        byWords = !isNaN(wordOption) && 0 < Number(wordOption);

    return byWords
        ? knuthShuffle(
            words(src)
        ).join(" ")
        : knuthShuffle([...src]).join("");
}

// -------------------- KNUTH SHUFFLE --------------------

// knuthShuffle :: [a] -> [a]
const knuthShuffle = xs => {
    const
        swapped = (iFrom, iTo, xs) =>
            xs.map(
                (x, i) => iFrom !== i
                    ? iTo !== i
                        ? x
                        : xs[iFrom]
                    : xs[iTo]
            );

    return enumFromTo(0)(xs.length - 1)
        .reduceRight(
            (a, i) => {
                const iRand = randomRInt(0)(i)();

                return i !== iRand
                    ? swapped(i, iRand, a)
                    : a;
            },
            xs
        );
};

// ----------------------- GENERIC -----------------------

// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
    // Enumeration of the integers from m to n.
    n => Array.from(
        { length: 1 + n - m },
        (_, i) => m + i
    );

// randomRInt :: Int -> Int -> (() -> IO Int)
const randomRInt = low =>
    // The return value of randomRInt is itself
    // a function, which, whenever evaluated,
    // yields a a new pseudo-random integer
    // in the range [low..high].
    high => () => low + Math.floor(
        Math.random() * (1 + (high - low))
    );

// words :: String -> [String]
const words = s =>
    // List of space-delimited sub-strings.
    // Leading and trailling space ignored.
    s.split(/\s+/u).filter(Boolean);

return main();
1 Like

I see now. That's clear, I think. But I have to head out for a while.

Keyboard Maestro has a Shuffle Lines Filter action. So all you have to do is break the characters in to lines, and then shuffle them and then put it back together.

Assuming a single word, something like this.

1 Like

Keyboard Maestro also has an action which lets you extract a string from a string, and if you do this right, you can "shuffle" a string quite easily. Here's my solution. It will let you specify a word, and it will generate three random shuffles, each one splitting the word seven times (I find that 7 seems to be pretty effective) and then displays the output.

Group Action (v11.0.3)

Group.kmactions (7.7 KB)

Keyboard Maestro Export

1 Like

Expanding on @peternlewis's answer (oh, the temerity!) you can "For Each" the clipboard contents and shuffle each word independently, appending each result to an "output" variable:

Shuffle Letters Within Words.kmmacros (5.4 KB)

Image

3 Likes

I am lost for words. I came looking for one solution. Now, thanks to @ComplexPoint and @peternlewis, @Nige_S and @Airy I have many solutions that do fit my needs. Thank you so much!

2 Likes