An Automatic URL Decrufter for Defined Hosts

There's nothing at all wrong with this:

^[^&]*

Although I'd write it like this with a “+” quantifier instead:

^[^&]+

So I know at a glance that more characters are required and not optional.

1 Like

Does Keyboard Maestro form a capture group automatically if none is specified? I guess it must, but I'd never considered it.

-rob.

1 Like

Whups...

I didn't read the details as well as I should have, however for a simple search you can just use a variable in the all All field.

Keyboard Maestro Export

image


Download: RegEx Test ⇢ Search & Replace ⇢ URL-DeCrufter.kmmacros (3.2 KB)

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

1 Like

In case it isn't clear -- there's a difference between what a pattern matches and what a capture group in the pattern will return. So (.*?)&.* does match the entire URL, but the capture group between ( and ) contains the bit you are after.

In more human terms, ^[^&]* is "from the beginning of the string, match as many characters as you can that are not a &, then stop". As you've found, the match can be accessed via the KM action's "All" field, and will be the entire string if no & is present.

(.*?)&.* is "match any string that contains at least one &, and put everything up to (but not including) the first & into capture group 1". The KM action's "All" while be the entire string if the string contains an & (an important difference), while "1" will be everything before the first &.

2 Likes

What confuses me about why this works, unless Lantro changed the structure of the command, is that I specify that capture group one (not the output of the regex) goes into the variable:

I just don't understand how the d_trimmedURL variable would be properly set without specifying the capture group as part of the regex. (This isn't really important, as he has it working now, and my version uses the above structure, which does use the capture group.) Just more curious as to why it's working for him when the structure of the command shows he should wind up with nothing in d_trimmmedURL.

-rob.

I'm guessing that some detail has been lost during the various iterations, and that his "non capture group" version works when putting d_trimmedURL into "All" and, now he's got the parentheses in place, yours is working when going into "1".

As you say, the important bit is that it is working for him!

And 8.4 is out now—sorry for the delay, had some other stuff going on. You should see if it you run the Check for Updates macro in the 8.3 version.

-rob.

2 Likes

Got it! Thanks again!

Version 8.5 is out now, with just one substantive change (flying.com changed their URL for crufty URLs), and updated permanent variable names to match the convention I've used in my other public macros. It's linked above, or the new version can be downloaded by checking for updates within the macro.

-rob.

1 Like

Version 8.6 is out.

__ The Decrufter 8.6 Macros.kmmacros (403 KB)

This is a huge update that removes the use of a large global variable for tracking decrufted URLs. Instead, there's a new database, which is faster, safer, and much easier to use. The database let me remove at least 10 regex search/replace functions that I was using to manage the history variable.) The Decrufter still has a global variable, but now it's a one liner with the path to the database, not a memory-intensive huge list of URLs.

There's also a new curl "progress bar" to let you know that the macro is still waiting on curl for a response. (This also provides a way to cancel the macro if curl gets stuck, as it sometimes will.) I also fixed a lot of other little things to make the macro run faster and fail more nicely.

-rob.

1 Like

I'm having a little trouble with the upgrade. My YouTube special URLS don't get decrufted (no message). I re-added the activation apps listed in 8.5, do I need to re-add the domains too?

Please DM me, we'll figure it out.

-rob.

1 Like

This is awesome Rob.

Friendly typo alert on the ---The Decrufter 8.6--- macro, I think please is misspelled "• If you just want to change how (or whether) copied links are opened in your browser, pelase"...should be "• If you just want to change how (or whether) copied links are opened in your browser, please".

I'm not sure if this is what you have in mind, but what I want from these macros is shortening the URL as much as possible, for example, this is the Decrufted Amazon URL:

https://www.amazon.com/Apple-Generation-Cancelling-Transparency-Personalized/dp/B0CHWRXH8B/ref=sr_1_3

...but I'd prefer something shorter like this:

https://www.amazon.com/dp/B0CHWRXH8B

Similarly, if I have a shortened Best Buy URL like this:

https://www.bestbuy.com/site/6447382.p

...it automatically get Decrufts into a longer URL like this:

https://www.bestbuy.com/site/apple-airpods-pro-2nd-generation-with-magsafe-case-usbc-white/6447382.p

Home Depot support would be great too:

Those are just my thoughts and I completely understand if you prefer longer URLs.

Thanks for the hard work on this - it is amazing.

I wrote this to remove the advertising tracking data from URLs in emails, so I wasn't interested in maximum shortness, especially as the macro works when I copy a link, so I never see or interact with it.

But you could edit it to create whatever versions you like—you'd just need to add custom rules for each URL you want it to handle. But as noted when I posted it originally, it's probably overkill for your needs, as its main focus was in removing the tracking details (so it does things via curl to find the real URL from the crufty one, etc.)

Thanks! Fixed in 8.7, which is hopefully coming shortly.

-rob.

I've been decrufting manually for far too long... thanks Rob!

Wow! Thank you for m making and sharing it @griffman!

An idea for an extension - a lot of links that are shared in mailings and even on Twitter are redirect links. Would it make sense to create an option to generate the resolved links?

Can you provide some examples? I rarely touch Twitter any more.

-rob.

You win by not going to Twitter :). Although, if you follow the right people, it can be a decent discourse on topics of interest.

Here is one example.

link in the email:
https://t.co/ySsz3TqOlI

resolved to:
https://thecobf.com/forum/topic/20517-fairfax-2024/page/42/#google_vignette

wait the above example when I copy in Safari is already getting resolved it seems.


let me find another one.

this one does not trigger the macro I think (not from mail.app at least) - will try to copy in Safari now after creating this post:

https://substack.com/redirect/3e5b76cf-0e0e-46b1-b6ca-0565fe1e7cd0?j=eyJ1IjoiaHE3NHoifQ.-16jlvzMiOlV8IesJVgNd3j5v_WVoFVvRuOT5OCEIqY

to

https://thebearcave.substack.com/p/more-problems-at-hershey-hsy?utm_source=substack&utm_medium=email

t.co hosts are already handled by The Decrufter—when I copied the above URL, it automatically decoded.

EDIT: Look at the macro named 12] Set up domains to decruft in The Decrufter, and in there is a variable that contains all the domains that the macro attempts to decruft.

-rob.

1 Like

Yes they are! This substack redirect link is not.

Also, is it possible to call the recruiter macro from another KM macro?

I have a text replacement macro that replaces ,,slink with the safari macro and that uses a JXA script. I am wondering if there is a way to introduce the decrufter macro into that macro.

Thanks!

Edit: Got it will look at the 12]... macro.
Edit 2: I read the description of the macro some more. Posted too soon - will add substack to it at a later date. The bigger opportunity for me would be to call this macro from the text expansion macro that I use. And thank again!

Edit 3: Sounds like if I add a URL to the system clipboard (from the ,,surl macro) and then invoke 00]... macro, I should find in the system clipboard the output. Will work on that. Unless you have a cleaner solution. Thank you!