Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bilenkonito/35046164fbd9e5a18c4a5b461723957c to your computer and use it in GitHub Desktop.

Select an option

Save bilenkonito/35046164fbd9e5a18c4a5b461723957c to your computer and use it in GitHub Desktop.
macOS Preferences Defaults
#!/usr/bin/env zsh
#===========================================================================
# * INFO
#
# macOS Preferences Defaults
# By Christopher Allen @ChristopherA https://github.com/christophera/
# My most basic macOS preferences, in this gist to make it easy to load on
# multiple machines and VMs. Ideally runs on all macOS versions since Yosemite,
# but only tested recently with macOS Big Sur 11.6 & macOS Mojave 12.0.1.
# As a convenience for bootstrapping, you can execute this script remotely,
# which will update your a Mac with all these settings using `curl`, which will
# not leave itself on your system (ideally a newly installed Mac system). Just
# paste the following command into the Terminal (`⌘␣Terminal`):
# curl -L https://gist.githubusercontent.com/ChristopherA/98628f8cd00c94f11ee6035d53b0d3c6/raw/macOS-Preferences-Defaults.sh | bash
# ! WARNING: Be cautious whenever someone asks you to run a script that has
# ! `curl` command in it, because if the author is malicious they can corrupt
# ! your system or make it vulnerable. In this case the script is run from a
# ! trusted website (`gist.githubusercontent.com/`), and is from my account
# ! (`ChristopherA`). Make sure you look at the script the URL refers to and
# ! confirm the URL is the same as you pasted into the Terminal, before hitting
# ! return!
# Much of this script is inspired by from Mathias Bynens' famous ~/.osx —
# https://mths.be/osx - https://github.com/mathiasbynens/dotfiles
#===========================================================================
#================================================
# * TEST ENVIROMENT
#================================================
# Mac Hardware Model
osx_hardware_model=$(sysctl -n hw.model)
# VMware detection. Used to set VMware specific preferences.
if [ ! -z "$(echo $osx_hardware_model | grep VMware)" ]; then
osx_hardware_model_is_vm = true
# else
# printf "This macOS is NOT running under VMware. ABORT\n"
# exit 1
fi
# Which version and build of macOS are we executing under?
read osx_product_version osx_vers_major osx_vers_minor osx_vers_patch \
<<< $(sw_vers -productVersion | awk -F. '{print $0 " " $1 " " $2 " " $3}')
osx_build_version="$(sw_vers -buildVersion)"
printf "Running $osx_distro inside of $osx_hardware_model.\n"
# What is the human-readable codename for this macOS?
case "$osx_product_version" in
"10.4"*) osx_codename="Mac OS X Tiger" ;;
"10.5"*) osx_codename="Mac OS X Leopard" ;;
"10.6"*) osx_codename="Mac OS X Snow Leopard" ;;
"10.7"*) osx_codename="Mac OS X Lion" ;;
"10.8"*) osx_codename="OS X Mountain Lion" ;;
"10.9"*) osx_codename="OS X Mavericks" ;;
"10.10"*) osx_codename="OS X Yosemite" ;;
"10.11"*) osx_codename="OS X El Capitan" ;;
"10.12"*) osx_codename="macOS Sierra" ;;
"10.13"*) osx_codename="macOS High Sierra" ;;
"10.14"*) osx_codename="macOS Mojave" ;;
"10.15"*) osx_codename="macOS Catalina" ;;
"11."*) osx_codename="macOS Big Sur" ;;
"12."*) osx_codename="macOS Monterey" ;;
*) osx_codename="macOS" ;;
esac
osx_distro="$osx_codename $osx_product_version ($osx_build_version)"
printf "Running $osx_distro inside of $osx_hardware_model.\n"
#================================================
# * TEST TERMINAL FOR FULL DISK ACCESS
#================================================
# Test if Terminal has Full Disk Access, and if it doesn't, prompt user to set
# it and start over.
# ? A number of preferences now require Terminal.app to have Full
# ? Disk Access, in particular Safari.app.
# ? https://lapcatsoftware.com/articles/containers.html
# TODO: Wrap with $osx_product_version tests
printf "Testing for Full Disk Access:\n\n"
errstr=$( /bin/ls /Users/admin/Library/Containers/com.apple.Safari 3>&1 1>&2 2>&3 3>&- )
# ? Full disk access has only been in since Mojave, but which files were protected first?
# TODO: Research if there is an older file to test.
if [[ $errstr == *"Operation not permitted" ]]; then
printf "Terminal.app needs Full Disk Access permission\n"
# Prompt user to give Terminal Full Disk Access
# ! This worked earlier in VMware Montery, but failed on M1 MacBook Pro (14-inch 2021)
# TODO: Investigate
osascript -e "tell application \"System Preferences\" to activate " \
-e "tell application \"System Preferences\" to reveal anchor \"Privacy_AllFiles\" of pane id \"com.apple.preference.security\" " \
-e "display dialog \"Before continuing:\n\nUnlock and check the box next to Terminal to give it full disk access.\n\nThen quit Terminal and run this script again.\" buttons {\"OK\"} default button 1 with icon caution "
exit # as we can't proceed until Terminal has been granted full Disk Access
else
printf "Terminal.app has permission to continue\n"
fi
#================================================
# * CLOSE SYSTEM PREFERENCES
#================================================
# 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'
#================================================
# * SYSTEMUISERVER
#================================================
# from https://gist.github.com/AndrewWCarson/d4e2e1e601b759c19553fec2310433e2
# for user in $(dscl . list /Users UniqueID | awk '$2 >= 500 {print $1}'); do
# userHome=$(dscl . read /Users/"$user" NFSHomeDirectory | sed 's/NFSHomeDirectory://' | grep "/" | sed 's/^[ \t]*//')
# if [ "$(defaults read "${userHome}/Library/Preferences/com.apple.systemuiserver.plist" 2> /dev/null | grep -i user.menu)" == "" ]; then
# touch "${userHome}/Library/Preferences/com.apple.systemuiserver.plist"
# defaults write "${userHome}/Library/Preferences/com.apple.systemuiserver.plist" menuExtras -array-add '<string>/System/Library/CoreServices/Menu Extras/User.menu</string>'
# fi
# done
# defaults write /Library/Preferences/.GlobalPreferences MultipleSessionEnabled -bool Yes
# Show script menu in menu bar
osascript <<EOT
tell application "AppleScript Utility"
set Script menu enabled to true
set application scripts position to top -- top or bottom
set show Computer scripts to false -- true or false
end tell
EOT
#================================================
# * DOCK
#================================================
if [[ "$osx_hardware_model_is_vm" = true ]] ; then
## Position the Dock on the bottom
defaults write com.apple.dock orientation bottom #default, my preference for a VM
else
## Position the Dock on the right
defaults write com.apple.dock orientation right #my preference for main machine
fi
## Set the icon size of Dock items to 27 pixels
defaults write com.apple.dock tilesize -int 27
# defaults write com.apple.dock tilesize -int ??
#================================================
# * FINDER
#================================================
# Quitting via ⌘ + Q; doing so will also hide desktop icons
# defaults write com.apple.finder QuitMenuItem -bool true
# Window animations and Get Info animations
defaults write com.apple.finder DisableAllAnimations -bool true
# Icons for hard drives, servers, and removable media on the desktop
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool false
defaults write com.apple.finder ShowHardDrivesOnDesktop -bool false
defaults write com.apple.finder ShowMountedServersOnDesktop -bool false
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool false
# Visibility of hidden files
#defaults write com.apple.finder AppleShowAllFiles -bool true
# Filename extensions
# defaults write NSGlobalDomain AppleShowAllExtensions -bool true
# Status bar
defaults write com.apple.finder ShowStatusBar -bool true
# Path bar
defaults write com.apple.finder ShowPathbar -bool true
# Text selection in Quick Look
defaults write com.apple.finder QLEnableTextSelection -bool true
# Full POSIX path as window title
defaults write com.apple.finder _FXShowPosixPathInTitle -bool false
# Search scope
# This Mac : `SCev`
# Current Folder : `SCcf`
# Previous Scope : `SCsp`
defaults write com.apple.finder FXDefaultSearchScope -string "SCcf"
# File extension change warning
# defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false
# Arrange by
# Kind, Name, Application, Date Last Opened,
# Date Added, Date Modified, Date Created, Size, Tags, None
defaults write com.apple.finder FXPreferredGroupBy -string "Name"
# Spring loaded directories
defaults write NSGlobalDomain com.apple.springing.enabled -bool true
# Delay for spring loaded directories
defaults write NSGlobalDomain com.apple.springing.delay -float 0.3
# Writing of .DS_Store files on network or USB volumes
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true
# Disk image verification
defaults write com.apple.frameworks.diskimages skip-verify -bool true
defaults write com.apple.frameworks.diskimages skip-verify-locked -bool true
defaults write com.apple.frameworks.diskimages skip-verify-remote -bool true
# Automatically open a new Finder window when a volume is mounted
defaults write com.apple.frameworks.diskimages auto-open-ro-root -bool false
defaults write com.apple.frameworks.diskimages auto-open-rw-root -bool false
defaults write com.apple.finder OpenWindowForNewRemovableDisk -bool false
# Set icon view settings on desktop and in icon views
# ! This no longer works in Monterey 12.0.1
# for view in 'Desktop' 'FK_Standard' 'Standard'; do
#
# # Item info near icons
# /usr/libexec/PlistBuddy -c "Set :${view}ViewSettings:IconViewSettings:showItemInfo bool true" ~/Library/Preferences/com.apple.finder.plist
#
# # Item info to right of icons
# /usr/libexec/PlistBuddy -c "Set :${view}ViewSettings:IconViewSettings:labelOnBottom bool false" ~/Library/Preferences/com.apple.finder.plist
#
# # Snap-to-grid for icons
# /usr/libexec/PlistBuddy -c "Set :${view}ViewSettings:IconViewSettings:arrangeBy string grid" ~/Library/Preferences/com.apple.finder.plist
#
# # Grid spacing for icons
# /usr/libexec/PlistBuddy -c "Set :${view}ViewSettings:IconViewSettings:gridSpacing integer 100" ~/Library/Preferences/com.apple.finder.plist
#
# # Icon size
# /usr/libexec/PlistBuddy -c "Set :${view}ViewSettings:IconViewSettings:iconSize integer 32" ~/Library/Preferences/com.apple.finder.plist
#
# done
# Preferred view style
# Icon View : `icnv`
# List View : `Nlsv`
# Column View : `clmv`
# Cover Flow : `Flwv`
# After configuring preferred view style, clear all `.DS_Store` files
# to ensure settings are applied for every directory
# sudo find / -name ".DS_Store" --delete
defaults write com.apple.finder FXPreferredViewStyle -string "clmv"
# Keep folders on top when sorting by name
# defaults write com.apple.finder _FXSortFoldersFirst -bool true
# View Options
# ! This no longer works in Monterey 12.0.1
# ColumnShowIcons : Show preview column
# ShowPreview : Show icons
# ShowIconThumbnails : Show icon preview
# ArrangeBy : Sort by
# dnam : Name
# kipl : Kind
# ludt : Date Last Opened
# pAdd : Date Added
# modd : Date Modified
# ascd : Date Created
# logs : Size
# labl : Tags
# /usr/libexec/PlistBuddy \
# -c "Set :StandardViewOptions:ColumnViewOptions:ColumnShowIcons bool false" \
# -c "Set :StandardViewOptions:ColumnViewOptions:FontSize integer 11" \
# -c "Set :StandardViewOptions:ColumnViewOptions:ShowPreview bool true" \
# -c "Set :StandardViewOptions:ColumnViewOptions:ArrangeBy string dnam" \
# ~/Library/Preferences/com.apple.finder.plist
# New window target
# Computer : `PfCm`
# Volume : `PfVo`
# $HOME : `PfHm`
# Desktop : `PfDe`
# Documents : `PfDo`
# All My Files : `PfAF`
# Other… : `PfLo`
defaults write com.apple.finder NewWindowTarget -string 'PfHm'
#defaults write com.apple.finder NewWindowTargetPath -string "file://${HOME}/"
# Warning before emptying Trash
defaults write com.apple.finder WarnOnEmptyTrash -bool false
# Empty Trash securely
defaults write com.apple.finder EmptyTrashSecurely -bool false
# AirDrop over Ethernet and on unsupported Macs running Lion
defaults write com.apple.NetworkBrowser BrowseAllInterfaces -bool true
# Desktop Enabled
defaults write com.apple.finder CreateDesktop -bool true
# Expand the following File Info panes:
# “General”, “Open with”, and “Sharing & Permissions”
defaults write com.apple.finder FXInfoPanesExpanded -dict \
General -bool true \
OpenWith -bool true \
Privileges -bool true
#================================================
# * HOT CORNERS
#================================================
# Hot corners
# Possible values:
# 0: no-op
# 2: Mission Control
# 3: Show application windows
# 4: Desktop
# 5: Start screen saver
# 6: Disable screen saver
# 7: Dashboard
# 10: Put display to sleep
# 11: Launchpad
# 12: Notification Center
if [[ "$osx_hardware_model_is_vm" = true ]] ; then
# VM don't need corner screen saver
defaults write com.apple.dock wvous-bl-corner -int 0 # no-op
defaults write com.apple.dock wvous-bl-modifier -int 0
else
# Bottom left screen corner → Start screen saver
defaults write com.apple.dock wvous-bl-corner -int 10 # sleep display
defaults write com.apple.dock wvous-bl-modifier -int 0
fi
#================================================
# * SAFARI
#================================================
# ? NOTE: Many of these don't work anymore, and those that do require Terminal
# ? to have Full Disk Access (which is tested and set above).
# ? https://lapcatsoftware.com/articles/containers.html
# TODO: test each setting if works in Monterey & Big Sur.
# Delete the old redundant .plist file, the correct location is
# ~/Library/Containers/com.apple.Safari since Safari 13 in Catalina 10.15
# Get Safari Version
read osx_safari_version osx_safari_version_major osx_safari_version_minor osx_safari_version_patch \
<<< $(/usr/libexec/PlistBuddy -c "print :CFBundleShortVersionString" \
/Applications/Safari.app/Contents/Info.plist | \
awk -F. '{print $0 " " $1 " " $2 " " $3}')
# If Safari 13 or greater, delete outdated redundant pref file from earlier Safari
if [[ -f ~/Library/Preferences/com.apple.Safari.plist && "$osx_safari_version_major" -ge 13 ]]; then
rm ~/Library/Preferences/com.apple.Safari.plist
fi
# Show status bar
# defaults write com.apple.Safari ShowStatusBar -bool true
# ! Doesn't seem to work Safari 15 on macOS Monterey
# Show hovering overlay on links
defaults write com.apple.Safari ShowOverlayStatusBar -bool true #default is false
# * [x] Does work in Safari 15 on macOS Monterey
# Safari opens with: last session
defaults write com.apple.Safari AlwaysRestoreSessionAtLaunch -bool true
# * [x] Does work in Safari 15 on macOS Monterey
# * [x] Does work in Safari 14.1.2 on macOS Mojave 10.14.6
# Set Safari’s home page to `about:blank` for faster loading
# defaults write com.apple.Safari HomePage -string "about:blank"
# ! Doesn't seem to work Safari 15 on macOS Monterey
# ? [ ] Set manually
# Don't open files in Safari after downloading:
defaults write com.apple.Safari AutoOpenSafeDownloads -bool false
# * [x] Does work in Safari 15 on macOS Monterey
# Show Develop menu in Safari
# defaults write com.apple.Safari IncludeDevelopMenu -bool true
# ! Doesn't seem to work Safari 15 on macOS Monterey, as it is now Debug
# TODO wrap these to test safari version
# Enable Safari’s Debug menu
defaults write com.apple.Safari IncludeInternalDebugMenu -bool true
# * [x] Does work in Safari 15 on macOS Monterey
#================================================
# * KILL AFFECTED APPLICATIONS
#================================================
# echo "Kill all affected actions, press any key..."
# read KEY
# ? This only works in interactive mode, not in `curl`.
# TODO: puzzle out a solution for use with `curl`
for app in "SystemUIServer" "cfprefsd" \
"Finder" "Dock" "Safari" \
# "Terminal" \
do
killall "${app}" > /dev/null 2>&1
done
sleep 2
open -a Safari
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment