From eb3c93f66f4c24639312dc2055bed802b4349192 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Tue, 15 Dec 2015 00:20:43 -0500 Subject: [PATCH 1/6] Code Cleanup Wrapped things up into functions. I am bored. Seriously. These edits, along with this commit message, are hereby released under the CC0 (Public Domain) License, since I am not interested in them at all. --- OfficeThinner.sh | 100 ++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/OfficeThinner.sh b/OfficeThinner.sh index fc11c40..9650647 100755 --- a/OfficeThinner.sh +++ b/OfficeThinner.sh @@ -12,28 +12,43 @@ # Author: goodbest # Repo: https://github.com/goodbest/OfficeThinner +# Configs. You can change paths here +basePath="/Applications/" +backupPath="$HOME/Desktop/OfficeThinnerBackup/" -# Configs. You can change path here -basePATH="/Applications/" -backupPATH="$HOME/Desktop/OfficeThinnerBackup/" +# Symbolic link the relevant files in the apps to the first one +apps=( 'Microsoft '{Word,Excel,Powerpoint,Outlook,Onenote} ) -WordPATH="Microsoft Word.app" -ExcelPATH="Microsoft Excel.app" -PowerPointPATH="Microsoft PowerPoint.app" -OutlookPATH="Microsoft Outlook.app" -OneNotePATH="Microsoft OneNote.app" - -# Symbolic link the files in following 4 apps to Word.app -appPathArray=( "$ExcelPATH" "$PowerPointPATH" "$OutlookPATH" "$OneNotePATH" ) +# link_this RELATIVE_PATH_FROM_.app_DIRs... +# if you want to debug this, well.. uncomment the `set`s. Human (like me) make mistakes. +link_this(){ + # set -xv + local officeApp + for officeApp in "${apps[@]:1}"; do + # printf >&2 'Eating %s...' "$officeApp" + link_that "${apps[0]}" "$officeApp" "$@" + done + # set +xv +} +# link_that sourceApp targetApp paths_to_eat... +link_that(){ + local thing thing_bk thing_dn + for thing; do + thing_dn=$(dirname "$pth") + thing_bk="$backupPath/$2.app/$thing_dn/" + mkdir -p "$thing_bk" + sudo mv "$2/$thing" "$thing_bk/" + # Using the (possibly) least ambiguous POSIX ln call.. (-> dir) + sudo ln -s "$1/$thing/" "$2/$thing_dn/" + done +} # Disk Usage Display diskUsage(){ - du -sh "$basePATH$WordPATH" - du -sh "$basePATH$ExcelPATH" - du -sh "$basePATH$PowerPointPATH" - du -sh "$basePATH$OutlookPATH" - du -sh "$basePATH$OneNotePATH" - echo "" + local apps_pth + apps_pth=("${apps[@]/%/.app}") + apps_pth=("${apps[@]/#/$1}") + du -sh "${apps_pth[@]}" } echo "Before running this script, Office is taking:" @@ -41,66 +56,37 @@ diskUsage # ============================== # Phase I: Deal with Fonts -# Comparison Result: Word = Excel = Powerpoint, Outlook is subset of Word, OneNote is subset of Word +# Comparison Result: Word = Excel = Powerpoint, Outlook and Onenote are subsets of Word # ============================== -fontPATH="/Contents/Resources/" -if [ -d "$basePATH$WordPATH$fontPATH/DFonts" ]; then - fontName="DFonts" +appResources="Contents/Resources/" +# XXX: what's the difference? should we symlink both? +if [ -d "$basePath/$WordPath/$appResources/DFonts" ]; then + fontDir="DFonts" else - fontName="Fonts" + fontDir="Fonts" fi echo "Thinning Fonts, it saves you ~1.4G space" -for appPATH in "${appPathArray[@]}"; -do - appName=${appPATH/.app/} - mkdir -p "$backupPATH$appName$fontPATH" - # echo "$backupPATH$appName$fontPATH" - sudo mv "$basePATH$appPATH$fontPATH$fontName" "$backupPATH$appName$fontPATH" - sudo ln -s "$basePATH$WordPATH$fontPATH$fontName" "$basePATH$appPATH$fontPATH$fontName" - # echo "$basePATH$appPATH$fontPATH$fontName" "$backupPATH$appName$fontPATH" - # echo "$basePATH$WordPATH$fontPATH$fontName" "$basePATH$appPATH$fontPATH$fontName" -done +link_this "Contents/Resources/$fontDir" # ============================== # Phase II: Deal with Proofing Tools # Comparison Result: Word = Excel = Powerpoint = Outlook, OneNote is subset of Word # ============================== -proofingPATH="/Contents/SharedSupport/" -proofingName="Proofing Tools" echo "Thinning Proofing Tools, it saves you ~1.5G space" -for appPATH in "${appPathArray[@]}"; -do - appName=${appPATH/.app/} - mkdir -p "$backupPATH$appName$proofingPATH" - # echo "$backupPATH$appName$proofingPATH" - sudo mv "$basePATH$appPATH$proofingPATH$proofingName" "$backupPATH$appName$proofingPATH" - sudo ln -s "$basePATH$WordPATH$proofingPATH$proofingName" "$basePATH$appPATH$proofingPATH$proofingName" - # echo "$basePATH$appPATH$proofingPATH$proofingName" "$backupPATH$appName$proofingPATH" - # echo "$basePATH$WordPATH$proofingPATH$proofingName" "$basePATH$appPATH$proofingPATH$proofingName" -done +link_this "Contents/SharedSupport/Proofing Tools" # ============================== # Phase III: Deal with MicrosoftOffice.framework # Comparison Result: Word = Excel = Powerpoint = Outlook = OneNote # ============================== -frameworkPATH="/Contents/Frameworks/MicrosoftOffice.framework/Versions/A/" -frameworkName="Resources" echo "Thinning MicrosoftOffice.framework, it saves you ~0.8G space" -for appPATH in "${appPathArray[@]}"; -do - appName=${appPATH/.app/} - mkdir -p "$backupPATH$appName$frameworkPATH" - # echo "$backupPATH$appName$frameworkPATH" - sudo mv "$basePATH$appPATH$frameworkPATH$frameworkName" "$backupPATH$appName$frameworkPATH" - sudo ln -s "$basePATH$WordPATH$frameworkPATH$frameworkName" "$basePATH$appPATH$frameworkPATH$frameworkName" - # echo "$basePATH$appPATH$frameworkPATH$frameworkName" "$backupPATH$appName$frameworkPATH" - # echo "$basePATH$WordPATH$frameworkPATH$frameworkName" "$basePATH$appPATH$frameworkPATH$frameworkName" -done +# Shouldn't we just deal with the '.framework' instead? +link_this "Contents/Frameworks/MicrosoftOffice.framework/Versions/A/Resources" echo "Office Thinning Complete." echo "" echo "After running this script, Office is taking:" diskUsage -echo "The duplicate files are backed up at $backupPATH" +echo "The duplicate files are backed up at $backupPath" echo "If everything is OK, you may delete these files. But the choice is yours." echo "You may have to re-run this script after you install Microsoft Office updates" From 13481e616116ff10e8839bd88ec4772095894adf Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Tue, 15 Dec 2015 00:39:57 -0500 Subject: [PATCH 2/6] Forgot .app What did I just say? Human makes mistakes. --- OfficeThinner.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OfficeThinner.sh b/OfficeThinner.sh index 9650647..c6d95c6 100755 --- a/OfficeThinner.sh +++ b/OfficeThinner.sh @@ -37,9 +37,9 @@ link_that(){ thing_dn=$(dirname "$pth") thing_bk="$backupPath/$2.app/$thing_dn/" mkdir -p "$thing_bk" - sudo mv "$2/$thing" "$thing_bk/" + sudo mv "$2.app/$thing" "$thing_bk/" # Using the (possibly) least ambiguous POSIX ln call.. (-> dir) - sudo ln -s "$1/$thing/" "$2/$thing_dn/" + sudo ln -s "$1.app/$thing/" "$2.app/$thing_dn/" done } From d90cd7cc8cb7722323af68a3a6663a25b761455b Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Tue, 15 Dec 2015 00:47:03 -0500 Subject: [PATCH 3/6] Fix DFont detection And more. That's the problem with GH webpage editing -- you can't do `git add -p`. --- OfficeThinner.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/OfficeThinner.sh b/OfficeThinner.sh index c6d95c6..d5285da 100755 --- a/OfficeThinner.sh +++ b/OfficeThinner.sh @@ -24,16 +24,22 @@ apps=( 'Microsoft '{Word,Excel,Powerpoint,Outlook,Onenote} ) link_this(){ # set -xv local officeApp + # printf >&2 'Processing' + # printf >&2 ' [%s]' "$@" + # printf >&2 '...\n' for officeApp in "${apps[@]:1}"; do - # printf >&2 'Eating %s...' "$officeApp" + # printf >&2 '--> In %s...\n' "$officeApp" link_that "${apps[0]}" "$officeApp" "$@" done # set +xv } # link_that sourceApp targetApp paths_to_eat... +# Adding `-v` to `mv` and `ln` may potentially make users happier.. link_that(){ local thing thing_bk thing_dn for thing; do + # skip inexist ones.. should reduce mess. + [[ -e $1.app/$thing/ ]] || continue thing_dn=$(dirname "$pth") thing_bk="$backupPath/$2.app/$thing_dn/" mkdir -p "$thing_bk" @@ -58,9 +64,7 @@ diskUsage # Phase I: Deal with Fonts # Comparison Result: Word = Excel = Powerpoint, Outlook and Onenote are subsets of Word # ============================== -appResources="Contents/Resources/" -# XXX: what's the difference? should we symlink both? -if [ -d "$basePath/$WordPath/$appResources/DFonts" ]; then +if [ -d "$basePath/${apps[0]}.app/Contents/Resources/DFonts" ]; then fontDir="DFonts" else fontDir="Fonts" From fa211e12729dee0a3b91151492bf7367bb63b8a8 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Tue, 15 Dec 2015 00:51:55 -0500 Subject: [PATCH 4/6] =?UTF-8?q?Shellcheck=20tested=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Human.. makes.. --- OfficeThinner.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OfficeThinner.sh b/OfficeThinner.sh index d5285da..7e97821 100755 --- a/OfficeThinner.sh +++ b/OfficeThinner.sh @@ -40,7 +40,7 @@ link_that(){ for thing; do # skip inexist ones.. should reduce mess. [[ -e $1.app/$thing/ ]] || continue - thing_dn=$(dirname "$pth") + thing_dn=$(dirname "$thing") thing_bk="$backupPath/$2.app/$thing_dn/" mkdir -p "$thing_bk" sudo mv "$2.app/$thing" "$thing_bk/" @@ -53,7 +53,7 @@ link_that(){ diskUsage(){ local apps_pth apps_pth=("${apps[@]/%/.app}") - apps_pth=("${apps[@]/#/$1}") + apps_pth=("${apps[@]/#/${1:-$basePath}") du -sh "${apps_pth[@]}" } From 92b0d5315d1d3b66a838858ea3ce50fb07fff64c Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Thu, 17 Dec 2015 14:29:56 -0500 Subject: [PATCH 5/6] Fix link_that appname This should finally work. --- OfficeThinner.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/OfficeThinner.sh b/OfficeThinner.sh index 7e97821..14118cb 100755 --- a/OfficeThinner.sh +++ b/OfficeThinner.sh @@ -33,19 +33,20 @@ link_this(){ done # set +xv } -# link_that sourceApp targetApp paths_to_eat... +# link_that targetApp sourceApp paths_to_eat... # Adding `-v` to `mv` and `ln` may potentially make users happier.. link_that(){ - local thing thing_bk thing_dn + local thing thing_bk thing_dn ref="$1" src="$2" + shift 2 for thing; do # skip inexist ones.. should reduce mess. - [[ -e $1.app/$thing/ ]] || continue + [[ -e $dst.app/$thing && -e $ref.app/$thing ]] || continue thing_dn=$(dirname "$thing") - thing_bk="$backupPath/$2.app/$thing_dn/" + thing_bk="$backupPath/$src.app/$thing_dn/" mkdir -p "$thing_bk" - sudo mv "$2.app/$thing" "$thing_bk/" + sudo mv "$src.app/$thing" "$thing_bk/" # Using the (possibly) least ambiguous POSIX ln call.. (-> dir) - sudo ln -s "$1.app/$thing/" "$2.app/$thing_dn/" + sudo ln -s "$ref.app/$thing/" "$src.app/$thing_dn/" done } From c9730504a384ab70dcbca41b4d6b467459347fc7 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Thu, 17 Dec 2015 14:33:17 -0500 Subject: [PATCH 6/6] Update OfficeThinner.sh --- OfficeThinner.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OfficeThinner.sh b/OfficeThinner.sh index 14118cb..3c9e43f 100755 --- a/OfficeThinner.sh +++ b/OfficeThinner.sh @@ -40,13 +40,13 @@ link_that(){ shift 2 for thing; do # skip inexist ones.. should reduce mess. - [[ -e $dst.app/$thing && -e $ref.app/$thing ]] || continue + [[ -e $basePath/$dst.app/$thing && -e $basePath/$ref.app/$thing ]] || continue thing_dn=$(dirname "$thing") - thing_bk="$backupPath/$src.app/$thing_dn/" + thing_bk="$basePath/$backupPath/$src.app/$thing_dn/" mkdir -p "$thing_bk" - sudo mv "$src.app/$thing" "$thing_bk/" + sudo mv "$basePath/$src.app/$thing" "$thing_bk/" # Using the (possibly) least ambiguous POSIX ln call.. (-> dir) - sudo ln -s "$ref.app/$thing/" "$src.app/$thing_dn/" + sudo ln -s "$basePath/$ref.app/$thing/" "$basePath/$src.app/$thing_dn/" done }