在 Mac 上以编程方式发现默认浏览器

在 Mac 上以编程方式发现默认浏览器

我想使用命令行来找出哪个应用程序打开了 http 和 https 链接。我需要在脚本中执行此操作。我可以通过执行“defaults read com.apple.LaunchServices”来查看答案,但我不想自己解析该字典。

答案1

Apple Script 具有内置解析属性列表文件的功能。macscripter.net 上的 StefanK 提供了一个代码片段这在前段时间已经帮助了我。您可以轻松地将其保存并作为脚本执行,我将其存储在我的用户 bin 目录中(我将其添加到我的$PATH):

#!/usr/bin/osascript

tell (system attribute "sysv") to set MacOS_version to it mod 4096 div 16
if MacOS_version is 5 then
    set {a1, a2} to {1, 2}
else
    set {a1, a2} to {2, 1}
end if
set pListpath to (path to preferences as Unicode text) & "com.apple.LaunchServices.plist"
tell application "System Events"
    repeat with i in property list items of property list item 1 of contents of property list file pListpath
        if value of property list item a2 of i is "http" then
            return value of property list item a1 of i
        end if
    end repeat
    return "com.apple.Safari"
end tell

答案2

其他选择:

VERSIONER_PERL_PREFER_32_BIT=1 perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]'

tell application "System Events"
    try
        value of property list item "LSHandlerRoleAll" of (property list item 1 of property list item "LSHandlers" of property list file ((path to preferences as text) & "com.apple.LaunchServices.plist") where value of property list items contains "http")
    on error
        "com.apple.safari"
    end try
end tell

http://www.hamsoftengineering.com/codeSharing/defaultApplication/defaultApplication.html

$ DefaultApplication -url http:
/Applications/Safari.app

相关内容