本文目录导读:

我来介绍几种在不同操作系统中用脚本设置默认浏览器的方法。
Windows 系统
方法1:使用 PowerShell 脚本
# 设置 Chrome 为默认浏览器 $chromePath = "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" Set-ItemProperty -Path $chromePath -Name "Progid" -Value "ChromeHTML" Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice" -Name "Progid" -Value "ChromeHTML" # 设置 Edge 为默认浏览器 Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" -Name "Progid" -Value "MSEdgeHTM" Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice" -Name "Progid" -Value "MSEdgeHTM"
方法2:使用 CMD 批处理脚本
@echo off :: 设置 Chrome 为默认浏览器 start chrome --make-default-browser :: 或者使用 Windows 设置应用 start ms-settings:defaultapps
macOS 系统
使用 AppleScript 脚本
-- 设置 Chrome 为默认浏览器
tell application "System Events"
set theApp to "Google Chrome.app"
set bundleID to "com.google.Chrome"
-- 使用 duti 工具或通过系统偏好设置
do shell script "open -a 'System Preferences'"
end tell
-- 更简单的方式:使用 SwiftDialog 或手动设置
使用 shell 脚本(需要安装 duti)
#!/bin/bash # 首先安装 duti: brew install duti # 设置 Chrome 为默认浏览器 duti -s com.google.Chrome http duti -s com.google.Chrome https duti -s com.google.Chrome ftp duti -s com.google.Chrome mailto # 设置 Firefox 为默认浏览器 # duti -s org.mozilla.firefox http # duti -s org.mozilla.firefox https
Linux 系统
使用 xdg-settings
#!/bin/bash # 设置默认浏览器 xdg-settings set default-web-browser google-chrome.desktop # 其他选项: # xdg-settings set default-web-browser firefox.desktop # xdg-settings set default-web-browser microsoft-edge.desktop # xdg-settings set default-web-browser chromium-browser.desktop # 查看当前默认浏览器 xdg-settings get default-web-browser
使用 update-alternatives(Debian/Ubuntu)
#!/bin/bash # 设置默认浏览器 sudo update-alternatives --set x-www-browser /usr/bin/google-chrome sudo update-alternatives --set www-browser /usr/bin/google-chrome
跨平台 Python 脚本
import platform
import subprocess
import os
def set_default_browser(browser="chrome"):
system = platform.system()
if system == "Windows":
browser_progids = {
"chrome": "ChromeHTML",
"edge": "MSEdgeHTM",
"firefox": "FirefoxURL-308046B0AF4A39CB"
}
progid = browser_progids.get(browser)
if progid:
import winreg
with winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice",
0, winreg.KEY_SET_VALUE) as key:
winreg.SetValueEx(key, "Progid", 0, winreg.REG_SZ, progid)
elif system == "Darwin": # macOS
browser_bundles = {
"chrome": "com.google.Chrome",
"firefox": "org.mozilla.firefox",
"safari": "com.apple.Safari"
}
bundle_id = browser_bundles.get(browser)
if bundle_id:
# 需要 duti 工具
subprocess.run(["duti", "-s", bundle_id, "http"])
subprocess.run(["duti", "-s", bundle_id, "https"])
elif system == "Linux":
browser_desktop = {
"chrome": "google-chrome.desktop",
"firefox": "firefox.desktop",
"chromium": "chromium-browser.desktop"
}
desktop_file = browser_desktop.get(browser)
if desktop_file:
subprocess.run(["xdg-settings", "set",
"default-web-browser", desktop_file])
# 使用示例
set_default_browser("chrome")
注意事项
- 管理员权限:Windows 上可能需要管理员权限运行
- 用户同意:某些系统会提示用户确认更改
- 浏览器安装:确保目标浏览器已安装
- 系统兼容性:Windows 10/11 可能有额外的安全限制
选择适合你系统的脚本方法即可!