AppleScript find the user's set default browser?

Solution 1:

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

Solution 2:

For OS X 10.10 and later, use the following example AppleScript code:

set defaultBrowser to do shell script "defaults read \\
    ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure \\
    | awk -F'\"' '/http;/{print window[(NR)-1]}{window[NR]=$2}'"

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

Note: The start of the if defaultBrowser statement tests for nothing "" as well as "safari", because, if Safari is the only Browser installed or if another Browser is installed and has never had a default Browser set, then by default nothing will be returned by the do shell script "defaults ..." command, and this means Safari is the default Browser.

In the example AppleScript code above, the value of the defaultBrowser variable initially gets set to one of the following values:

  • Nothing: ""
  • com.apple.safari
  • com.google.chrome
  • org.mozilla.firefox
  • Some other: output

Then within the if statement, the defaultBrowser gets set to the proper name of the Browser. Although you can certainly modify as needed/wanted.

Also note that the do shell script command used herein is more efficient then the other answer, which needlessly uses the plutil command to first convert the binary plist file to an xml plist file and then back to a binary plist file afterwards. It also needlessly uses both grep and awk when both are not necessary since defaults and awk (or just defaults and grep) can do it without all the extra rigmarole.


For pre OS X 10.10, replace:

~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure

With:

com.apple.LaunchServices

In the do shell script "defaults ..." command.