AppleScript find the user's set default 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) &amp; "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?
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.