由于我最近经常更改默认网络浏览器,我想知道是否有办法专门解决您的默认浏览器。我只能找到一个 10 year old Stackoverflow-post 提到用当前的邮件客户端做类似的事情,但我没有找到关于默认浏览器的任何信息。我想使用默认浏览器也有类似的方法,但我并不真正理解已接受答案的代码。
答案 0 :(得分:1)
在 macOS 中有一段时间了,~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist 文件 包含有关使用 HTTP 和 HTTPS 协议的网址的默认浏览器的信息。
以下示例 AppleScript 代码 解析该文件的 http; 和 https; 并提供了一些(不是全部)逻辑,以根据返回的内容执行您想要的操作。按照编码,它倾向于将两者设置为相同的值,如果不是,则需要添加额外的逻辑以适应该场景。
set defaultBrowserList to paragraphs of (do shell script "defaults read \\
~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure \\
| awk -F'\"' '/http;|https;/{print window[(NR)-1]}{window[NR]=$2}'")
if (count defaultBrowserList) is not equal to 2 then return
if item 1 of defaultBrowserList is equal to item 2 of defaultBrowserList then
set defaultBrowser to item 1 of defaultBrowserList
else
return
end if
if defaultBrowser is "" or defaultBrowser contains "safari" then
-- # The default Browser is Safari.
set defaultBrowser to "Safari"
-- # Your code goes here.
else if defaultBrowser contains "chrome" then
-- # The default Browser is Google Chrome.
set defaultBrowser to "Google Chrome"
-- # Your code goes here.
else if defaultBrowser contains "firefox" then
-- # The default Browser is Firefox.
set defaultBrowser to "Firefox"
-- # Your code goes here.
else
set defaultBrowser to "Other"
-- # Your code goes here.
end if
注意事项:
if defaultBrowser
语句 的开始测试 ""
和 "safari"
,因为如果 Safari 是只安装了浏览器,或者如果安装了另一个浏览器并且从未设置过默认的浏览器,那么默认情况下,{{ 1}} command,这意味着 Safari 是默认的浏览器。
在上面的示例 AppleScript 代码中,do shell script "defaults ..."
的值变量最初设置为以下值之一:
defaultBrowser
""
com.apple.safari
com.google.chrome
org.mozilla.firefox
然后在 output
statement 中,if
被设置为 浏览器 的专有名称。虽然您当然可以根据需要/想要进行修改。
示例 AppleScript 代码,如上所示,在 macOS Catalina 下的 脚本编辑器 中进行了测试 将系统偏好设置中的语言和地区设置设为英语(美国) - 主要并且为我工作没有问题 1.
对于 OS X 10.10 之前的版本,替换:
defaultBrowser
与:
~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure
在 com.apple.LaunchServices
命令中。
注意:示例 AppleScript 代码就是这样,没有任何包含的错误处理不包含任何额外的错误处理。用户有责任根据需要添加任何错误处理。查看 try 中的 error statement 和 AppleScript Language Guide statement。另见Working with Errors。此外,在适当的情况下,可能需要在事件之间使用 delay command,例如do shell script "defaults ..."
,适当设置 delay 的 value。