Search a Named Clipboard for a Word or Phrase

I am trying to search the text of a named clipboard to see if it contains one or more instances of at least one of the words or phrases below. Case does not matter.

san francisco
malibu
always los angeles
sunny city
beach
sand

The macro output should be a simple yes or no. The final macro will contain over a thousand words or phrases to search the named clipboard.

Could someone please help me?

Why are you using a Named Clipboard?

Just use an If Then Else action, set to any of the following are true with multiple Clipboard conditions.

image

Or use a case insensitive regex match.

image

and if you wanted to automatically assemble a case-insensitive regular expression like that (from a given list of phrases), you could use a script action:

Test for presence of any of listed phrases.kmmacros (3.6 KB)


Expand disclosure triangle to view JS source
(() => {
    "use strict";

    // "yes" if the system clipboard contains any phrase
    // listed in the KM "phraseList" variable.
    // (otherwise "no")

    const main = () => {
        const
            kme = Application("Keyboard Maestro Engine"),
            phrases = lines(kme.getvariable("phraseList"))
            .flatMap(s => {
                const token = s.trim();

                return 0 < token.length
                    ? [token.toLocaleLowerCase()]
                    : [];
            })
            .join("|"),
            maybeMatches = kme.processTokens(
                "%SystemClipboard%"
            )
            .match(
                new RegExp(
                    `(${phrases})`,
                    "ui"
                )
            );

        return Boolean(maybeMatches)
            ? "yes"
            : "no";
    };


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

    // lines :: String -> [String]
    const lines = s =>
    // A list of strings derived from a single string
    // which is delimited by \n or by \r\n or \r.
        0 < s.length
            ? s.split(/\r\n|\n|\r/u)
            : [];

    // MAIN ---
    return main();
})();
1 Like

Is there no size limit for the “matches” field?

Not really, no. How long were you intending to make your list?

If it is a very large list, then maybe Keyboard Maestro is not the best tool for the job.

But it should handle a list that is tens of k long without too much trouble I would imagine. Not that it is something I have checked.

1 Like

I've scripted some regex matches using a variable that were quite large (I wouldn't want to maintain a large list that was only extant in the match field).

That said I wouldn't go too crazy without doing some testing...

1 Like

That's why I asked ...

This is the sort of thing to test for yourself...

Using a list of 235,886 words I got quite a respectable time out of KM on my old hardware.

image


Download: TEST – RegEx – Clipboard Contains v1.00.kmmacros (2.4 MB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

2 Likes

Hey Chris,

There must be some misunderstanding here. I wanted to know how many characters are allowed here:

Screen Shot 2023-04-11 at 08.23.24

You created a solution with a list, that would have been my first idea too!

Apparently you missed the fact that I tranformed the 235,886 word list into a RegEx pattern.

This translates into something over 2,493,109 Characters.

Good show @peternlewis!

:sunglasses:

1 Like

Yes I did. All clear now. Thank you!

1 Like

There is no particular limit that I know of.

That said, I wouldn't want to add 2M to the Keyboard Maestro Macros.plist file if I could avoid it, so I'd probably put in a file if it is going to be very large (more than 20k say?), and read the file into a variable and use that.

But whatever works - if it works for you, and you don't experience any slowness in running the macro or using the editor, then there is not reason you should have any problem.

1 Like