Skip to content

webbrowser.py cannot use a non-standard browser under MacOS #113539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mmaass-x opened this issue Dec 28, 2023 · 8 comments
Closed

webbrowser.py cannot use a non-standard browser under MacOS #113539

mmaass-x opened this issue Dec 28, 2023 · 8 comments
Labels
OS-mac stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mmaass-x
Copy link

mmaass-x commented Dec 28, 2023

Bug report

Bug description:

webbrowser.py reads the environment variable "BROWSER" to find executable using "_synthesize".
This does not work with macOS, where the browser is open by either

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome

or

open -a "Google Chrome"

it doesn't match the list of configured browsers

Workaround is to create a zsh script in the path called 'chrome':

#!/bin/zsh
#
open -a "Google Chrome.app" $

"_synthesize" needs to be fixed.

CPython versions tested on:

3.11

Operating systems tested on:

macOS

Linked PRs

@mmaass-x mmaass-x added the type-bug An unexpected behavior, bug, or error label Dec 28, 2023
@ronaldoussoren
Copy link
Contributor

It might be better to tweak the code that calls _synthesize, using the already registered browsers when available (which would lead to using the registration for MacOSXOSAScript at the start of register_standard_browsers. That likely needs changes as well because this currently tries to start chrome using the following shell command:

osascript -e 'tell application "chrome" 
                         activate
                         open location "https://www.python.org"
                       end'

I don't have Chrome installed, could you check if this command opens the python website for you? If not, try the same with "chrome" replaced by "google chrome".

@sobolevn
Copy link
Member

@ronaldoussoren I have chrome installed on macos, it does not work for me:

» osascript -e 'tell application "chrome" 
                         activate
                         open location "https://www.python.org"
                       end'
52:60: execution error: Cannot open application "chrome". (-1728)

But, this one works:

» osascript -e 'tell application "google chrome" 
                         activate
                         open location "https://www.python.org"
                       end'

Also works with: firefox, safari (I don't have any other browsers).

@ronaldoussoren
Copy link
Contributor

The diff below seems to work, but I haven't fully thought through the consequences on platforms other than macOS. That's why I haven't created a PR at this time.

diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
index 636e8ca459..310c6d58ce 100755
--- a/Lib/webbrowser.py
+++ b/Lib/webbrowser.py
@@ -114,7 +114,21 @@ def _synthesize(browser, *, preferred=False):
     executable for the requested browser, return [None, None].
 
     """
-    cmd = browser.split()[0]
+    parts = browser.split()
+    if len(parts) == 1:
+        # Assume this the name of a registered command, use
+        # that unless it is a GenericBrowser.
+        try:
+            command = _browsers[browser.lower()]
+        except KeyError:
+            pass
+
+        else:
+            if not isinstance(command[1], GenericBrowser):
+                _tryorder.insert(0, browser.lower())
+                return [None, command[1]]
+
+    cmd = parts[0]
     if not shutil.which(cmd):
         return [None, None]
     name = os.path.basename(cmd)
@@ -466,9 +480,10 @@ def register_standard_browsers():
 
     if sys.platform == 'darwin':
         register("MacOSX", None, MacOSXOSAScript('default'))
-        register("chrome", None, MacOSXOSAScript('chrome'))
+        register("chrome", None, MacOSXOSAScript('google chrome'))
         register("firefox", None, MacOSXOSAScript('firefox'))
         register("safari", None, MacOSXOSAScript('safari'))
+        register("safari-preview", None, MacOSXOSAScript('safari technology preview'))
         # OS X can use below Unix support (but we prefer using the OS X
         # specific stuff)

Ignore the registration of "safari-preview", I added that because that's the only alternative browser on the system I'm working on. A PR for this issue should not contain that.

@ronaldoussoren
Copy link
Contributor

I've added a PR and I'm being very conservative with this: the PR phrasing and documentation changes assume this is a new feature and not a bug fix.

The reason is that the documentation for webbrowser module mentions that values in the $BROWSER search list refer to command-lines:

If the environment variable BROWSER exists, it is interpreted as the os.pathsep-separated list of browsers to try ahead of the platform defaults. When the value of a list part contains the string %s, then it is interpreted as a literal browser command line to be used with the argument URL substituted for %s; if the part does not contain %s, it is simply interpreted as the name of the browser to launch. 1

@micseydel
Copy link

Any update or recommended workaround for this? @sobolevn's osascript works for me by itself, and from within a script, but the webbrowser module doesn't seem to use it even when it's on my PATH.

@picnixz picnixz added the stdlib Python modules in the Lib dir label Apr 18, 2025
hugovk added a commit that referenced this issue Apr 25, 2025
…n webbrowser.py (#113561)

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hugovk
Copy link
Member

hugovk commented Apr 25, 2025

This will be in the next release (3.14.0b1) in a couple of weeks.

@hugovk hugovk closed this as completed Apr 25, 2025
@micseydel
Copy link

@hugovk thanks for the update. Is there a recommended workaround for affected versions?

@hugovk
Copy link
Member

hugovk commented Apr 26, 2025

No official recommendation, you could patch the module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-mac stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants