Apple’s Worldwide Developers Conference to kick off June 10 at 10 a.m. PDT with Keynote address

The Keynote will be available to stream on apple.com, the Apple Developer app, the Apple TV app, and the Apple YouTube channel. On-demand playback will be available after the conclusion of the stream.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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 Best 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
Question marked as Best reply

Aug 4, 2020 8:38 AM in response to liam1101

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



Aug 4, 2020 4:10 AM in response to BDAqua

I apologize. I thought I was clear. I want to discover the default browser from the command line without installing any 3rd-party tools such as 'defaultbrowser' by kerma.


The first link is irrelevant because my goal has never been to change my default shell.


The second link, to superuser.com, is one that I've visited before; its solution is woefully outdated and no longer works, i.e. defaults read com.apple.LaunchServices returns nothing.


The aforementioned link also contains references to 'defaultbrowser' (which I explicitly said I don't want) and focuses more on how to set the default browser from the command line.


The fact that any browser available for macOS has the ability to detect that it's not the default browser - as well as providing the ability to change itself to the default browser - demonstrates that there is some underlying, OS-based location where this information is stored. I assumed, perhaps naïvely, that such information would be available via 'defaults' or some *util tool.

Aug 4, 2020 2:44 PM in response to liam1101

You are welcome.


The defaults version is subject to change with future operating system releases as Apple is continually changing stuff. It has happened before. The AppleScript/Objective-C solution in a Bash or Zsh script is not about to change anytime soon — unless Apple deprecates, and removes the NSWorkspace method that I used.

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 ID.