Skip to content

Instantly share code, notes, and snippets.

@elferink
Forked from dannysmith/osx_setup.sh
Last active October 25, 2022 07:11
Show Gist options
  • Save elferink/6bc4e1a47d4be3214ac156fb5631e321 to your computer and use it in GitHub Desktop.
Save elferink/6bc4e1a47d4be3214ac156fb5631e321 to your computer and use it in GitHub Desktop.
Sensible defaults for New Mac
#!/usr/bin/env bash
set -e # exit on error
# Mostly based on the work of Mathias Bynens http://mths.be/osx
# and https://github.com/mphstudios/macOS_setup/blob/main/defaults_write.sh
SCREENSHOT_LOCATION="${HOME}/Desktop/Screenshots"
# Ask for the administrator password upfront
sudo -v
# Keep-alive: update existing `sudo` time stamp until `.osx` has finished
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Add Touch ID for sudo (if not already there)
if ! grep -q "pam_tid.so" /etc/pam.d/sudo; then
echo -e "auth sufficient pam_tid.so\n$(cat /etc/pam.d/sudo)" | sudo tee /etc/pam.d/sudo
fi
# Close any open System Preferences panes, to prevent them from overriding
# settings we're about to change
osascript -e 'tell application "System Preferences" to quit'
dw='defaults write'
PlistBuddy="/usr/libexec/PlistBuddy"
echo "Make sure you have enabled 'Full Disk Access' for the Terminal/iTerm app in 'System Preferences -> Privacy -> Full Disk Access'"
echo "Clear all applications in the Dock? (Meant for new installs) [y/n]"
read -r response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
defaults delete com.apple.dock persistent-apps
defaults delete com.apple.dock persistent-others
killall Dock
fi
###############################################################################
# General UI/UX #
###############################################################################
# Disable the sound effects on boot
sudo nvram SystemAudioVolume=" "
# Menu bar: hide the Time Machine, Volume, User, and Bluetooth icons
# for domain in ~/Library/Preferences/ByHost/com.apple.systemuiserver.*; do
# dw "${domain}" dontAutoLoad -array \
# "/System/Library/CoreServices/Menu Extras/TimeMachine.menu" \
# "/System/Library/CoreServices/Menu Extras/Volume.menu" \
# "/System/Library/CoreServices/Menu Extras/User.menu"
# done
# dw com.apple.systemuiserver menuExtras -array \
# "/System/Library/CoreServices/Menu Extras/Bluetooth.menu" \
# "/System/Library/CoreServices/Menu Extras/AirPort.menu" \
# "/System/Library/CoreServices/Menu Extras/Battery.menu" \
# "/System/Library/CoreServices/Menu Extras/Clock.menu"
# Increase window resize speed for Cocoa applications
$dw NSGlobalDomain NSWindowResizeTime -float 0.001
# Expand save panel by default
$dw NSGlobalDomain NSNavPanelExpandedStateForSaveMode -bool true
# Expand print panel by default
$dw NSGlobalDomain PMPrintingExpandedStateForPrint -bool true
# Save to disk (not to iCloud) by default
#dw NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool false
# Automatically quit printer app once the print jobs complete
$dw com.apple.print.PrintingPrefs "Quit When Finished" -bool true
# Disable the “Are you sure you want to open this application?” dialog
$dw com.apple.LaunchServices LSQuarantine -bool false
# Display ASCII control characters using caret notation in standard text views
# Try e.g. `cd /tmp; unidecode "\x{0000}" > cc.txt; open -e cc.txt`
$dw NSGlobalDomain NSTextShowsControlCharacters -bool true
# Disable automatic termination of inactive apps
#dw NSGlobalDomain NSDisableAutomaticTermination -bool true
# Set Help Viewer windows to non-floating mode
#dw com.apple.helpviewer DevMode -bool true
# Reveal IP address, hostname, OS version, etc. when clicking the clock
# in the login window
#sudo dw /Library/Preferences/com.apple.loginwindow AdminHostInfo HostName
# Restart automatically if the computer freezes
#systemsetup -setrestartfreeze on
# Check for software updates daily, not just once per week
$dw com.apple.SoftwareUpdate ScheduleFrequency -int 1
###############################################################################
# SSD-specific tweaks #
###############################################################################
# Disable local Time Machine snapshots
#sudo tmutil disablelocal
# Disable hibernation (speeds up entering sleep mode)
#sudo pmset -a hibernatemode 0
# Remove the sleep image file to save disk space
#sudo rm /Private/var/vm/sleepimage
# Create a zero-byte file instead…
#sudo touch /Private/var/vm/sleepimage
# …and make sure it can’t be rewritten
#sudo chflags uchg /Private/var/vm/sleepimage
###############################################################################
# Photos #
###############################################################################
# Prevent Photos from opening automatically when devices are plugged in
$dw com.apple.ImageCapture disableHotPlug -bool true
###############################################################################
# Trackpad, mouse, keyboard, Bluetooth accessories, and input #
###############################################################################
# Increase sound quality for Bluetooth headphones/headsets
# $dw com.apple.BluetoothAudioAgent "Apple Bitpool Min (editable)" -int 40
# Enable full keyboard access for all controls
# (e.g. enable Tab in modal dialogs)
$dw NSGlobalDomain AppleKeyboardUIMode -int 3
# Use scroll gesture with the Ctrl (^) modifier key to zoom
#$dw com.apple.universalaccess closeViewScrollWheelToggle -bool true
#$dw com.apple.universalaccess HIDScrollZoomModifierMask -int 262144
# Follow the keyboard focus while zoomed in
$dw com.apple.universalaccess closeViewZoomFollowsFocus -bool true
# Disable press-and-hold for keys in favor of key repeat
#$dw NSGlobalDomain ApplePressAndHoldEnabled -bool false
# Set a fast keyboard repeat rate
$dw NSGlobalDomain KeyRepeat -int 2
$dw NSGlobalDomain InitialKeyRepeat -int 15
# Disable emoji pop-up on fn press
$dw com.apple.HIToolbox AppleFnUsageType -bool false
###############################################################################
# Screen #
###############################################################################
# Save screenshots to location
mkdir -p $SCREENSHOT_LOCATION
$dw com.apple.screencapture location -string $SCREENSHOT_LOCATION
# Save screenshots in PNG format (other options: BMP, GIF, JPG, PDF, TIFF)
$dw com.apple.screencapture type -string "png"
# Enable subpixel font rendering on non-Apple LCDs
$dw NSGlobalDomain AppleFontSmoothing -int 2
# Enable HiDPI display modes (requires restart)
sudo $dw /Library/Preferences/com.apple.windowserver DisplayResolutionEnabled -bool true
###############################################################################
# Finder #
###############################################################################
# Finder: allow quitting via ⌘ + Q; doing so will also hide desktop icons
$dw com.apple.finder QuitMenuItem -bool true
# Keep folders on top
$dw com.apple.finder "_FXSortFoldersFirst" -bool "true"
# Automatically empty bin after 30 days
$dw com.apple.finder "FXRemoveOldTrashItems" -bool "true"
# Set $HOME as the default location for new Finder windows
# For other paths, use `PfLo` and `file:///full/path/here/`
$dw com.apple.finder NewWindowTarget -string "PfLo"
$dw com.apple.finder NewWindowTargetPath -string "file://${HOME}/"
# Hide icons for hard drives, servers, and removable media on the desktop
$dw com.apple.finder ShowExternalHardDrivesOnDesktop -bool false
$dw com.apple.finder ShowHardDrivesOnDesktop -bool false
$dw com.apple.finder ShowMountedServersOnDesktop -bool false
$dw com.apple.finder ShowRemovableMediaOnDesktop -bool false
# Finder: show hidden files by default
# $dw com.apple.finder AppleShowAllFiles -bool true
# Show all filename extensions in Finder by default
$dw NSGlobalDomain AppleShowAllExtensions -bool true
# Finder: show path bar
$dw com.apple.finder ShowPathbar -bool true
# Show status bar in Finder
$dw com.apple.finder ShowStatusBar -bool true
# Finder: allow text selection in Quick Look
$dw com.apple.finder QLEnableTextSelection -bool true
# Display full POSIX path as Finder window title
$dw com.apple.finder _FXShowPosixPathInTitle -bool true
# When performing a search, search the current folder by default
$dw com.apple.finder FXDefaultSearchScope -string "SCcf"
# Disable the warning when changing a file extension
$dw com.apple.finder FXEnableExtensionChangeWarning -bool false
# Enable spring loading for directories
$dw NSGlobalDomain com.apple.springing.enabled -bool true
# Remove the spring loading delay for directories
$dw NSGlobalDomain com.apple.springing.delay -float 0
# Avoid creating .DS_Store files on network volumes
$dw com.apple.desktopservices DSDontWriteNetworkStores -bool true
# Automatically open a new Finder window when a volume is mounted
$dw com.apple.frameworks.diskimages auto-open-ro-root -bool true
$dw com.apple.frameworks.diskimages auto-open-rw-root -bool true
$dw com.apple.finder OpenWindowForNewRemovableDisk -bool true
# Enable snap-to-grid for icons on the desktop and in other icon views
$PlistBuddy -c "Set :DesktopViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
$PlistBuddy -c "Set :FK_StandardViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
$PlistBuddy -c "Set :StandardViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
# Use list view in all Finder windows by default
# Four-letter codes for the other view modes: `icnv`, `clmv`, `Flwv`
$dw com.apple.finder FXPreferredViewStyle -string "Nlsv"
# Enable AirDrop over Ethernet and on unsupported Macs running Lion
$dw com.apple.NetworkBrowser BrowseAllInterfaces -bool true
# Enable the MacBook Air SuperDrive on any Mac
# sudo nvram boot-args="mbasd=1"
# Show the ~/Library folder
# chflags nohidden ~/Library
# Expand the following File Info panes:
# "General", "Open with", and "Sharing & Permissions"
$dw com.apple.finder FXInfoPanesExpanded -dict \
General -bool true \
OpenWith -bool true \
Privileges -bool true
###############################################################################
# Dock, Dashboard, and hot corners #
###############################################################################
# Enable highlight hover effect for the grid view of a stack (Dock)
$dw com.apple.dock mouse-over-hilite-stack -bool true
# Minimize windows into their application's icon
# $dw com.apple.dock minimize-to-application -bool true
# Enable spring loading for all Dock items
$dw com.apple.dock enable-spring-load-actions-on-all-items -bool true
# Speed up Mission Control animations
$dw com.apple.dock expose-animation-duration -float 0.1
# Disable Dashboard
$dw com.apple.dashboard mcx-disabled -bool true
# Don't show Dashboard as a Space
$dw com.apple.dock dashboard-in-overlay -bool true
# Don't automatically rearrange Spaces based on most recent use
$dw com.apple.dock mru-spaces -bool false
# Remove the auto-hiding Dock delay
$dw com.apple.dock autohide-delay -float 0
# Automatically hide and show the Dock
$dw com.apple.dock autohide -bool true
# Make Dock icons of hidden applications translucent
$dw com.apple.dock showhidden -bool true
# Make Dock more transparent
$dw com.apple.dock hide-mirror -bool true
# Reset Launchpad
find ~/Library/Application\ Support/Dock -name "*.db" -maxdepth 1 -delete
###############################################################################
# Privacy #
###############################################################################
# Do not send search queries to Apple
$dw com.apple.Safari UniversalSearchEnabled -bool false
$dw com.apple.Safari SuppressSearchSuggestions -bool true
# Disable personalized ads
$dw com.apple.AdLib allowApplePersonalizedAdvertising -int 0
# Replace default Google search with DuckDuckGO
$PlistBuddy \
-c 'delete :NSPreferredWebServices:NSWebServicesProviderWebSearch:NSDefaultDisplayName' \
-c 'add :NSPreferredWebServices:NSWebServicesProviderWebSearch:NSDefaultDisplayName string "DuckDuckGo"' \
\
-c 'delete :NSPreferredWebServices:NSWebServicesProviderWebSearch:NSProviderIdentifier' \
-c 'add :NSPreferredWebServices:NSWebServicesProviderWebSearch:NSProviderIdentifier string "com.duckduckgo"' \
\
~/Library/Preferences/.GlobalPreferences.plist
###############################################################################
# Safari & WebKit #
###############################################################################
# Set Safari's home page to `about:blank` for faster loading
$dw com.apple.Safari HomePage -string "about:blank"
# Disable hitting the Backspace key to go to the previous page in history
$dw com.apple.Safari com.apple.Safari.ContentPageGroupIdentifier.WebKit2BackspaceKeyNavigationEnabled -bool false
# Hide Safari's sidebar in Top Sites
$dw com.apple.Safari ShowSidebarInTopSites -bool false
# Enable Safari's debug menu
$dw com.apple.Safari IncludeInternalDebugMenu -bool true
# Make Safari's search banners default to Contains instead of Starts With
$dw com.apple.Safari FindOnPageMatchesWordStartsOnly -bool false
# Remove useless icons from Safari's bookmarks bar
$dw com.apple.Safari ProxiesInBookmarksBar "()"
# Enable the Develop menu and the Web Inspector in Safari
$dw com.apple.Safari IncludeDevelopMenu -bool true
$dw com.apple.Safari WebKitDeveloperExtrasEnabledPreferenceKey -bool true
$dw com.apple.Safari com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled -bool true
# Add a context menu item for showing the Web Inspector in web views
$dw NSGlobalDomain WebKitDeveloperExtras -bool true
# Stop Safari from automatically opening some files after downloading
$dw com.apple.Safari AutoOpenSafeDownloads -bool false
###############################################################################
# Mail #
###############################################################################
# Copy email addresses as `foo@example.com` instead of `Foo Bar <foo@example.com>` in Mail.app
$dw com.apple.mail AddressesIncludeNameOnPasteboard -bool false
# Add the keyboard shortcut ⌘ + Enter to send an email in Mail.app
$dw com.apple.mail NSUserKeyEquivalents -dict-add "Send" -string "@\\U21a9"
# Display emails in threaded mode, sorted by date (oldest at the top)
$dw com.apple.mail DraftsViewerAttributes -dict-add "DisplayInThreadedMode" -string "yes"
$dw com.apple.mail DraftsViewerAttributes -dict-add "SortedDescending" -string "yes"
$dw com.apple.mail DraftsViewerAttributes -dict-add "SortOrder" -string "received-date"
###############################################################################
# Terminal & iTerm 2 #
###############################################################################
# Only use UTF-8 in Terminal.app
$dw com.apple.terminal StringEncodings -array 4
###############################################################################
# Time Machine #
###############################################################################
# Prevent Time Machine from prompting to use new hard drives as backup volume
$dw com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool true
# Disable local Time Machine backups
# hash tmutil &> /dev/null && sudo tmutil disablelocal
###############################################################################
# Activity Monitor #
###############################################################################
# Show the main window when launching Activity Monitor
$dw com.apple.ActivityMonitor OpenMainWindow -bool true
# Visualize CPU usage in the Activity Monitor Dock icon
$dw com.apple.ActivityMonitor IconType -int 5
# Show all processes in Activity Monitor
$dw com.apple.ActivityMonitor ShowCategory -int 0
# Sort Activity Monitor results by CPU usage
$dw com.apple.ActivityMonitor SortColumn -string "CPUUsage"
$dw com.apple.ActivityMonitor SortDirection -int 0
###############################################################################
# Address Book, Dashboard, iCal, TextEdit, and Disk Utility #
###############################################################################
# Enable the debug menu in Address Book
$dw com.apple.addressbook ABShowDebugMenu -bool true
# Enable Dashboard dev mode (allows keeping widgets on the desktop)
$dw com.apple.dashboard devmode -bool true
# Enable the debug menu in iCal (pre-10.8)
$dw com.apple.iCal IncludeDebugMenu -bool true
# Use plain text mode for new TextEdit documents
$dw com.apple.TextEdit RichText -int 0
# Open and save files as UTF-8 in TextEdit
$dw com.apple.TextEdit PlainTextEncoding -int 4
$dw com.apple.TextEdit PlainTextEncodingForWrite -int 4
# Enable the debug menu in Disk Utility
$dw com.apple.DiskUtility DUDebugMenuEnabled -bool true
$dw com.apple.DiskUtility advanced-image-options -bool true
###############################################################################
# Mac App Store #
###############################################################################
# Enable the WebKit Developer Tools in the Mac App Store
$dw com.apple.appstore WebKitDeveloperExtras -bool true
# Enable Debug Menu in the Mac App Store
$dw com.apple.appstore ShowDebugMenu -bool true
# Enable the automatic update check
$dw com.apple.SoftwareUpdate AutomaticCheckEnabled -bool true
# Check for software updates daily, not just once per week
$dw com.apple.SoftwareUpdate ScheduleFrequency -int 1
# Download newly available updates in background
$dw com.apple.SoftwareUpdate AutomaticDownload -int 1
# Install System data files & security updates
$dw com.apple.SoftwareUpdate CriticalUpdateInstall -int 1
# Automatically download apps purchased on other Macs
$dw com.apple.SoftwareUpdate ConfigDataInstall -int 1
# Turn on app auto-update
$dw com.apple.commerce AutoUpdate -bool true
###############################################################################
# BT Audio fixes #
###############################################################################
$dw com.apple.BluetoothAudioAgent "Apple Bitpool Max (editable)" 80
$dw com.apple.BluetoothAudioAgent "Apple Bitpool Min (editable)" 48
$dw com.apple.BluetoothAudioAgent "Apple Initial Bitpool (editable)" 40
$dw com.apple.BluetoothAudioAgent "Negotiated Bitpool" 58
$dw com.apple.BluetoothAudioAgent "Negotiated Bitpool Max" 58
$dw com.apple.BluetoothAudioAgent "Negotiated Bitpool Min" 48
$dw com.apple.BluetoothAudioAgent "Stream - Flush Ring on Packet Drop (editable)" 0
$dw com.apple.BluetoothAudioAgent "Stream - Max Outstanding Packets (editable)" 30
$dw com.apple.BluetoothAudioAgent "Stream Resume Delay" "0.75"
###############################################################################
# Spelling #
###############################################################################
# Disable automatic capitalization
$dw NSGlobalDomain NSAutomaticCapitalizationEnabled -bool false
# Disable smart-dashes
$dw NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool false
# Disable automatic period substitution
$dw NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool false
# Disable smart quotes
$dw NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false
# Disable automatic spelling correct
$dw NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false
$dw NSGlobalDomain NSAutomaticTextCompletionEnabled -bool false
$dw -g WebAutomaticSpellingCorrectionEnabled -boolean false
###############################################################################
# Spotlight #
###############################################################################
# Disable Spotlight indexing for any volume that gets mounted and has not yet
# been indexed before.
# Use `sudo mdutil -i off "/Volumes/foo"` to stop indexing any volume.
sudo $PlistBuddy -c "Add :Exclusions:0 string '/Volumes'" /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist
# Change indexing order and disable some search results
# Yosemite-specific search results (remove them if you are using macOS 10.9 or older):
# MENU_DEFINITION
# MENU_CONVERSION
# MENU_EXPRESSION
# MENU_SPOTLIGHT_SUGGESTIONS (send search queries to Apple)
# MENU_WEBSEARCH (send search queries to Apple)
# MENU_OTHER
$dw com.apple.spotlight orderedItems -array \
'{"enabled" = 0;"name" = "MENU_DEFINITION";}' \
'{"enabled" = 1;"name" = "MENU_CONVERSION";}' \
'{"enabled" = 0;"name" = "MENU_OTHER";}' \
'{"enabled" = 1;"name" = "MENU_EXPRESSION";}' \
'{"enabled" = 0;"name" = "MENU_SPOTLIGHT_SUGGESTIONS";}' \
'{"enabled" = 0;"name" = "MENU_WEBSEARCH";}' \
'{"enabled" = 1;"name" = "DOCUMENTS";}' \
'{"enabled" = 1;"name" = "DIRECTORIES";}' \
'{"enabled" = 1;"name" = "PDF";}' \
'{"enabled" = 0;"name" = "PRESENTATIONS";}' \
'{"enabled" = 0;"name" = "SPREADSHEETS";}' \
'{"enabled" = 0;"name" = "MESSAGES";}' \
'{"enabled" = 0;"name" = "CONTACT";}' \
'{"enabled" = 0;"name" = "EVENT_TODO";}' \
'{"enabled" = 0;"name" = "BOOKMARKS";}' \
'{"enabled" = 0;"name" = "IMAGES";}' \
'{"enabled" = 0;"name" = "MOVIES";}' \
'{"enabled" = 0;"name" = "MUSIC";}' \
'{"enabled" = 1;"name" = "APPLICATIONS";}' \
'{"enabled" = 1;"name" = "FONTS";}' \
'{"enabled" = 1;"name" = "SYSTEM_PREFS";}' \
'{"enabled" = 0;"name" = "SOURCE";}'
# Load new settings before rebuilding the index
# killall mds > /dev/null 2>&1
sudo launchctl stop com.apple.metadata.mds
sudo launchctl start com.apple.metadata.mds
# Make sure indexing is enabled for the main volume
sudo mdutil -i on / > /dev/null
# Rebuild the index from scratch
sudo mdutil -E / > /dev/null
###############################################################################
# Kill affected applications #
###############################################################################
declare -a apps=("cfprefsd" \
"Activity Monitor" \
"Calendar" \
"Contacts" \
"Dock" \
"Finder" \
"Google Chrome" \
"iCal" \
"Mail" \
"Messages" \
"NotificationCenter" \
"Photos" \
"Safari" \
"SystemUIServer"
"Terminal" \
"Twitter" \
)
for app in ${apps[@]} ; do
killall "${app}" > /dev/null 2>&1
done
echo "Done. Note that some of these changes require a logout/restart to take effect."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment