Search and Replace Clipboard Contents Using RegEx

I've searched other questions related to this topic but I don't find answers that can help me figure out what I am doing wrong.

What I'm trying to do is pretty simple. I'm trying to use the Search and Replace Clipboard macro to work with strings of the following type:

http://www.url.com/name-of-page-e51c4c1862e9?source=email-21b463b7567-161871024569-digest.reader-4fa64c49202c-e51c9c1762e9----0-73------------------25961964_ed13

What I want to achieve is a clipboard with the following content:

http://www.url.com/name-of-page-e51c4c1862e9

That is, I want to delete everything from the '?' symbol until the end of the string.

The regex I'm using in the search is: ?.*

This is in principle good regex (it works in other applications that use regex, f.ex. BBEdit).

Then I leave the replace field empty (i.e. replace with nothing). This is what usually works in other search and replace scenarios.

When I paste the contents of the clipboard, however, I get the whole string and nothing has been deleted.

What am I doing wrong? Isn't this the way this should work? I'm using the 'Sytem Clipboard' option in the 'to' menu as well. Using the 'source' option does not solve the problem.

I would appreciate any help I can get with this.

JM

You have to escape that question mark: \?.* (it's a metacharacter).

Set Variable to Text.kmmacros (2.9 KB)

OK. Thanks a lot for your answer mparsini. Alas, I think there is still something I don't understand.

I downloaded your macro and it works perfectly. The thing is I was already escaping the question mark. Here's a screen shot of my macro. This would start after I have right clicked on a link with my mouse. The down arrow and return actions are intended to get to the 'Copy link location' and copy that to the clipboard.

That the macro does. No problem. If I do paste, I get the whole URL. The problem comes after the Return action. If you see the screenshot, I'm escaping the '?'. But that exact search and replace that works in your macro, does not work for me here. I see you do something different by using the Set Variable "local var" action. I'm not all that familiar with KM so I don't know exactly what this does, but what's wrong with what I'm doing? If I have the string I need in my system clipboard, why is the search and replace not working?

Thanks a lot again for your answer. I'm sure I will learn something from it.

It’s probably a timing issue: after your Type Return action, KM executes the next action immediately and probably before your Mac has been able to “fill in” the system clipboard. I suggest you insert a Pause action in between the Type Return and Search and Replace actions. A pause of maybe 0.3 seconds should be sufficient - but that’s a value you can experiment with.

1 Like

Yes! Thank you, Tiffle! That did it.

JM

Glad you solved it. BTW, you can avoid the contextual menu dance (at least in Safari) with this:

ss-961

Then paste the variable rather than the Clipboard. No pause required.

1 Like

Hi mpasini,

Thanks again. Even though the problem is solved, I always like to learn new things or do what I already do more efficiently.

Can I ask you what this action does? I understand it avoids having to use a pause. I'm curious to know how this action does all of this.

I've read the KM wiki and I'm not sure I understand what the 'Set variable to text' action is actually doing. As far as I can understand it, in this case we are examining the variable 'H_LinkURL' would be replaced by a URL. Since I've done the 'Copy link' before that, the value for the variable will be the URL I have in my clipboard.

What I don't understand, though, is how the Set variable to text action knows to get that URL from the clipboard. All the token says is SafariURL. Shouldn't say %ClipboardContents% or something like that?I actually happen to get the link from a message in Thunderbird not from Safari but it still works well.

In essence it looks like what the action seems to be doing is simply keeping whatever I have in the clipboard in the clipboard! I tried with a string that didn't even have https:// at the beginning and it still worked.

The obvious advantage it has is that I don't need to introduce a pause. But how does it do it? Why using this as the intermediate step allows you to avoid the pause?

I think if I understand how it works, I'll be better at solving problems myself.

Anyway, thanks a lot for all your help.

JM

That will only be useful if @jfontana wants to work on the URL that Safari has open, as opposed to a link on the page that he’s right-clicked on...

Hi Tadeusz,

Actually this works with whatever one has in the Clipboard, whether it is a URL or not. That's why I asked about how this action works.

With the macro I show in the screenshot, I can copy any string that contains a ? and it will return a clipboard that contains the same string minus the part that the regex search and replace eliminates. That's why I said that what this action seems to do is simply get the contents of the clipboard and nothing more. It does it in such a way, though, that the pause becomes unnecessary. Perhaps it is essentially like just a pause because the assigning of the clipboard text as the value for the variable takes the time that was needed for the search and replace to find something to operate on.

This is the only way I can make sense of what is happening.

Yes - why wouldn't it?

Nope.

This action that Mike posted:

KM 0 2021-04-19_11-46-59

simply transfers the URL that is currently open in Safari (denoted by %SafariURL%) to the variable H_LinkURL. This action does not affect the system clipboard at all.

As Mike said, you don't have to do any right-clicking to achieve this and because the clipboard is not involved, you can use this variable in the your Search and Replace action - hence no delay.

You say you are not getting a delay and that's because there is already a value in the clipboard in your macro as you posted earlier..

Hi Tadeusz,

OK. I got this totally wrong, then. Now your previous answer to mrpasini makes sense to me. Yes, you are right. The URL that I intended to capture and clean is not an URL that is in a Safari open window. I wanted to get links from some newsletters that I receive via e-mail. The idea is to copy those links and clean them using the regex search and replace.

Now that I understand how that works thanks to your explanation, however, I'm really puzzled that I got it to work (at least for a while). I just put the mouse on top of the link in my e-mail client (Thunderbird) and ran the macro using the Set Variable H_LinkURL to %SafariURL% as mrpasini sugested. It opened a new window in Safari with page from the cleaned URL.

I would swear it did this a couple of times. But after I closed the Safari window, I didn't get the macro to work the same way.

JM

No worries Josep! As long as it is now working as you’d hoped, that’s the main thing. If you’re still having problems though, do post the whole macro in future as that will help those trying to help you. For example, all those actions you have that do Down Arrow can be replaced with a single action but that depends on exactly what comes before them in you macro and that is not visible in your screenshot.

You might want to look at this to help with posting...

Ah, so that's what @jfontana was doing. I missed that, sorry. Thanks for the correction.

If he's scraping the newsletter for all of its links, he might like to use something like https://hackertarget.com/extract-links/ (or roll his own) to get them all at once and then process them.

1 Like