Created
November 22, 2023 06:21
-
-
Save miku1958/d7f9594193626880fa9a0dc1c7eac785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A shell function that mimics the defaults command using /usr/libexec/PlistBuddy | |
function defaults2() { | |
# Check the number of arguments | |
# If the number of arguments is less then 2, fallback to defaults command | |
if [ $# -lt 2 ]; then | |
/usr/bin/defaults "$@" | |
return $? | |
fi | |
# Get the command, domain, key and value arguments | |
local cmd=$1 | |
local domain=$2 | |
local key=$3 | |
local value=$4 | |
# Check if key and value are provided for write command | |
if [ "$cmd" = "write" ] && ([ -z "$key" ] || [ -z "$value" ]); then | |
echo "Key or value is missing" | |
return 1 | |
fi | |
# Get the plist file path from the domain name | |
local plist="$domain" | |
# Check if the file exists, otherwise try other locations | |
if [ ! -f "$plist" ]; then | |
# Define an array of possible plist locations | |
local plist_locations=("/Users/$USER/Library/Preferences/" "/Users/$USER/Library/Application Support/" "/Library/Preferences/" "/System/Library/Preferences/") | |
# Loop through the plist locations and check if the file exists | |
for location in "${plist_locations[@]}"; do | |
if [ -f "$location$domain" ]; then | |
plist="$location$domain" | |
break | |
fi | |
# Try to append .plist if not found | |
if [ -f "$location$domain.plist" ]; then | |
plist="$location$domain.plist" | |
break | |
fi | |
done | |
fi | |
# Check if the file exists, otherwise exit the function | |
if [ ! -f "$plist" ]; then | |
echo "File not found: $plist" | |
return 2 | |
fi | |
# Define the PlistBuddy command and the key path with colon as separator | |
local pb="/usr/libexec/PlistBuddy" | |
# Execute the command using PlistBuddy and check for errors | |
case $cmd in | |
read) | |
# Read the value for the given key | |
$pb -c "Print $key" "$plist" 2>/dev/null | |
# If PlistBuddy fails, fallback to defaults command | |
if [ $? -ne 0 ]; then | |
/usr/bin/defaults read "$domain" "$key" | |
fi | |
;; | |
write) | |
# Write the value for the given key, creating it if necessary | |
$pb -c "Add $key $value" "$plist" 2>/dev/null || | |
$pb -c "Set $key $value" "$plist" 2>/dev/null | |
# If PlistBuddy fails, fallback to defaults command | |
if [ $? -ne 0 ]; then | |
/usr/bin/defaults write "$domain" "$key" "$value" | |
fi | |
;; | |
delete) | |
# Delete the key and its value | |
$pb -c "Delete $key" "$plist" 2>/dev/null | |
# If PlistBuddy fails, fallback to defaults command | |
if [ $? -ne 0 ]; then | |
/usr/bin/defaults delete "$domain" "$key" | |
fi | |
;; | |
*) | |
# Invalid command | |
echo "Invalid command: $cmd" | |
return 3 | |
;; | |
esac | |
# Return the exit status of the last command or fallback command | |
return $? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment