-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathChromeDefaultBrowser.sh
74 lines (56 loc) · 2.79 KB
/
ChromeDefaultBrowser.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/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."