Skip to content

Instantly share code, notes, and snippets.

@linniksa
Forked from inoperable/defaults-export.zsh
Last active March 10, 2024 19:27
Show Gist options
  • Save linniksa/ad05020e6372c0a78534db07099d8d03 to your computer and use it in GitHub Desktop.
Save linniksa/ad05020e6372c0a78534db07099d8d03 to your computer and use it in GitHub Desktop.

Revisions

  1. linniksa revised this gist Mar 10, 2024. 1 changed file with 14 additions and 11 deletions.
    25 changes: 14 additions & 11 deletions defaults-export.zsh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/env zsh

    set -e

    function exportDefaults {
    local outdir="$HOME/defaults"
    local outdirApple="$outdir/apple"
    @@ -11,8 +13,8 @@ function exportDefaults {
    local globals=0


    function cleanOutdirs {
    [[ ! -d $HOME/defaults ]] && mkdir "$HOME/defaults"
    function cleanOutDirs {
    [[ ! -d "$outdir" ]] && mkdir "$outdir"
    if [[ -d $outdirApple ]]; then
    echo "removing all files in $outdirApple"
    rm -rf "${outdirApple/*}"
    @@ -41,17 +43,17 @@ function exportDefaults {
    for domain in "${domains[@]}"; do
    plist="${domain//,/}.plist"
    if [[ $globals == 0 ]]; then
    if [[ $domain =~ com.apple ]]; then
    defaults export "$domain" "$outdirApple/$plist"
    if [[ "$domain" =~ com.apple ]] || [[ $domain =~ 'Apple Global Domain' ]]; then
    defaults export "$domain" - > "$outdirApple/$plist"
    #echo "writing $plist to $outdirApple"
    filecount=$((filecount+1))
    else
    defaults export "$domain" "$outdirUser/$plist"
    defaults export "$domain" - > "$outdirUser/$plist"
    #echo "writing $plist to $outdirUser"
    filecount=$((filecount+1))
    fi
    else
    sudo defaults export "$domain" "$outdirGlobal/$plist"
    sudo defaults export "$domain" - > "$outdirGlobal/$plist"
    #echo "writing $plist to $outdirGlobal"
    filecount=$((filecount+1))
    fi
    @@ -61,12 +63,13 @@ function exportDefaults {
    done
    }

    cleanOutdirs
    cleanOutDirs

    # -------------------------------------------------
    local domains=($(defaults domains))
    domains+=('Apple Global Domain') # add "hidden" domain
    local filesleft=${#domains[@]}
    echo "USER namespace has ${#domains[@]} domains, exportig..."
    echo "USER namespace has ${#domains[@]} domains, exporting..."
    exportDomains
    echo "written $filecount files in $outdir"
    local filestotal=$((filestotal+filecount))
    @@ -75,16 +78,16 @@ function exportDefaults {
    # -------------------------------------------------
    local domains=($(sudo defaults domains))
    local filesleft=${#domains[@]}
    echo "GLOBAL namespace has ${#domains[@]} domains, exportig..."
    echo "GLOBAL namespace has ${#domains[@]} domains, exporting..."
    exportDomains
    echo "written $filecount files in $outdir"
    local filestotal=$((filestotal+filecount))
    # -------------------------------------------------

    sudo chown -R "$(whoami)":staff "$HOME/defaults"
    sudo chown -R "$(whoami)":staff "$outdir"
    local timed="$((SECONDS / 3600))hrs $(((SECONDS / 60) % 60))min $((SECONDS % 60))sec"

    echo "exported $filestotal files in $timed"
    }

    exportDefaults
    exportDefaults
  2. @inoperable inoperable renamed this gist Apr 25, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @inoperable inoperable revised this gist Apr 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-export.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/bash
    #!/usr/bin/env zsh

    function exportDefaults {
    local outdir="$HOME/defaults"
  4. @inoperable inoperable renamed this gist Mar 16, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @inoperable inoperable created this gist Mar 16, 2017.
    90 changes: 90 additions & 0 deletions -
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    #!/bin/bash

    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