如何在 Mac OS X 上的 bash 脚本中获取默认浏览器名称

Gal*_*Gal 5 macos bash shell grep google-chrome

我想在脚本执行之前找出 Mac OS X 机器上的默认浏览器是否是 Google Chrome。

我该怎么做?谢谢!

l'L*_*L'l 8

您可以grep/awk在启动服务首选项列表中找出默认设置的浏览器:

x=~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist; \
plutil -convert xml1 $x; \
grep 'https' -b3 $x | awk 'NR==2 {split($2, arr, "[><]"); print arr[3]}'; \
plutil -convert binary1 $x
Run Code Online (Sandbox Code Playgroud)

这将一个变量 ( x) 设置为启动服务首选项列表,然后使用 将其转换plutilxml格式,以便我们可以使用grep它。我们找到要查找的字符串 ( https) 然后输出结果。最后一步是将 plist 转换回binary格式。

如果 chrome 设置为默认值,您将获得:

结果

com.google.chrome
Run Code Online (Sandbox Code Playgroud)