External Attribute to know default browser

Vijaym09
New Contributor

Hi All,

 

may I know is there any script to know what the value for default browser. 

In the environment there are multiple browsers using, each one sited their own default browser. Need to know what is the default browser seted each Mac.

thanks in advace

 

1 ACCEPTED SOLUTION

YanW
Contributor III

We use this EA

#!/bin/sh

loggedInUser=$(stat -f%Su /dev/console)

defaultBrowser=$(plutil -p /Users/$loggedInUser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | grep 'https' -b3 |awk 'NR==3 {split($4, arr, "\""); print arr[2]}')

echo "<result>$defaultBrowser</result>"

 

View solution in original post

4 REPLIES 4

athomson
New Contributor III

I've been using this for the last couple of years with success. 

#!/bin/bash
# set -x

#   This script can be used within the JSS as an extension attribute script
#   to reveal the last user's default browser identifier (e.g. com.apple.Safari). 

#   Author:        Andrew Thomson
#   Date:          07-08-2019


function lastUserName () {
    
    if ! /usr/bin/defaults read /Library/Preferences/com.apple.loginwindow.plist lastUserName 2> /dev/null; then
        echo "ERROR: Unable to determine last user name."
        return $LINENO
    else
        return 0
    fi
}


function getDefaultBrowser() {

    local USER_NAME="$1"
    local PB="/usr/libexec/PlistBuddy"
    local LS="/Users/$USER_NAME/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
    local COUNT=0

    #   verify user name parameter
    if [ -z "$USER_NAME" ]; then
        echo "ERROR: No user name specified." >&2
        return $LINENO
    fi

    #   verify plist path
    if [ ! -f "$LS" ]; then
        echo "ERROR: Specified plist file not found [$LS]." >&2
        return $LINENO
    fi

    #   count array items for LSHandlers array
    while true ; do
       if ! $PB -c "Print :LSHandlers:$((COUNT++))" "$LS" &> /dev/null; then break; fi
    done

    #   eunmerate array
    for ((INDEX=0; INDEX<=$((COUNT-2)); INDEX++)); do

        #   get URLScheme for each item
        SCHEME=$($PB -c "Print :LSHandlers:$INDEX:LSHandlerURLScheme" "$LS" 2> /dev/null)

        #   if URLScheme is http then get identifier 
        if [ "$SCHEME" == "http" ]; then
            IDENTIFIER=$($PB -c "Print :LSHandlers:$INDEX:LSHandlerRoleAll" "$LS" 2> /dev/null)
            break
        fi
    done

    echo "$IDENTIFIER"
    return 0
}


function main() {

    local USER_NAME="$(lastUserName)"
    local IDENTIFIER="$(getDefaultBrowser "$USER_NAME")"

    if [ -z "$IDENTIFIER" ]; then 
        echo "<result>com.apple.safari</result>"
    else
        echo "<result>$IDENTIFIER</result>"
    fi
}


if [[ "$BASH_SOURCE" == "$0" ]]; then
    main $@
    exit
fi

howie_isaacks
Valued Contributor II

Your EA and the one posted by @YanW both work for me.

YanW
Contributor III

We use this EA

#!/bin/sh

loggedInUser=$(stat -f%Su /dev/console)

defaultBrowser=$(plutil -p /Users/$loggedInUser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | grep 'https' -b3 |awk 'NR==3 {split($4, arr, "\""); print arr[2]}')

echo "<result>$defaultBrowser</result>"

 

howie_isaacks
Valued Contributor II

When I tried this out your command for the "defaultBrowser" variable returned a blank result. On further investigation the string 'http' wasn't in this plist file. I then toggled the default browser setting from Safari to another browser and that got the strings needed to generate. After that your command worked. For fun I used this instead. It  also returned the correct result. How odd that the strings weren't there but my Mac still had Safari set as the default.

 

defaultBrowser=$(defaults read /Users/$loggedInUser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | grep 'https' -b3 | awk 'NR==3 {split($4, arr, "\""); print arr[2]}')