3

This question asks how to open an HTML file in the default browser on Mac OS.

There is a useful answer which refers to this infamous bit of Perl:

VERSIONER_PERL_PREFER_32_BIT=true perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]'

Here's some working Python code:

import shlex, subprocess

env = {'VERSIONER_PERL_PREFER_32_BIT': 'true'}
raw = """perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]'"""
process = subprocess.Popen(shlex.split(raw), env=env, stdout=subprocess.PIPE)
out, err = process.communicate()

default_browser = out.strip()

Is there a more direct way?

5
  • 1
    Read this docs.python.org/2/library/webbrowser.html Commented May 6, 2014 at 23:13
  • Doesn't work for file URLs. Commented May 6, 2014 at 23:21
  • Just prefix the path with file:// Commented May 6, 2014 at 23:34
  • To elaborate, given a file URL it has the same problem as open: it invokes the default program for html files, which for me is Sublime, not Chrome. There's a good explanation in this answer: stackoverflow.com/a/10250717/893113 Commented May 6, 2014 at 23:39
  • For the record, modern Python would use subprocess.check_output of subprocess.run instead of the multi-line Popen song and dance. Commented Nov 18, 2024 at 15:07

3 Answers 3

2

Here's a Python solution using pyobjc:

from Foundation import CFPreferencesCopyAppValue

handlers = CFPreferencesCopyAppValue('LSHandlers', 'com.apple.LaunchServices')

try:
    handler = next(x for x in handlers if x.get('LSHandlerURLScheme') == 'http')
    bundle_identifier = handler.get('LSHandlerRoleAll')
except StopIteration:
    pass

This returns the bundle identifier, which you can use with the -b argument to open.

Sign up to request clarification or add additional context in comments.

Comments

1

You can read the "property list" file using plistlib from the standard library:

from pathlib import Path
import plistlib

PREFERENCES = (
    Path.home()
    / "Library"
    / "Preferences"
    / "com.apple.LaunchServices/com.apple.launchservices.secure.plist"
)

NAMES = {
    "com.apple.safari": "Safari",
    "com.google.chrome": "Google Chrome",
    "org.mozilla.firefox": "Firefox",
}

with PREFERENCES.open("rb") as fp:
    data = plistlib.load(fp)

for handler in data["LSHandlers"]:
    if handler.get("LSHandlerURLScheme") == "http":
        role = handler["LSHandlerRoleAll"]
        name = NAMES[role]
        print(name)

Comments

0

I think this might be useful here.

webbrowser is inbuilt python library

import webbrowser
import os

# Path to your HTML file
html_file_path = "/path/to/your/file.html"

# Open the HTML file in the default web browser
webbrowser.open(f"file://{os.path.abspath(html_file_path)}")

Optional: You can specify the webbrowser as well

# Replace 'chrome', 'firefox', or 'safari' with the appropriate browser name
browser = webbrowser.get('chrome')
# Then do open
browser.open(f"file://{os.path.abspath(html_file_path)}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.