Skip to main content
Solved

External Attribute to know default browser


Forum|alt.badge.img+1

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

 

Best answer by YanW

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 original
Did this topic help you find an answer to your question?

Forum|alt.badge.img+11
  • Contributor
  • May 25, 2023

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

YanW
Forum|alt.badge.img+11
  • Contributor
  • May 25, 2023

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
Forum|alt.badge.img+23
YanW wrote:

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>"

 


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]}')

 

 


howie_isaacks
Forum|alt.badge.img+23
athomson wrote:

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

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings