Posted on 06-16-2022 04:16 AM
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
Solved! Go to Solution.
Posted on 06-16-2022 08:23 AM
#!/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
Posted on 06-16-2022 05:48 AM
Posted on 06-16-2022 06:23 AM
Hello Gavin,
This may be useful for setting Edge as the default browser: https://github.com/targendaz2/Mac-Set-Default-Apps
Posted on 06-16-2022 09:18 AM
Hi @Rocketman_Zac unfortunately this script does not work for python3. It is written in python2 I believe.
Posted on 06-16-2022 10:20 AM
@adl-gavinator Great catch. Thanks for the heads up!
Posted on 06-16-2022 08:23 AM
#!/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
Posted on 06-16-2022 09:17 AM
What variables am I changing here to target Edge @dmccluskey ?
Posted on 04-19-2024 11:42 AM
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
Posted on 04-21-2024 01:38 AM
^^^ 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!
Posted on 06-16-2022 09:20 AM
you also need to reboot too
Posted on 06-16-2022 02:42 PM
Mine shows this. Your parameter 4 and 5 have titles...
Posted on 06-16-2022 02:44 PM
thats because need to customize those fields in the script options if you want fancy names.
Posted on 06-17-2022 12:43 AM
Thanks for your help! This worked like a charm. I may try and work a reboot into my deployment somewhere as a next step.