Skip to content

Instantly share code, notes, and snippets.

@apfelchips
Last active February 10, 2024 16:25
Show Gist options
  • Save apfelchips/3b4aee47abe1fabc545dfdfdc1ae57f6 to your computer and use it in GitHub Desktop.
Save apfelchips/3b4aee47abe1fabc545dfdfdc1ae57f6 to your computer and use it in GitHub Desktop.
initialize macOS.
#!/bin/bash
# https://www.defaults-write.com
# https://github.com/joeyhoer/starter/tree/master/system
# https://github.com/atweiden/macfiles/blob/master/macos.sh
# https://gist.github.com/ryanpcmcquen/b2e608311f286a4ab3e1
# https://github.com/herrbischoff/awesome-macos-command-line
#
# discover filesystem changes: sudo filemon -c
#
# man defaults: https://ss64.com/osx/defaults.html
#
# $defaults write -g = $defaults write NSGlobalDomain
# native shortcuts eg. Mission control are saved in ~/Library/Preferences/com.apple.symbolichotkeys.plist
# -e -> exit when any command fails
# -u -> return error code when it tries to expand a variable that isn't set
# -o pipefail -> sets the exit code of a pipeline (aka. command1 | command2) to that of the rightmost command with a non-zero status
set -euo pipefail
# display commands and their arguments as they are executed.
#set -x
# verbose mode for inputs
#set -v
# DEBUG trap triggered before each command: saves the command to $last_command variable
trap 'last_command=${current_command:-"Start of script"}; current_command=$BASH_COMMAND' DEBUG
# EXIT trap triggered before exiting: prints the $failed_command and $exit_code if $exit_code is not 0
trap 'exit_code="$?"; failed_command="${last_command}"; if [ $exit_code != 0 ]; then echo "\"${failed_command}\" command failed with exit code: $exit_code." 1>&2; fi' EXIT
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
## try blocks for scripts with set -e
function try() {
test -z "$@" || echo "trying: $@" 1>&2
if [[ $- = *e* ]]; then
set OPT_E_ENABLED="1"
fi
set +e # don't exit on error after this function was called
}
function endtry() {
test -z "$@" || echo "end trying: $@" 1>&2
if [ "${OPT_E_ENABLED:-0}" -eq 1 ]; then
set -e
fi
return 0
}
function catch() {
local previous_exit_code="$?"
echo "notice: $@ | exit code: $previous_exit_code" 1>&2
return 0
}
# logging functions
function info() {
local previous_exit_code="$?"
echo "notice: $@ | exit code: $previous_exit_code" 1>&2
return $previous_exit_code
}
function fatal() {
local previous_exit_code="$?"
echo "fatal: $@ | exit code: $previous_exit_code" 1>&2
exit 1
}
# cache sudo credentials
sudo -v
# Keep-alive: update existing `sudo` time stamp until script has finished
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Always boot in verbose mode
# To undo this, type: sudo nvram -d boot-args or clear nvram opt+cmd+p+r
sudo nvram boot-args="-v"
## change keyboard layout
# https://www.macissues.com/2015/04/16/fix-an-incorrect-default-keyboard-layout-at-the-os-x-login-prompt/
# Disable the sound effects on boot
sudo nvram SystemAudioVolume=" "
try "install, update & upgrade homebrew"
# Get the command line tools!
sudo xcode-select -p &>/dev/null || sudo xcode-select --install && sudo xcode-select --reset
command -v brew >/dev/null || /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # Install homebrew
brew update && brew bundle --file="${HOMEBREW_BUNDLE_FILE:-$XDG_CONFIG_HOME/homebrew/Brewfile.rb}" && brew upgrade && brew cask upgrade
endtry
## Text input tweaks.
# Disable smart dashes as they’re annoying when typing code.
defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool false
# Disable automatic periods with a double space:
defaults write NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool false
# Disable smart quotes as they’re annoying when typing code.
defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false
# Disable emoji popup
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
# Disable auto-correct
defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false
# change delay until key repeat - Default: 25
#defaults write NSGlobalDomain InitialKeyRepeat -int 19
# change keyboard repeat rate - Default: 6
#defaults write NSGlobalDomain KeyRepeat -int 5
## Trackpad customisation
# Disable "natural" scrolling
defaults write NSGlobalDomain com.apple.swipescrolldirection -bool false
# disable ForceTouch
defaults write com.apple.AppleMultitouchTrackpad ForceSuppressed -bool true
# Two finger swiping
defaults write NSGlobalDomain AppleEnableSwipeNavigateWithScrolls -bool false
defaults write NSGlobalDomain com.apple.trackpad.twoFingerHorizSwipeGesture -int 0
# Three finger swiping --> needed when using clickswipe with BTT
# defaults write NSGlobalDomain com.apple.trackpad.threeFingerHorizSwipeGesture -int 0
# defaults write com.apple.AppleMultitouchTrackpad TrackpadThreeFingerHorizSwipeGesture -int 0
# disable Look up word under cursor --> more customizable with BTT
defaults write com.apple.AppleMultitouchTrackpad TrackpadThreeFingerTapGesture -int 0
defaults write com.apple.AppleMultitouchTrackpad TrackpadFourFingerHorizSwipeGesture -int 2 # space swiping
defaults write com.apple.AppleMultitouchTrackpad TrackpadFourFingerVertSwipeGesture -int 0 # Expose fixed by BTT
defaults write com.apple.AppleMultitouchTrackpad TrackpadFourFingerPinchGesture -int 2 # show Desktop pinch gesture
# Modify Keyboard shortcuts
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 32 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 33 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 34 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 35 "<dict><key>enabled</key><false/></dict>"
# Disable ctrl+left: Spaces Left
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 79 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 80 "<dict><key>enabled</key><false/></dict>"
# Spaces Right
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 81 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 82 "<dict><key>enabled</key><false/></dict>"
# # ctrl+
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 118 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 119 "<dict><key>enabled</key><false/></dict>"
defaults write com.apple.symbolichotkeys.plist AppleSymbolicHotKeys -dict-add 120 "<dict><key>enabled</key><false/></dict>"
# disable Time Machine Nagging
defaults write com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool true
### Finder
defaults write NSGlobalDomain AppleShowAllExtensions -bool true
# Set sidebar icon size to small
defaults write NSGlobalDomain NSTableViewDefaultSizeMode -int 1
# Save to disk (not to iCloud) by default
defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool false
# When performing a search, search the current folder by default
defaults write com.apple.finder FXDefaultSearchScope -string "SCcf"
#Use list view in all Finder windows by default [icnv, clmv, Flwv]
defaults write com.apple.finder FXPreferredViewStyle -string "Nlsv"
# Show icons for hard drives, servers, and removable media on the desktop
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool true
defaults write com.apple.finder ShowHardDrivesOnDesktop -bool true
defaults write com.apple.finder ShowMountedServersOnDesktop -bool true
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool true
# Disable the warning when changing a file extension
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false
# Character Special Key Abbreviation
#--------------------------------------------------------
# @ Command (Apple) CMD
# ~ Option OPT
# $ Shift SHIFT
# ^ Control CTRL
defaults write com.apple.safari NSUserKeyEquivalents ' {
"Undo Close Tab" = "@$t";
}'
### Safari
# Prevent Safari from opening ‘safe’ files automatically after downloading
defaults write com.apple.Safari AutoOpenSafeDownloads -bool false
# don’t send search queries to Apple
defaults write com.apple.Safari UniversalSearchEnabled -bool false
defaults write com.apple.Safari SuppressSearchSuggestions -bool true
# Show the full URL in the address bar (note: this still hides the scheme)
defaults write com.apple.Safari ShowFullURLInSmartSearchField -bool true
# PlainText FTW!
defaults write com.apple.TextEdit RichText -int 0
defaults write com.apple.assistant.support "Assistant Enabled" -bool false
### UI Tweaks
# Disable animations when opening and closing windows.
sudo defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool false
# Disable Fullscreen Annimation
sudo defaults write NSGlobalDomain NSWindowResizeTime -float 0.001
# Dark Mode: Menu bar / Dock only --> still have to select Dark Mode in System Preferences to take effect
sudo defaults write NSGlobalDomain NSRequiresAquaSystemAppearance -bool true
# always use extended print dialogue
sudo defaults write NSGlobalDomain PMPrintingExpandedStateForPrint -bool true
# increase time it takes Mission Control to open when daragging a window to the top --> improves Magnet.app
defaults write com.apple.dock workspaces-edge-delay -float 4.0
# Disable automatically rearrange Spaces based on recent use
defaults write com.apple.dock mru-spaces -bool false
# Developer / Debug Menus
defaults write com.apple.appstore ShowDebugMenu -bool true
defaults write com.apple.Safari IncludeInternalDebugMenu -bool true
## Crash Reporter as a Notification
defaults write com.apple.CrashReporter UseUNC -int 1
# Zoom using scroll gesture with the Ctrl (^) modifier key
defaults write com.apple.universalaccess closeViewScrollWheelToggle -bool true
defaults write com.apple.universalaccess HIDScrollZoomModifierMask -int 262144
# Disable CD & DVD Actions:
defaults write com.apple.digihub com.apple.digihub.blank.cd.appeared -dict action 1
defaults write com.apple.digihub com.apple.digihub.blank.dvd.appeared -dict action 1
defaults write com.apple.digihub com.apple.digihub.cd.picture.appeared -dict action 1
defaults write com.apple.digihub com.apple.digihub.dvd.video.appeared -dict action 1
# enable better Bluetooth Audio codecs
sudo defaults write bluetoothaudiod "Enable AptX codec" -bool true
sudo defaults write bluetoothaudiod "Enable AAC codec" -bool true
### Garbage file reduction
# not respected in recent macOS versions
# disable Spotlight on external/network Drives
# http://enterprisemac.bruienne.com/2015/09/15/disable-spotlight-indexing-of-network-volumes/
sudo defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
sudo defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true
sudo defaults write /Library/Preferences/com.apple.SpotlightServer.plist ExternalVolumesDefaultOff -bool True
# Avoid creating .DS_Store files on network or USB volumes
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true
# reset all systempreferences Notifications to remove the "Start using iCloud" Nag
defaults delete com.apple.systempreferences AttentionPrefBundleIDs &>/dev/null || echo ""
### UX Tweaks
# Disable “natural” (Lion-style) scrolling
defaults write NSGlobalDomain com.apple.swipescrolldirection -bool false
# Use scroll gesture with the Ctrl (^) modifier key to zoom
defaults write com.apple.universalaccess closeViewScrollWheelToggle -bool true
defaults write com.apple.universalaccess HIDScrollZoomModifierMask -int 262144
# Disable Siri
defaults write com.apple.Siri UserHasDeclinedEnable -bool true
defaults write com.apple.Siri StatusMenuVisible -bool false
command -v duti >/dev/null && test -d "$XDG_CONFIG_HOME/duti/" && duti "$XDG_CONFIG_HOME/duti/"
# DeQuarantine all Applications installed by Homebrew Cask
xattr -dr com.apple.quarantine "$HOME/Applications" 2>/dev/null
xattr -dr com.apple.quarantine "$HOME/Library/QuickLook" 2>/dev/null
xattr -dr com.apple.quarantine "/usr/local/Caskroom" 2>/dev/null
command -v brew >/dev/null && sudo xattr -dr com.apple.quarantine "$(brew --prefix)/Caskroom" 2>/dev/null
sudo xattr -dr com.apple.quarantine "/Applications" 2>/dev/null
killall Dock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment