Last active
June 19, 2025 23:53
-
-
Save inoperable/6041a6cc9426043bb830f4f4ed658449 to your computer and use it in GitHub Desktop.
export defaults for all domains in macOS into separate .plist files for editing and importing, files are written into $HOME/defaults
This file contains hidden or 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
#!/usr/bin/env zsh | |
function exportDefaults { | |
local outdir="$HOME/defaults" | |
local outdirApple="$outdir/apple" | |
local outdirUser="$outdir/user" | |
local outdirGlobal="$outdir/global" | |
local filesdone=0 | |
local filecount=0 | |
local filestotal=0 | |
local globals=0 | |
function cleanOutdirs { | |
[[ ! -d $HOME/defaults ]] && mkdir "$HOME/defaults" | |
if [[ -d $outdirApple ]]; then | |
echo "removing all files in $outdirApple" | |
rm -rf "${outdirApple/*}" | |
else | |
mkdir "$outdirApple" | |
fi | |
if [[ -d $outdirUser ]]; then | |
echo "removing all files in $outdirUser" | |
rm -rf "${outdirUser/*}" | |
else | |
mkdir "$outdirUser" | |
fi | |
if [[ -d $outdirGlobal ]]; then | |
echo "removing all files in $outdirGlobal" | |
rm -rf "${outdirGlobal/*}" | |
else | |
mkdir "$outdirGlobal" | |
fi | |
} | |
function exportDomains { | |
filesdone=0 | |
filecount=0 | |
for domain in "${domains[@]}"; do | |
plist="${domain//,/}.plist" | |
if [[ $globals == 0 ]]; then | |
if [[ $domain =~ com.apple ]]; then | |
defaults export "$domain" "$outdirApple/$plist" | |
#echo "writing $plist to $outdirApple" | |
filecount=$((filecount+1)) | |
else | |
defaults export "$domain" "$outdirUser/$plist" | |
#echo "writing $plist to $outdirUser" | |
filecount=$((filecount+1)) | |
fi | |
else | |
sudo defaults export "$domain" "$outdirGlobal/$plist" | |
#echo "writing $plist to $outdirGlobal" | |
filecount=$((filecount+1)) | |
fi | |
filesleft=$((filesleft-1)) | |
filesdone=$((filesdone+1)) | |
echo -ne "[ $filesdone/$filesleft ] \r" | |
done | |
} | |
cleanOutdirs | |
# ------------------------------------------------- | |
local domains=($(defaults domains)) | |
local filesleft=${#domains[@]} | |
echo "USER namespace has ${#domains[@]} domains, exportig..." | |
exportDomains | |
echo "written $filecount files in $outdir" | |
local filestotal=$((filestotal+filecount)) | |
# ------------------------------------------------- | |
globals=1 | |
# ------------------------------------------------- | |
local domains=($(sudo defaults domains)) | |
local filesleft=${#domains[@]} | |
echo "GLOBAL namespace has ${#domains[@]} domains, exportig..." | |
exportDomains | |
echo "written $filecount files in $outdir" | |
local filestotal=$((filestotal+filecount)) | |
# ------------------------------------------------- | |
sudo chown -R "$(whoami)":staff "$HOME/defaults" | |
local timed="$((SECONDS / 3600))hrs $(((SECONDS / 60) % 60))min $((SECONDS % 60))sec" | |
echo "exported $filestotal files in $timed" | |
} | |
exportDefaults |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@inoperable My pleasure! Thank you for providing this script in the first place.
One thing I didnt seen before /usr/env/bash could be trouble since Apple dropped bash in favour of zsh (unless that changed back, I dropped macOS since Apple Silicon showed up - main reason being economics)