Set Microsoft Edge as Default Browser

adl-gavinator
New Contributor III

Hi All,

I need to find a way to set MacOS to use Microsoft Edge as the default browser.  Either by script or config profile.  Alot of auth/token services do not work with Safari on Monterey so would like to avoid using it altogether.  

We would also like to give users the option to move to a MacOS device and a good attraction is the ability to sign into Edge (which they used on their Windows machines) and have their favourites, browsing history and cookies appear on the MacOS version of Edge. 

Any help would be greatly appreciated. 

Gavin

1 ACCEPTED SOLUTION

dmccluskey
Contributor II

#!/bin/bash

###
# https://github.com/palantir/jamf-pro-scripts/blob/main/scripts/Set%20Default%20Browser%20and%20Email...
# Name: Set Default Browser and Email Client.sh
# Description: Sets default browser and email client for currently logged-in user.
# Created: 2017-09-06
# Last Modified: 2020-07-08
# Version: 1.4.3
#
#
# Copyright 2017 Palantir Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
###

 

########## variable-ing ##########

 

# Jamf Pro script parameter "Browser Agent String"
# Should be in the format domain.vendor.app (e.g. com.apple.safari).
browserAgentString="$4"
# Jamf Pro script parameter "Email Agent String"
# Should be in the format domain.vendor.app (e.g. com.apple.mail).
emailAgentString="$5"
loggedInUser=$(/usr/bin/stat -f%Su "/dev/console")
loggedInUserHome=$(/usr/bin/dscl . -read "/Users/$loggedInUser" NFSHomeDirectory | /usr/bin/awk '{print $NF}')
launchServicesPlistFolder="$loggedInUserHome/Library/Preferences/com.apple.LaunchServices"
launchServicesPlist="$launchServicesPlistFolder/com.apple.launchservices.secure.plist"
plistbuddyPath="/usr/libexec/PlistBuddy"
plistbuddyPreferences=(
"Add :LSHandlers:0:LSHandlerRoleAll string $browserAgentString"
"Add :LSHandlers:0:LSHandlerURLScheme string http"
"Add :LSHandlers:1:LSHandlerRoleAll string $browserAgentString"
"Add :LSHandlers:1:LSHandlerURLScheme string https"
"Add :LSHandlers:2:LSHandlerRoleViewer string $browserAgentString"
"Add :LSHandlers:2:LSHandlerContentType string public.html"
"Add :LSHandlers:3:LSHandlerRoleViewer string $browserAgentString"
"Add :LSHandlers:3:LSHandlerContentType string public.url"
"Add :LSHandlers:4:LSHandlerRoleViewer string $browserAgentString"
"Add :LSHandlers:4:LSHandlerContentType string public.xhtml"
"Add :LSHandlers:5:LSHandlerRoleAll string $emailAgentString"
"Add :LSHandlers:5:LSHandlerURLScheme string mailto"
)
lsregisterPath="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

 

########## function-ing ##########

 

# Exits if any required Jamf Pro arguments are undefined.
function check_jamf_pro_arguments {
jamfProArguments=(
"$browserAgentString"
"$emailAgentString"
)
for argument in "${jamfProArguments[@]}"; do
if [[ -z "$argument" ]]; then
echo " ERROR: Undefined Jamf Pro argument, unable to proceed."
exit 74
fi
done
}

 

########## main process ##########

 

# Exit if any required Jamf Pro arguments are undefined.
check_jamf_pro_arguments


# Clear out LSHandlers array data from $launchServicesPlist, or create new plist if file does not exist.
if [[ -e "$launchServicesPlist" ]]; then
"$plistbuddyPath" -c "Delete :LSHandlers" "$launchServicesPlist"
echo "Reset LSHandlers array from $launchServicesPlist."
else
/bin/mkdir -p "$launchServicesPlistFolder"
"$plistbuddyPath" -c "Save" "$launchServicesPlist"
echo "Created $launchServicesPlist."
fi


# Add new LSHandlers array.
"$plistbuddyPath" -c "Add :LSHandlers array" "$launchServicesPlist"
echo "Initialized LSHandlers array."


# Set handler for each URL scheme and content type to specified browser and email client.
for plistbuddyCommand in "${plistbuddyPreferences[@]}"; do
"$plistbuddyPath" -c "$plistbuddyCommand" "$launchServicesPlist"
if [[ "$plistbuddyCommand" = *"$browserAgentString"* ]] || [[ "$plistbuddyCommand" = *"$emailAgentString"* ]]; then
arrayEntry=$(echo "$plistbuddyCommand" | /usr/bin/awk -F: '{print $2 ":" $3 ":" $4}' | /usr/bin/sed 's/ .*//')
prefLabel=$(echo "$plistbuddyCommand" | /usr/bin/awk '{print $4}')
echo "Set $arrayEntry to $prefLabel."
fi
done


# Fix permissions on $launchServicesPlistFolder.
/usr/sbin/chown -R "$loggedInUser" "$launchServicesPlistFolder"
echo "Fixed permissions on $launchServicesPlistFolder."


# Reset Launch Services database.
"$lsregisterPath" -kill -r -domain local -domain system -domain user
echo "Reset Launch Services database. A restart may also be required for these new default client changes to take effect."

 

exit 0

View solution in original post

12 REPLIES 12

Rocketman_Zac
New Contributor II

Hello Gavin,

This may be useful for setting Edge as the default browser: https://github.com/targendaz2/Mac-Set-Default-Apps

Looking for a Jamf Managed Service Provider? Look no further than Rocketman

Hi @Rocketman_Zac unfortunately this script does not work for python3.  It is written in python2 I believe.

@adl-gavinator Great catch. Thanks for the heads up! 

Looking for a Jamf Managed Service Provider? Look no further than Rocketman

dmccluskey
Contributor II

#!/bin/bash

###
# https://github.com/palantir/jamf-pro-scripts/blob/main/scripts/Set%20Default%20Browser%20and%20Email...
# Name: Set Default Browser and Email Client.sh
# Description: Sets default browser and email client for currently logged-in user.
# Created: 2017-09-06
# Last Modified: 2020-07-08
# Version: 1.4.3
#
#
# Copyright 2017 Palantir Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
###

 

########## variable-ing ##########

 

# Jamf Pro script parameter "Browser Agent String"
# Should be in the format domain.vendor.app (e.g. com.apple.safari).
browserAgentString="$4"
# Jamf Pro script parameter "Email Agent String"
# Should be in the format domain.vendor.app (e.g. com.apple.mail).
emailAgentString="$5"
loggedInUser=$(/usr/bin/stat -f%Su "/dev/console")
loggedInUserHome=$(/usr/bin/dscl . -read "/Users/$loggedInUser" NFSHomeDirectory | /usr/bin/awk '{print $NF}')
launchServicesPlistFolder="$loggedInUserHome/Library/Preferences/com.apple.LaunchServices"
launchServicesPlist="$launchServicesPlistFolder/com.apple.launchservices.secure.plist"
plistbuddyPath="/usr/libexec/PlistBuddy"
plistbuddyPreferences=(
"Add :LSHandlers:0:LSHandlerRoleAll string $browserAgentString"
"Add :LSHandlers:0:LSHandlerURLScheme string http"
"Add :LSHandlers:1:LSHandlerRoleAll string $browserAgentString"
"Add :LSHandlers:1:LSHandlerURLScheme string https"
"Add :LSHandlers:2:LSHandlerRoleViewer string $browserAgentString"
"Add :LSHandlers:2:LSHandlerContentType string public.html"
"Add :LSHandlers:3:LSHandlerRoleViewer string $browserAgentString"
"Add :LSHandlers:3:LSHandlerContentType string public.url"
"Add :LSHandlers:4:LSHandlerRoleViewer string $browserAgentString"
"Add :LSHandlers:4:LSHandlerContentType string public.xhtml"
"Add :LSHandlers:5:LSHandlerRoleAll string $emailAgentString"
"Add :LSHandlers:5:LSHandlerURLScheme string mailto"
)
lsregisterPath="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

 

########## function-ing ##########

 

# Exits if any required Jamf Pro arguments are undefined.
function check_jamf_pro_arguments {
jamfProArguments=(
"$browserAgentString"
"$emailAgentString"
)
for argument in "${jamfProArguments[@]}"; do
if [[ -z "$argument" ]]; then
echo " ERROR: Undefined Jamf Pro argument, unable to proceed."
exit 74
fi
done
}

 

########## main process ##########

 

# Exit if any required Jamf Pro arguments are undefined.
check_jamf_pro_arguments


# Clear out LSHandlers array data from $launchServicesPlist, or create new plist if file does not exist.
if [[ -e "$launchServicesPlist" ]]; then
"$plistbuddyPath" -c "Delete :LSHandlers" "$launchServicesPlist"
echo "Reset LSHandlers array from $launchServicesPlist."
else
/bin/mkdir -p "$launchServicesPlistFolder"
"$plistbuddyPath" -c "Save" "$launchServicesPlist"
echo "Created $launchServicesPlist."
fi


# Add new LSHandlers array.
"$plistbuddyPath" -c "Add :LSHandlers array" "$launchServicesPlist"
echo "Initialized LSHandlers array."


# Set handler for each URL scheme and content type to specified browser and email client.
for plistbuddyCommand in "${plistbuddyPreferences[@]}"; do
"$plistbuddyPath" -c "$plistbuddyCommand" "$launchServicesPlist"
if [[ "$plistbuddyCommand" = *"$browserAgentString"* ]] || [[ "$plistbuddyCommand" = *"$emailAgentString"* ]]; then
arrayEntry=$(echo "$plistbuddyCommand" | /usr/bin/awk -F: '{print $2 ":" $3 ":" $4}' | /usr/bin/sed 's/ .*//')
prefLabel=$(echo "$plistbuddyCommand" | /usr/bin/awk '{print $4}')
echo "Set $arrayEntry to $prefLabel."
fi
done


# Fix permissions on $launchServicesPlistFolder.
/usr/sbin/chown -R "$loggedInUser" "$launchServicesPlistFolder"
echo "Fixed permissions on $launchServicesPlistFolder."


# Reset Launch Services database.
"$lsregisterPath" -kill -r -domain local -domain system -domain user
echo "Reset Launch Services database. A restart may also be required for these new default client changes to take effect."

 

exit 0

What variables am I changing here to target Edge @dmccluskey ?

MMTechno
New Contributor II

I have got massive problems with that.. I can run this ok from self service and all is working fine!

I have then tried to incorporate it into my build process and i call this as a policy with a trigger from my install script. I know for a fact that the user is logged in. I have actually added a "cat" command  into the policy so it echoes the contents of the .plist file and i can see that all the values populate. However after my workflow's reboot the plist is empty. Any ideas? I am on Sonoma and we are on Jamf 11.4.1 

 

MMTechno
New Contributor II

^^^ Please disregard this - i found the issue - basically i have been applying the script during my workflow and the settings seem to be overwritten by Company Portal's settings but just momentarily - Added the script to run after the company portal has exited and all is good! 

dmccluskey
Contributor II

you also need to reboot too

 

2022-06-16_11-18-48.jpg

Mine shows this.  Your parameter 4 and 5 have titles...

adlgavinator_0-1655415756260.png

 

dmccluskey
Contributor II

thats because need to customize those fields in the script options if you want fancy names.

Thanks for your help!  This worked like a charm.  I may try and work a reboot into my deployment somewhere as a next step.