How to get script to run non .com URLs too?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

How to get script to run non .com URLs too?

Post by milkygirl90 » 28 Jul 2021, 19:14

Code: Select all

ExtractAddress(str)
{
    if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
        return match1
    if (RegExMatch(str, "S)(www\.\S+)", match))
        return "http://" . match1
    if (RegExMatch(str, "S)(\w+\.(com|net|org|gov|cc|edu|info|ly|us))", match))
        return "http://" . match1
   	return ""
}

  addr := ExtractAddress(clip)
      Run %addr%
    return
  
  
In this example, I can highlight any link and it will go straight to that URL. The only time when it doesn't work is:

1. Non standard domains

Code: Select all

zoom.us
It does a Google search instead of going to that URL

2. Cutting off the rest of the URL

Code: Select all

bit.ly/testing
gets truncated to

Code: Select all

bit.ly
How do I get it to run a URL as long as the ".ly" portion is less than 4 characters, and not truncate my URL?

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: How to get script to run non .com URLs too?

Post by boiler » 28 Jul 2021, 20:03

Instead of just using the URL as the target for the Run command, you can run your browser with the URL as the argument. Example:

Code: Select all

addr := "https://wwf.org"
Run chrome.exe %addr%

Your RegEx pattern that checks the last part (the top level domain) stops after that. To include beyond that, this should do it:

Code: Select all

   if (RegExMatch(str, "S)(\w+\.(com|net|org|gov|cc|edu|info|ly|us)(/\S+)?\b)", match))
Demo:

Code: Select all

Text =
(
This is the URL: bit.ly/testing that we are
looking to extract from these lines
of text.
)

MsgBox, % ExtractAddress(text)

ExtractAddress(str)
{
    if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
        return match1
    if (RegExMatch(str, "S)(www\.\S+)", match))
        return "http://" . match1
    if (RegExMatch(str, "S)(\w+\.(com|net|org|gov|cc|edu|info|ly|us)(/\S+)?\b)", match))
        return "http://" . match1
   	return ""
}

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to get script to run non .com URLs too?

Post by milkygirl90 » 28 Jul 2021, 22:08

you're right, this works nicely.

How about this question below?

How do I get it to run a URL as long as the ".ly" portion is less than 4 characters, and not truncate my URL?

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: How to get script to run non .com URLs too?

Post by boiler » 28 Jul 2021, 22:16

That’s what the second part of my post is about. Maybe I didn’t understand your question. Did you run the demo? Does that not address it?

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to get script to run non .com URLs too?

Post by milkygirl90 » 28 Jul 2021, 22:43

ah I see. I couldn't understand how the code limits it to just 4 chars.. that's why.

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: How to get script to run non .com URLs too?

Post by boiler » 28 Jul 2021, 23:10

It doesn’t. I guess I don’t understand what it’s supposed to do. Limit what to just 4 chars? What’s the reason for the limit? I’m not seeing how the example you gave illustrates that.

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to get script to run non .com URLs too?

Post by milkygirl90 » 29 Jul 2021, 01:35

because domains these days don't just end with .com.

They can end with .us, .biz, .random and so on. Limiting to 4 characters so that I limit the number of false positives (i.e. non websites)..

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: How to get script to run non .com URLs too?

Post by boiler » 29 Jul 2021, 05:34

If you’re re going to allow any 4 letters, then there is no reason for the list (com|net|org|gov|cc|edu|info|ly|us) as they are included in the definition four characters or less (I would make it two to four), so they don’t help limit anything. Like this:

Code: Select all

ExtractAddress(str)
{
    if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
        return match1
    if (RegExMatch(str, "S)(www\.\S+)", match))
        return "http://" . match1
    if (RegExMatch(str, "S)(\w+\.[a-z]{2,4}(/\S+)?\b)", match))
        return "http://" . match1
   	return ""
}

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to get script to run non .com URLs too?

Post by milkygirl90 » 29 Jul 2021, 07:21

yeah.. I couldn't think of a better way to capture all sorts of domains without getting false positives. Thank you for the suggestion!

Post Reply

Return to “Ask for Help (v1)”