Stripping Tracker Portions From a URL (Clean URL Parameters)

Another method... I use this AppleScript to expand shortened URLs and then remove Google tracking codes from it. I have it in a KM macro that then pastes the results. This lets important non-tracking-related parameters in the URL survive the experience, rather than just killing all parameters.

Expand and Clean (5.4 KB)

An alternative (that's less fun) is the StretchLink app, which does all of this quite nicely. (It was the inspiration for my script.)

1 Like

I made this:
[g] OnClipboardChange.kmmacros (9.3 KB)

I bet it can be optimised, but there we go.

In the case of an Amazon URL it still contains a reference ID, for example "ref=sr_1_5"

How do I remove that as well?

Pre-Processed URL

Processed URL

Desired URL

Use a Search and Replace action.

Search Clipboard with RegEx:

Replace with empty string.

For a bit broader search, you could use:

that would catch (match) any query string that starts with a unicode letter and then zero or more letters or digits.

For more info, see Unicode Regular Expressions.

Thank you — I also utilized the regex logic provided by @peternlewis


I just noticed that when I use this, it adds a line break after the URL when pasting into Google Doc.

How do I resolve this?

The KM Search and Replace did NOT add anything. It removed all text from "ref=" to the end of the string.

Sounds like there was already a CR or LF in the string, which should not have been if you just copied the URL

To allow for a end of line character, you could use this:

\R is the same as (?:\r?\n|\r)

When I remove the last action that removes the reference number - the extra line issue goes away

Note: I tried your the regex you just provided, but to no avail

Please post your exact macro, image and file. Can't diagnose what I can't see.


@heyJoeCampbell, I'm not sure what you are doing, but looks like the problem may be in the paste to Google docs.

When I try this:

Set Chrome to


and paste into BBEdit, I get this:
with no CR or LF at the end.

Try pasting into Gmail - I also experienced the linebreak there as well

Sorry, I've given you a solution that does NOT have a linebreak on the clipboard.
I'm not going to try to diagnose issues with gmail or google docs in using that data.
If you use a KM Macro to do the paste, then just add a Type Keystroke of backspace after the paste.

Thank you for your assistance

I figured it out, moving the ref=.*/\1/ action before the %Variable%Chrome__URL% action resolved the rogue line break issue

Thanks again for the assistance :grinning:


This doesn't make any sense.
This is not a valid RegEx:

Your last action overwrites the Clipboard, making all of the above changes useless.
None of your actions address the "rogue line break issue"

But if it works for you, then fine. I guess we're done here.

Caution to other readers: I do not recommend the solution posted by @heyJoeCampbell.



Only 5 yrs late to the party. Does anyone maintain a repository for KM Macros and so we can browse and find the latest versions of them? I see this Macro here and think it likely some improvements. There are hundreds of other macros here that feel like orphans.

I suggest:

Hey Mark,

The short answer is nyet.

If there were I imagine @Peter_Morgan would have mentioned it.

The Keyboard Maestro Forum is a user-forum not a library. Many macros are offered for posterity and are not maintained. Many macros are offered to help accomplish someone's task du jour.

Only a few macros on the forum are regularly maintained and/or augmented by their authors.

You can find a few of them in this listing:

Best Macro List


1 Like