MacOS – AppleScript find the user’s set default browser

applescriptmacosweb-browser

I've been doing some research on this topic but it seems Apple has changed their file hierarchy. From the MacScripter post "Get user default browser" it references the line:

 set pListpath to (path to preferences as Unicode text) & "com.apple.LaunchServices.plist"

but when running the handler in Sierra I'm returned an error that the file is empty. Further research I found "How to use an AppleScript app bundle as the default browser in os x?" and "Use default browser in custom Apple Script?" that suggest:

on open location theURL
 ...
end open location

but I'm wanting to preserve the browser in a variable to reference later on in my script. In Sierra is there a file or way to reference what the default browser?

Best Answer

Correct, the file for has changed to ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist.

I'm running macOS 10.13 (High Sierra), but the script should work with Sierra as well. Please report back.

# return default browser in macOS
# https://apple.stackexchange.com/questions/313454/applescript-find-the-users-set-default-browser
# tested with macOS 10.13.3

set default_browser to do shell script "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"

if default_browser is "org.mozilla.firefox" then
    set browser_name to "Firefox"
else if default_browser is "com.apple.safari" then
    set browser_name to "Safari"
else
    set browser_name to "Probably Google Chrome"
end if

The output will look like this:

com.apple.safari

org.mozilla.firefox