Author Topic: How to launch a URL from WinBatch  (Read 152 times)

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 276
How to launch a URL from WinBatch
« on: September 26, 2019, 10:32:30 pm »
This should be simple, but I couldn't find a good way to do it.

At a command prompt, I can do:

C> start http://something

and it will open a new tab in my default browser with the indicated URL.

You would think that "Run" would do it:

; In WB...
Run("http://something","")

but that fails ("WinExec: command not found").

You would then think that RunShell ought to work, but that also fails.

Note: I'm not looking for anything complex, that involves dialogs or CLR or .NET or whatever.  Object*() stuff would be OK, but as far as I can tell, that requires hard-coding the browser name...

Also, this should probably work, but I don't want to "hard-code" the browser EXE:

Run("C:/full/path/to/My/Browser.EXE","http://something")

This *does* work, but it is ugly:

Run("cmd","/c start http://something")

Surely, there is a way to hook into whatever functionality CMD uses to launch the URL?

td

  • Tech Support
  • *****
  • Posts: 3103
    • WinBatch
Re: How to launch a URL from WinBatch
« Reply #1 on: September 26, 2019, 10:53:46 pm »
I don't know but

Code: Winbatch
ShellExecute('https://www.winbatch.com', '','', @Normal, '')
 

works for me.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

  • Sr. Member
  • ****
  • Posts: 291
Re: How to launch a URL from WinBatch
« Reply #2 on: September 27, 2019, 05:44:02 am »
That's what I use.
The mind is everything; What you think, you become.

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 276
Re: How to launch a URL from WinBatch
« Reply #3 on: September 28, 2019, 09:57:23 am »
It doesn't work for me.

Any other ideas?

jmburton2001

  • Jr. Member
  • **
  • Posts: 59
Re: How to launch a URL from WinBatch
« Reply #4 on: September 29, 2019, 07:52:36 am »
My two cents (maybe worth less)...

I use the same exact code that td shows above for URL's, PDF files, txt files, chm files, etc. A few years ago when working on a new computer I had problems with it not invoking URL's or PDF's. I searched and searched and searched for a solution.

I can't remember where I found this, but it was obscure.

Using ShellExecute() or Run() would not work unless I used the full path and executable name. I found that if the "default" program for any given extension isn't properly connected in Windows, the script (i.e. Windows ShellExecute API) doesn't know how to handle it, and therefore it fails. Once I set the "default" programs in Windows, ShellExecute functioned as intended.

YMMV  :)

td

  • Tech Support
  • *****
  • Posts: 3103
    • WinBatch
Re: How to launch a URL from WinBatch
« Reply #5 on: September 30, 2019, 08:27:26 am »
What you are referring to as the "Default" program is actually a Windows shell file/URI association.  File/URI associations are implemented by a series of registry keys and values under HKEY_CLASSES_ROOT in the Windows registry database.  File associations map file extensions or URIs to executables or other system objects.  The Windows shell reads those registry entries to determine which executable or another object to use to perform some action on or with the specified file or URI.  The WinBatch ShellExecute function relies on the Windows shell's ability to sort out the correct executable for the input using the shell's ShellExecuteEx API.  The WinBatch RunShell function is a much more complicated function that also relies to some extent on the same registration information.  However, it can get waylayed because of the complexity and that is why the much simpler ShellExecute function is the better choice for some but not all usages.

It is true that creating a default program for a particular file or URI is often the only requirement necessary on older versions of Windows to launch a program without specifying an actual executable.  However, on newer versions of Windows MSFT saw fit to add a few additional registry values that can cause this mechanism to appear to fail silently.  The simplest known workaround for this new wrinkle in the OS's implementation is to specify a verb in the last parameter to ShellExecute function.  For example, to open a URL in the HTTP namespace you could do something like the following:

Code: Winbatch
ShellExecute('https://www.winbatch.com', '','', @Normal, 'Open')
   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 3103
    • WinBatch
Re: How to launch a URL from WinBatch
« Reply #6 on: October 01, 2019, 07:48:53 am »
A few more details on URI (URL) mapping in the registry on Windows 10:

The mapping for HTTP (there is one for HTTPS too) is under the registry key "HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice".  The value "Progid" under the key contains the application Progid for the default browser.  The Progid can, in turn, be found in the previously mentioned  HKEY_CLASSES_ROOT root key.

Also, here is another approach to starting the system's default browser:

Code: Winbatch
objShell = ObjectCreate("Shell.Application")
objShell.ShellExecute('https://www.winbatch.com')
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade