Extracting variable text from the clipboard?

I want to be able to extract a numerical ID from a link that’s copied to my clipboard.
I can’t seem to figure out how to go about doing this in KM.
To give a concrete example:

Say I have this link: http://www.domain.com/SomeText/123456.html
I want to be able to grab the “123456” from that link when it’s on my clipboard.

My reason for doing this is so that I can append it to another link, such as: http://www.domain.com/ArticleID=123456

Basically, I want a way to streamline something I do many times a day at work. We have a public-facing site with the .html and an internal editor where that same ID is used to edit the post. If I see a mistake on the live site, I copy that ID and append it to a link to our internal tool in order to make an edit. Rather than doing it manually, I’d like to be able to copy the public URL, hit a hotkey, and have the new URL on my clipboard and opened in my browser. I can figure out everything in this process except how to get that ID from the first URL.

Can anyone point me in the right direction? The “SomeText” changes with every link, and there’s also even tags after the .html sometimes, so what I was planning on doing was finding the “.html” in the link and then just using keystroke commands to invoke “command shift left arrow” to highlight the ID and then go from there… but I don’t know how I can get up to that point.

Hey @elbirth,

Something like this?

I'm popping up the new URL in a window, but you can easily change that to save to the clipboard.

Or – if your internal editor is smart enough – you might be able to send the link directly to it.

-Chris


Capture URL from Safari and Create New URL from ID Number.kmmacros (5.8 KB)

1 Like

Yes! This works perfectly, thanks @ccstone!

After testing to ensure functionality, I changed the last step from opening it in a window to opening the URL in the default browser. Our editor is browser-based, so just opening the URL in the browser is the best method, and this accomplishes exactly that.
I use Chrome, so I did tweak the URL capture portion to be %ChromeURL% instead of %SafariURL%, but outside of that, this is exactly what I needed, so THANKS again!

A quick side question- Do you know where I could learn more about whichever language it is that’s being used? For example, the step where you have (\d+).html$
I can more or less extrapolate what that’s doing in order to achieve the end result, but there’s no way I’d have been able to come up with that on my own. As I get more advanced with Keyboard Maestro, this kind of stuff is what really excites me and gets me to wanting to learn more.

I'm using a simple regular expression in Keyboard Maestro's search action.

Look in the Keyboard Maestro Editor Help menu for the “ICU Regular Expression Reference” menu item.

The regular expression in the macro is searching for: (\d+)\.html$

(\d+)  ⇢  search for 1 or more digits and use the parentheses to capture them for reuse.
\.     ⇢  . is a special token meaning any character - the backslash escapes it, so we have a literal dot/period.
html   ⇢  literal characters
$      ⇢  end of line token.

There are many websites and books devoted to regular expressions, and it's easy to get lost in them and waste time – so start out with stuff we've recommended until you know your way around.

See these links:

How did you learn RegEx (regular expressions).

Keyboard Maestro Wiki on Regular Expressions – be sure to note all the resources listed at the bottom.

You can also search the forum for “regex” and “regular expression” to find many examples.

If you don't have it download BBEdit. The demo version is fully functional for 30 days and then reverts the the “lite” mode, unless purchased and activated with a license.

Whether you buy it or just use the lite version BBEdit is a must for all people working with text on a Mac. It has excellent regular expression support and is one of the most AppleScriptable applications available on the Mac.

I've used it since 1993 or so and am evangelizing it here, because it's a good app for working with regular expressions.

-Chris

Awesome info, thanks for taking the time to point me in the right direction! It looks like I’ll be spending some quality time learning regular expressions, as manipulating text is going to be a big part of what I use Keyboard Maestro for, I think. I’ve used it for a few years already, but mostly just for the macros to simplify some common actions. Now that I’m taking it a step further, I can tell I’m going to have to learn more in the way of scripting to truly see the full potential.

RegEx has a bit of a learning curve, but it is well worth it. I use RegEx in KM, AppleScript, JXA, BBEdit, etc almost daily.

You can learn a few basics that will get you started, and that you can use immediately.

Getting Started with RegEx

  1. Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
  2. Use http://www.RegEx101.com to develop, test, and understand RegEx patterns
  3. Regular Expressions (KM Wiki)

I find the KM RegEx Actions to be user-friendly. There are a number of KM Actions that support RegEx, but these two are the main ones:

Again, I highly recommend RegEx101.com. You can easily sign up for an account there (no ads), and then save all of your RegEx snippets for easy search and use later. When you save a snippet there, you can then post the link to it elsewhere to document it and/or ask for help.

Finally, as with all KM questions, feel free to ask RegEx questions here if you get stumped, or sometimes (as happened to me in the past) when you don't even know how to get started. Of course, you can always Google "RegEx" and some keywords to find solutions (often at stackoverflow.com).