This question询问如何在 Mac 操作系统的默认浏览器中打开 HTML 文件。
有一个useful answer它指的是 Perl 的这个臭名昭著的部分:
VERSIONER_PERL_PREFER_32_BIT=true perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]'
这是一些有效的 Python 代码:
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()
有没有更直接的方法?
最佳答案
这是一个使用 pyobjc 的 Python 解决方案:
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
这会返回包标识符,您可以将其与 -b
参数一起用于 open
。
关于python - 在 Python 中确定 Mac 默认浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506033/