Using a Plist

Changing default browser

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "<http://www.apple.com/DTDs/PropertyList-1.0.dtd>">
<plist version="1.0">
<dict>
<key>DefaultBrowserSettingEnabled</key>
<true/>
</dict>
</plist>
Untitled
Untitled

Using a Script

As it happens, I just found a way to script this, but it takes a bit of doing. Up to you if you wanna do it this way. It sets Chrome as the default browser, mail pp, and calendar app (we’re a Gsuite shop):

Only downside is having to install an instance of python3

#!/bin/bash

#get current console user name

currentuser=`stat -f "%Su" /dev/console`

curl <https://www.python.org/ftp/python/3.11.5/python-3.11.5-macos11.pkg> --output /Users/Shared/python3.pkg
installer -pkg /Users/Shared/python3.pkg -target /usr/local/bin
rm /Users/Shared/python3.pkg
/usr/local/bin/python3 -m pip install pyobjc-framework-LaunchServices

su "$currentuser" -c /usr/local/bin/python3 <<EOF                                
import LaunchServices;
result = LaunchServices.LSSetDefaultHandlerForURLScheme(
    "mailto",
    "com.google.Chrome")
result = LaunchServices.LSSetDefaultHandlerForURLScheme(
    "webcal",
    "com.google.Chrome")
result = LaunchServices.LSSetDefaultHandlerForURLScheme(
    "http",
    "com.google.Chrome")
result = LaunchServices.LSSetDefaultHandlerForURLScheme(
    "https",
    "com.google.Chrome")
result = LaunchServices.LSSetDefaultHandlerForURLScheme(
    "public.html",
    "com.google.Chrome")
print("Result: %d (%s)" % (
    result,
    "Success" if result == 0 else "Error"))
EOF

Another Script

https://github.com/aysiu/Mac-Scripts-and-Profiles/blob/main/ChromeDefaultBrowser.sh
#!/bin/zsh

## Note (6 Dec., 2019): The old version that used lsregister no longer worked as of
## Catalina (10.15). The version that doesn't use lsregister does appear to work in
## Ventura (13.4.1), as long as you reboot. Otherwise, trying to run lsregister afterwards
## may prevent System Settings from launching up

# Desired default browser string
DefaultBrowser='com.google.chrome'
#DefaultBrowser='com.microsoft.edgemac'
#DefaultBrowser='org.mozilla.firefox'
#DefaultBrowser='com.apple.safari'

# PlistBuddy executable
PlistBuddy='/usr/libexec/PlistBuddy'

# Plist directory
PlistDirectory="$HOME/Library/Preferences/com.apple.LaunchServices"

# Plist name
PlistName="com.apple.launchservices.secure.plist"

# Plist location
PlistLocation="$PlistDirectory/$PlistName"

# Array of preferences to add
PrefsToAdd=("{ LSHandlerContentType = \\"public.url\\"; LSHandlerPreferredVersions = { LSHandlerRoleViewer = \\"-\\"; }; LSHandlerRoleViewer = \\"$DefaultBrowser\\"; }"
"{ LSHandlerContentType = \\"public.html\\"; LSHandlerPreferredVersions =  { LSHandlerRoleAll = \\"-\\"; }; LSHandlerRoleAll = \\"$DefaultBrowser\\"; }"
"{ LSHandlerPreferredVersions = { LSHandlerRoleAll = \\"-\\"; }; LSHandlerRoleAll = \\"$DefaultBrowser\\"; LSHandlerURLScheme = https; }"
"{ LSHandlerPreferredVersions = { LSHandlerRoleAll = \\"-\\"; }; LSHandlerRoleAll = \\"$DefaultBrowser\\"; LSHandlerURLScheme = http; }"
)

# Double-check the PlistLocation exists
if [[ -f "$PlistLocation" ]]; then

    # Initialize counter that will just keep moving us through the array of dicts
    # A bit imprecise... would be better if we could just count the array of dicts, but we'll stop when we get to a blank one
    Counter=0

    # Initialize DictResult just so the while loop begins
    DictResult='PLACEHOLDER'

    while [[ ! -z "$DictResult" ]]; do
        DictResult=$("$PlistBuddy" -c "Print LSHandlers:$Counter" "$PlistLocation")

        # Check for existing settings
        if [[ "$DictResult" == *"public.url"* || "$DictResult" == *"public.html"* || "$DictResult" == *"LSHandlerURLScheme = https"* || "$DictResult" == *"LSHandlerURLScheme = http"* ]]; then
            # Delete the existing. We'll add new ones in later
             "$PlistBuddy" -c "Delete :LSHandlers:$Counter" "$PlistLocation"
             /bin/echo "Deleting $Counter from Plist"
        fi

        # Increase counter
      Counter=$((Counter+1))

    # End of while loop
    done

# Plist does not exist
else
    # Say the Plist does not exist
    /bin/echo "Plist does not exist. Creating directory for it."
    /bin/mkdir -p "$PlistDirectory"

# End checking whether Plist exists or not
fi

echo "Adding in prefs"
for PrefToAdd in "${PrefsToAdd[@]}"
    do
        /usr/bin/defaults write "$PlistLocation" LSHandlers -array-add "$PrefToAdd"
    done

/bin/echo "You may need to reboot for changes to take effect."
https://macadmins.slack.com/archives/C04QVP86E/p1704856298074119?thread_ts=1704844450.927719&cid=C04QVP86E

Another Script

plistbuddyPreferences=(
  "Add :LSHandlers:0:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:0:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:0:LSHandlerURLScheme string http"
  "Add :LSHandlers:1:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:1:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:1:LSHandlerURLScheme string https"
  "Add :LSHandlers:2:LSHandlerContentType string public.html"
  "Add :LSHandlers:2:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:2:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:3:LSHandlerContentType string public.xhtml"
  "Add :LSHandlers:3:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:3:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:4:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:4:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:4:LSHandlerURLScheme string webcal"
  "Add :LSHandlers:5:LSHandlerContentType string public.url"
  "Add :LSHandlers:5:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:5:LSHandlerRoleAll string com.google.chrome"
)
https://macadmins.slack.com/archives/C0B9HEEGP/p1662071575805819?thread_ts=1661328418.193819&cid=C0B9HEEGP
#!/bin/zsh

# Set Chrome as default browser
# original script from Palantir Tech
# <https://github.com/palantir/jamf-pro-scripts/blob/main/scripts/Set%20Default%20Browser%20and%20Email%20Client.sh>

# richard@richard-purves.com

# Variables here
loggedInUser=$( /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk -F': ' '/[[:space:]]+Name[[:space:]]:/ { if ( $2 != "loginwindow" ) { print $2 }}' )
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:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:0:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:0:LSHandlerURLScheme string http"
  "Add :LSHandlers:1:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:1:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:1:LSHandlerURLScheme string https"
  "Add :LSHandlers:2:LSHandlerContentType string public.html"
  "Add :LSHandlers:2:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:2:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:3:LSHandlerContentType string public.xhtml"
  "Add :LSHandlers:3:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:3:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:4:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:4:LSHandlerRoleAll string com.google.chrome"
  "Add :LSHandlers:4:LSHandlerURLScheme string webcal"
  "Add :LSHandlers:5:LSHandlerContentType string public.url"
  "Add :LSHandlers:5:LSHandlerPreferredVersions:LSHandlerRoleAll string -"
  "Add :LSHandlers:5:LSHandlerRoleAll string com.google.chrome"
)

lsregisterPath="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

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

# Kill any running Launch Services processes
/usr/bin/killall lsd
echo "Stopping any running Launch Services processes."

exit 0
Collapse

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *