Find Matching Text From Clipboard in a List of Possibilities

This is seemingly simple, but has me stumped. Assume the contents of the clipboard is a URL, i.e.:

https://example.com/some/path/to/a/page

I have a variable, URL_list, that contains a number of domains:

apple
ibm
example
keyboardmaestro
etc

In pseudocode, what I want to do is this:

if (clipboard contains a domain from URL_list) then
do some stuff
otherwise
end macro
end if

I tried a For Each loop (var: filterableURL) with a Search System Clipboard for Regular Expression...

.+%Variable%filterableURL%.+

But I don't think I'm doing it right, as it's not working. All help appreciated.

-rob.

(edit: fixed typo in regex)

Hey Rob,

This is probably how I'd do that for myself using Keyboard Maestro native actions.

Match a URL's Domain Against a Domain List v1.00.kmmacros (12 KB)
Keyboard Maestro Export

I've extracted the domain using brute force rather than finesse, but that should be more reliable.

This job is a little easier with AppleScript:

set theURL to "https://forum.keyboardmaestro.com/t/find-matching-text-from-clipboard-in-a-list-of-possibilities/23768"

set myDomainList to paragraphs 2 thru -2 of "
apple
example
ibm
keyboardmaestro
"

set theURL to theURL as URL
set webHost to DNS form of host of theURL

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}

set webDomain to text item -2 of webHost

set AppleScript's text item delimiters to oldTIDS

if webDomain is in myDomainList then
   return true
end if

-Chris

Chris:

Wow, thanks! And now I don't feel so badly for not finding the solution. It'd be great if KM just natively supported a function like this, where you can easily check if a value (possibly regex manipulated) is contained in a list.

And because this was code I was running before I started filtering, in order to exit the macro quickly if there's not a match, I think I'll have to skip implementing this. The current solution I have works, but suffers a bit in readability. (I store the list of hosts as a regex, i.e. apple|ibm|example, and then just do a search on clipboard using regex, with the regex set to the variable.

I think I can probably add a single step, though, and restore readability: Store the list as above, one per line, then just replace line breaks to pipes via search before passing to the search.

Again, thanks for the effort - no way I would've gotten that on my own!

-rob.

It's very possible I'm missing something, but if I'm reading your OP right, something like this with native KM actions seems like it should work:

Compare List to URL Domain.kmmacros (3.7 KB)

While I used the %FrontBrowserURL% token instead of a URL on the clipboard to expedite testing, as far as I can tell, there's no reason in theory that it shouldn't work with that as well.

Thanks for another solution, @gglick! After mulling it over, though, I realized I was overcomplicating it: My objective was to exit my macro as quickly as possible if it didn't contain a URL I wanted to filter—because it runs on clipboard change, so I didn't want to linger in code if nothing was going to happen.

I also wanted a readable list of hosts, which is what started me on the path that led to this question. But I realized I could have both with a little help from find and replace:

The list form lets me easily see and edit, then I use a search and replace to swap the line breaks for regex pipes (the 'or' symbol), and then I run a regex search looking for matches in the clipboard's text. I have the search set to end the macro on failure (without notification).

This is working perfectly for me, but I wouldn't have gotten there without everyone's help here, so thanks again!

-rob.

1 Like

There's More Than One Way to Do It!

:sunglasses:

1 Like