3

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?

2

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

  • 1
    that works. Doesn't give the exact output I'd hope for but I can write a conditional further to define the application. Would prefer an all AppleScript solution but I know sometimes a do shell script is needed. – ʀ2ᴅ2 Jan 25 '18 at 15:58
  • I've realized with Apple it's sometimes better to code it as if default_browser contains "firefox" then. Food for thought but both approaches work. – ʀ2ᴅ2 Jan 25 '18 at 16:10
3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .