How to Find a Specific Character in a String or Hyperlink and Place Mouse After It?

Okay, so let's see if I can explain this well enough...

I'm going a lengthy project for which the rest of the macro is written.
However, there is a certain point where I have to use the "Pause Until" function, and manually Select a string of text, and then the Macro will continue until the end.
The reason that I can't make this text selection part of the master macro is that the URL I'm trying to clip a 10-character string from, keeps changing it's length based on the name of the file.

So, after the hyperlink gets pasted into the Mac "TextEdit" program, here is what I'm trying to accomplish via Keyboard Maestro Macro:

  1. Find the "x" occurrence (first, second, last...whatever) of "=" (I am looking for the actual equal sign),
  2. Place the cursor directly after the "x" occurrence of the "=" sign
  3. Left-Click the Mouse (and hold), go "y" spaces to the right of that initial location, release the Mouse (thus selecting that portion of text for Copying).

There might be another (simpler?) option that I'll mention.
The 10-character string that I need to select/copy is at the end of the URL, and the macro currently places the CURSOR at the end of the URL.
SO...the problem then becomes that the Click/Move options currently available are all relative to the MOUSE's position....NOT the Current CURSOR position.

Is there a way to way to "MOVE MOUSE" to the Current CURSOR position?

That's all I need for this final piece of the puzzle so that I'm no longer tethered to the computer for this one, simple, 5-second, manual selection process in the middle of my macro.

Because KM is SO feature rich, I'm lost as to which of it's many functions/sub-categories I'd even begin to investigate to accomplish this small task, so figured I'd ask the group.

Thanks for any/all help.

I think you could likely use a search using regular expression action, and search the %FrontBrowserURL% token. See my example below to see how you could capture everything after the third = sign in a URL.

I’m on a time crunch right now, but let me know if you don’t understand what’s going on and I can explain it further later tonight or tomorrow.

-Chris

Macro Screenshot (click to expand/collapse)

Scratchpad.kmmacros (7.6 KB)

Wow... just clicked on that and that is intense (at least for a newbie like me).

Will need some time to sort through that to see if it's what I need, if I understand what the steps, and thus if I need more information/tweaking.

Thank you so much for doing that. I'm right in the middle of a different project, but am looking forward to seeing if that script will do what I'm looking for.

Thank you, again, for taking the time to script that.

Will keep you posted.

Best regards,

-J

1 Like

FWIW, there’s really only one action you would need; the one titled “This is the actual action to use...”.

The rest are there just because it’s a sample macro. Also, the following RegEx might be better suited to find text specifically after the third iteration of an = sign.

^(?:[^=]+=){3}([^=]+)

But to break down action itself ↓

Action Screenshot (click to expand/collapse)

The %FrontBrowserURL% token represents the URL for whatever the front window of the front browser is. Right now, for me, it’s the following:

https://forum.keyboardmaestro.com/t/how-to-find-a-specific-character-in-a-string-or-hyperlink-then-place-mouse-after-it/30446/2

Now then, the RegEx (Regular Expression) basically looks for anything after the third = sign, but before a 4th = sign. Here’s what each piece means:

^ starts at the beginning of the URL
(?:) non-capturing group, in other words, it wants to find something, but not use it
[^=] any character that is not an = sign
+ at least one of the previous character (again, anything that is not an = sign)
= followed by a literal = sign
{3} match that entire pattern exactly three times
([^=]+) again capture everything that follows that is not an = sign

In other words, capture every character after the third = sign, but before the fourth = sign if there is another one. If you want to tinker with it, this site (RegEx 101) will let you test RegEx live.

1 Like

While @cdthomer is right in that you can use a regex for this, pattern matching can be confusing and in simple cases it's often worth looking for other methods -- if only to stop your brain exploding!

If that's right and you only ever need the last 10 characters of the URL, KM has a "Substring" action which is much easier to understand. Here's an example that'll display the last 10 characters of the URL of the active tab/window of the frontmost browser:

Last 10 Characters of URL.kmmacros (2.6 KB)

If things are more complicated you can do similar using multiple substring actions or by treating the URL string as an array with custom delimiters. So to get the text between the 3rd and 4th = in https://www.example.com/part1=part2=part3=Text to extract=part5=stuffAtEnd:

Text Between 3rd and 4th Equal Signs.kmmacros (2.4 KB)

If your needs are more complicated then it'll really help if you provide examples of the text/URLs and what you want to extract.

1 Like

I learned something new today! Thanks for pointing this out because I rarely work with URLs so I am not too familiar with what KM actions are best-suited for this kind of thing.

Useful for any string for which you know the indices of the text you want to extract. Consider a log where every line starts with Status: -- you could get the important (to you!) part of each line of with Substring: from 9:

Substrings Demo 3.kmmacros (2.7 KB)

Image

Substrings are much more efficient than the alternatives (you could do the above by search'n'replace with "Status: " and "", or a regex for "everything after the first space", etc), so worth using when you can.

Thanks for posting this.

Was just about to run out the door to work, but saw this, downloaded it, assigned a hot key, made sure that Chrome was active and then hit the hot key, and nothing happened. Am I supposed to see that results somewhere?

Thanks again.

  • J.

Should display the last 10 characters of the URL in a text window -- works here in with both Chrome and Safari. Try it by loading a page in Chrome then switching back to KM and hitting the "Run" button, in case you've a hotkey conflict.

Make sure the macro you installed and it's container macro-group are enabled.

Okay... while this script won't run by itself when activated by a Hot Key (for whatever reason), it DOES run within another macro/script, and does give me the result that I'm looking for... so, thanks for all of the help getting to this point!

Now... having said that, is there a way to shave a couple of steps off of my macro, and instead of actually having to load the URL into Chrome, load the page, then get the Last 10 Characters from the current front-window URL... is there a way to re-tool this script to just look at the last item on the CLIPBOARD (which would be the TARGET URL in question), and have this script just snag the Last 10 Characters from the string that is the most recent on the CLIPBOARD?

This would save me some steps, as currently when I load the URL into Chrome and load the page, I'm given an error message from the site saying I'm not logged in (even though I am), so I'd like to take Chrome out of the equation during the "Get Last 10 Characters" script, if I could, and just have the script get the Last 10 Characters from the URL that is currently on the Clipboard.

Is this possible?...

That usually means that while the macro is enabled either the Group containing it isn't or the Group has been set to only available/active in certain situations -- situations that aren't "true" when you get the "bloop" sound.

Check the Group settings -- below is "most permissive", so maybe start there and then incrementally tighten them up to want you need:

image

Just change the first action to:

image

KM's Tokens are really versatile -- take a look at the list on the Wiki to see what's available.

Success!....

Thanks to you, and everybody who chimed in, to help me fix this issue!

This program is a BEAST, and sort of intimidating for a newcomer like me, who knows that whatever I want to get accomplished CAN be done (because computers will do whatever we tell them to...that is until they become self-aware...but I digress), but because I'm not a coder, a lot of this stuff is still foreign to me, and going to take some time to shorten the learning curve....but I can only imagine what one can accomplish with this program, having seen the very little that I have so far.

Thanks again for all of the help!

Best regards,

  • J
1 Like