You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AppleScript或Zsh脚本获取已注册浏览器列表并切换默认?

获取与切换macOS默认浏览器的脚本方案

一、获取默认浏览器候选应用列表

AppleScript 实现

需要给终端或脚本编辑器开启辅助功能权限(系统设置 > 隐私与安全性 > 辅助功能),然后运行以下脚本:

tell application "System Settings"
    activate
    reveal pane id "com.apple.preference.dock"
end tell

tell application "System Events"
    tell application process "System Settings"
        tell window "Desktop & Dock"
            tell scroll area 1
                tell group 2
                    tell pop up button 1 whose description is "Default web browser"
                        set browserList to value of every menu item of menu 1
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

return browserList

执行后会返回系统默认浏览器下拉菜单里的所有应用名称。

Zsh 命令行实现

通过Launch Services查询系统注册的所有浏览器,输出名称和对应的Bundle ID:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump | grep -A 5 -B 5 "browser" | grep -E "(bundleID|name)" | awk '{print $3}' | paste - -

二、切换默认浏览器的脚本

AppleScript 切换脚本

直接指定目标浏览器名称即可切换,运行后会自动关闭系统设置:

-- 替换成你需要的浏览器名称,比如"Google Chrome"、"Microsoft Edge"
set targetBrowser to "Google Chrome"

tell application "System Events"
    tell application process "System Settings"
        tell window "Desktop & Dock"
            tell scroll area 1
                tell group 2
                    tell pop up button 1 whose description is "Default web browser"
                        click
                        click menu item targetBrowser of menu 1
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

quit application "System Settings"

Zsh 切换脚本

需要提前知道目标浏览器的Bundle ID(比如Chrome是com.google.Chrome,Safari是com.apple.Safari),执行后无需重启系统即可生效:

# 替换为目标浏览器的Bundle ID
TARGET_BUNDLE_ID="com.google.Chrome"

# 更新默认浏览器设置
defaults write com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers -array-add \
    '{LSHandlerContentType = "public.url"; LSHandlerRoleAll = "'"$TARGET_BUNDLE_ID"'";}'

# 重启相关服务生效
killall Finder
killall Dock

三、适配多客户切换场景

可以按以下方式优化使用体验:

  • 为每个客户创建单独的切换脚本,比如switch-clientX-browser.zsh和switch-clientY-browser.zsh,分别填入对应浏览器的Bundle ID。
  • 给脚本添加执行权限:chmod +x switch-clientX-browser.zsh。
  • 将脚本放到~/bin目录,直接在终端调用,或者通过Alfred、Spotlight快速启动切换。

内容的提问来源于stack exchange,提问作者arnaldop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:33:21