How can I discover the default browser from the command line?

I do not want to install 3rd-party tools, e.g. kerma/defaultbrowser et al.

I assume there's a way to do it with 'defaults' but I'm unable to find any official documentation or user community support.

Posted on Jul 20, 2020 8:33 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 4, 2020 8:38 AM

There is no longer a user friendly command-line entry that returns the default browser name. There are two choices that I am aware of:


# from https://apple.stackexchange.com/questions/313454/applescript-find-the-users-set-default-browser
defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure | awk -F'"' '/http;/{print window[(NR)-1]}{window[NR]=$2}'




which returns com.vendor.browsername (e.g. com.apple.safari) for the default browser.


Or you can put the following into a Bash or Zsh script, mark it executable, and run it from the command-line to return just the default browser name:


#!/bin/zsh

# return default browser name that would open the URL

function default_browser () {
    osascript <<-AS
    use framework "AppKit"
    use AppleScript version "2.4"
    use scripting additions

    property NSWorkspace : a reference to current application's NSWorkspace
    property NSURL : a reference to current application's NSURL

    set wurl to NSURL's URLWithString:"https://www.apple.com"
    set thisBrowser to (NSWorkspace's sharedWorkspace)'s ¬
                        URLForApplicationToOpenURL:wurl
    set appname to (thisBrowser's absoluteString)'s lastPathComponent()'s ¬
                    stringByDeletingPathExtension() as text
    return appname as text
AS
    return
}

printf '%s\n' $(default_browser)
exit 0


chmod +x dbrowser.zsh
./dbrowser.zsh
Safari



6 replies

There are no replies.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I discover the default browser from the command line?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.