Looking for strings

Is it possible to catch strings in a text (clipboard) which have the same endings?
I have to programming this two things:

Example 1: search for links in a text, which are ending with “.ch”,".com",".de" and catch them (variable, clipboard).

Example 2: search vor links in a text and start the browser automatically.

Thanks a lot for helping me!


Hey Reto,

Not a problem.

I've used the Clipboard as the data source for these, but you can easily change that to a variable.

This one handles your first request. It also removes any duplicates and sorts the output.

Match Pattern with Perl.kmmacros.zip (984 Bytes)

To find links change the pattern to something like:


* Although there are more comprehensive patterns for finding urls.

This one matches URLs and opens them in Safari.

Be careful with this. I've done nothing to limit the number of links opened, and if you open a hundred or more you could slow down your system and/or crash Safari. (It would not be difficult to put in a limiter.)

Match URLs with Perl ; Open in Safari.kmmacros.zip (1.3 KB)

On my system I'd do the whole job in AppleScript with the Satimage.osax, which makes this kind of stuff easy. Particularly since I'm using AppleScript to operate Safari anyway.

(The SIO adds excellent regular expression support to AppleScript along with a quick sort function that can remove duplicates and other goodies.)

That would look something like this:

set _data to the clipboard
set urlList to find text "https?://[^'\"[:blank:]]+" in _data with regexp, all occurrences and string result without case sensitive
set urlList to sortlist urlList
if urlList ≠ {} then
  tell application "Safari"
    make new document
    set bounds of front window to {228, 22, 1542, 1196}
    tell front window
      set URL of its document to item 1 of urlList
      if length of urlList > 1 then
        set urlList to rest of urlList
        repeat with i in urlList
          make new tab with properties {URL:i}
        end repeat
      end if
    end tell
  end tell
end if


Hey Chris

Thanks a lot for your answer and the well written program code. It was exactly what i was looking for.
But i've one question more. Is it also possible to look for expressions (strings) without any applescript or pearl. In this way the program would be easier to handle for a layperson.
I've done something, but i don't know how to search for strings. In my program its not a different between searching for "Andreas" or "Andreas Knecht". But it should be, otherwise it sorts also out "Andreas Rubli" per example.


Hey Reto,

Yes of course.

The reason I used Perl and AppleScript for the original problem is that as far as I know Keyboard Maestro won't find multiple strings per line. It will only match a literal text or regular expression once against the given text. (Peter will correct me if I'm mistaken.)

In your example you're looking to 'match' a string, but what you may not realize is that it means 'match regular expression'. Probably what you want is 'contains' or 'is' 'Andreas Knecht'.

All of those text-conditions are completely obvious except 'match'. Personally I think it should be something like 'REmatch' or 'matchRE' for match regular expression. Some people would not understand that 'RE' means Regular Expression, but they wouldn't automatically assume a perfect match either.

Don't be afraid to test things out.

Test { Text-Matches }.kmmacros (4.3 KB)

Play with it. Change the text. Change 'matches' to 'contains', etc.

Everyone, please - when asking for help be as specific as possible.

This may seem a trifle lecturey, but bear with me.

If you ever had math, science, or computer science in school you know one of the first things they teach you is to throughly define a problem - a practice which distinctly improves the likelihood of solving same.

The principle is the same when asking for help on user-lists. The more completely you describe your task the more likely you are to get quality help, and it will save you and your helpers time and effort. Remember that you already know what you're trying to do, but your audience is hearing about it for the first time.

I've been helping people on the Internet for over 20 years now. During that time I've received quite a lot of help myself, and it always annoys me when someone responds "Huh?" to my question or goes off on a tangent that fails to address my issue. When that has happened I've often found by rereading my original question that was me who was at fault, and I've learned over the years that it pays dividends to take the trouble to ask better questions. (Occasionally I still screw this up and have to shake my head at myself... :-))

For example:

Problem 1:

I need to extract every URL with a domain of http://www.google.com/ or https://www.google.com/ from a body of text.

That seems pretty straightforward, but Problem 1's description fails to specify what the input is and what the desired output is — and those can make a difference in how one approaches the job.

KM can iterate through lines and find a given pattern per line with no problem, but the way its search works has some limits I can overcome easily by turning to AppleScript or Perl. (If I knew for a fact that my input had only one URL per line I might stick with KM. Then again I can think of at least one find/replace workaround to let me stick with KM, but I'll leave that to your imagination for now.)

Problem 2:

I need to discover whether a body of text contains a certain string: 'Chris Stone', OR 'Christopher Stone', OR 'Christopher C. Stone'.

Problem 2's description fails to say this is an IF <condition> THEN issue, so in helping them I might provide exactly what they asked for and still have to field the follow-up question: "But how do I DO X if that condition is true or Y if the condition is false?"

Given a more complete description I can guide them to the IF-THEN-ELSE action in the Control Flow category and the text-condition for the action.

This problem is quite easy to manage with KM's tools.


  The-Text: MyVariable
  contains: Chris Stone

  The-Text: MyVariable
  contains: Christopher Stone

  The-Text: MyVariable
  contains: Christopher C. Stone

  --> Whatever

That's very simple to read and adjust as necessary.

Now, since I grok RegEx and find it fairly easy to read (most of the time) I personally would use 'match' instead:

The-Text: MyVariable
matches: Chris(topher)?( C.?)? Stone


1 Like

Hey Chris

I wasn’t at work for the last weeks, but i would like to thank you for your answer. It was exactly what i was looking for!

Of course you’re right, that my questions wasn’t specific enough. I will carry on for the next time!

Thanks and best regards

Hey Reto,

Although this doesn't apply to your original question of detecting whether a variable contains a string, it does follow-on to my original answer and makes Perl unnecessary.

Peter clued me in to how to match substrings in a variable, so I'm following up for completeness.


RegEx { Find Substrings } 03.kmmacros (4.2 KB)

Tags: @RegEx, @Regular_Expressions, @Match, @SubString

Thanks for this one. I was having trouble using a Variable in the Search Variable Action – seemed not to want to process the token. But this one took my variable just fine.

With it I built a “Bash command cheatsheat” for myself, since I so regularly need to look up bash commands as I’m reading articles about shells and bash and programming, being a noob. The info command in the shell itself is really helpful but a single line summary is nice to have on the fly when I’m not wanting to fall into a massive long definition in the shell or wikipedia.