8000 gh-128540: lookup default webbrowser on macOS by minrk · Pull Request #130535 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-128540: lookup default webbrowser on macOS #130535

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next 8000 Next commit
webbrowser: lookup default webbrowser on mac
ensures web browser is launched on mac, even for URLs that are not http[s].
  • Loading branch information
minrk committed Feb 25, 2025
commit b2cd48affe77d27bbf85a7625f7c8777e2db9af7
27 changes: 26 additions & 1 deletion Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,32 @@ def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
url = url.replace('"', '%22')
if self.name == 'default':
script = f'open location "{url}"' # opens in default browser
proto, _sep, _rest = url.partition(":")
if _sep and proto.lower() in {"http", "https"}:
# default web URL, don't need to lookup browser
script = f'open location "{url}"'
else:
# if not a web URL, need to lookup default browser to ensure a browser is launched
# this should always work, but is overkill to lookup http handler
# before launching http
script = f"""
use framework "AppKit"
use AppleScript version "2.4"
use scripting additions

property NSWorkspace : a reference to current application's NSWorkspace
property NSURL : a reference to current application's NSURL

set http_url to NSURL's URLWithString:"https://python.org"
set browser_url to (NSWorkspace's sharedWorkspace)'s ¬
URLForApplicationToOpenURL:http_url
set app_path to browser_url's relativePath as text -- NSURL to absolute path '/Applications/Safari.app'

tell application app_path
activate
open location "{url}"
end tell
"""
else:
script = f'''
tell application "{self.name}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure web browser is launched by {func}`webbrowser.open` on macOS, even for
``file://`` URLs.
0