Skip to content

Instantly share code, notes, and snippets.

@kamui545
Created October 24, 2019 03:37
Show Gist options
  • Save kamui545/c810eccf6281b33a53e094484247f5e8 to your computer and use it in GitHub Desktop.
Save kamui545/c810eccf6281b33a53e094484247f5e8 to your computer and use it in GitHub Desktop.
Customize macOS dock via command line
#!/usr/bin/env bash
source "./dock_functions.sh"
declare -a apps=(
'/System/Applications/Utilities/Terminal.app'
'/System/Applications/Music.app'
'/Applications/Google Chrome.app'
'/Applications/PhpStorm.app'
'/Applications/Visual Studio Code.app'
'/System/Applications/System Preferences.app'
);
declare -a folders=(
~/Downloads
);
clear_dock
disable_recent_apps_from_dock
for app in "${apps[@]}"; do
add_app_to_dock "$app"
done
for folder in "${folders[@]}"; do
add_folder_to_dock $folder
done
killall Dock
#!/usr/bin/env bash
# https://github.com/rpavlick/add_to_dock
# adds an application to macOS Dock
# usage: add_app_to_dock "Application Name"
# example add_app_to_dock "/System/Applications/Music.app"
function add_app_to_dock {
app="${1}"
if open -Ra "${app}"; then
echo "$app added to the Dock."
defaults write com.apple.dock persistent-apps -array-add "<dict>
<key>tile-data</key>
<dict>
<key>file-data</key>
<dict>
<key>_CFURLString</key>
<string>${app}</string>
<key>_CFURLStringType</key>
<integer>0</integer>
</dict>
</dict>
</dict>"
else
echo "ERROR: Application $1 not found."
fi
}
# adds a folder to macOS Dock
# usage: add_folder_to_dock "Folder Path" -a n -d n -v n
# example: add_folder_to_dock "~/Downloads" -a 2 -d 0 -v 1
# key:
# -a or --arrangement
# 1 -> Name
# 2 -> Date Added
# 3 -> Date Modified
# 4 -> Date Created
# 5 -> Kind
# -d or --displayAs
# 0 -> Stack
# 1 -> Folder
# -v or --showAs
# 0 -> Automatic
# 1 -> Fan
# 2 -> Grid
# 3 -> List
function add_folder_to_dock {
folder="${1}"
arrangement="1"
displayAs="0"
showAs="0"
while [[ "$#" -gt 0 ]]; do
case $1 in
-a|--arrangement)
if [[ $2 =~ ^[1-5]$ ]]; then
arrangement="${2}"
fi
;;
-d|--displayAs)
if [[ $2 =~ ^[0-1]$ ]]; then
displayAs="${2}"
fi
;;
-v|--showAs)
if [[ $2 =~ ^[0-3]$ ]]; then
showAs="${2}"
fi
;;
esac
shift
done
if [ -d "$folder" ]; then
echo "$folder added to the Dock."
defaults write com.apple.dock persistent-others -array-add "<dict>
<key>tile-data</key>
<dict>
<key>arrangement</key>
<integer>${arrangement}</integer>
<key>displayas</key>
<integer>${displayAs}</integer>
<key>file-data</key>
<dict>
<key>_CFURLString</key>
<string>file://${folder}</string>
<key>_CFURLStringType</key>
<integer>15</integer>
</dict>
<key>file-type</key>
<integer>2</integer>
<key>showas</key>
<integer>${showAs}</integer>
</dict>
<key>tile-type</key>
<string>directory-tile</string>
</dict>"
else
echo "ERROR: Folder $folder not found."
fi
}
function add_spacer_to_dock {
defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
}
function add_small_spacer_to_dock {
defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="small-spacer-tile";}'
}
function clear_apps_from_dock {
defaults delete com.apple.dock persistent-apps
}
function clear_others_from_dock {
defaults delete com.apple.dock persistent-others
}
function clear_dock {
clear_apps_from_dock
clear_others_from_dock
}
function disable_recent_apps_from_dock {
defaults write com.apple.dock show-recents -bool false
}
function enable_recent_apps_from_dock {
defaults write com.apple.dock show-recents -bool true
}
function reset_dock {
defaults delete com.apple.dock
killall Dock
}
@kaspersoerensen
Copy link

Brilliant!

@dataprolet
Copy link

Took me long enough to figure out how to add system apps, but here's my code for whoever it may be useful:

# Customize Dock
## Delete Dock
defaults write com.apple.dock persistent-apps -array

# Add system icons
declare -a sys_icons=(
    "/Applications/System Settings"
    "/Volumes/Preboot/Cryptexes/App/System/Applications/Safari"
    )
for sys_icon in "${sys_icons[@]}"; do
    defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/System${sys_icon}.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
done

## Add application icons
declare -a icons=("Google Chrome" "Slack" "Sourcetree" "IntelliJ IDEA CE" "XCode" "Android Studio" "iTerm" "Postman" "Postgres" "pgAdmin 4")
for icon in "${icons[@]}"; do
    if [ -d "/Applications/${icon}.app" ]; then
        if ! defaults read com.apple.dock | grep "${icon}"; then
            defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/${icon}.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
        fi
    fi
done

## Restart Dock
killall Dock

@thomasnal
Copy link

thomasnal commented Feb 22, 2025

This is absolutely awesome. Does anyone know how can I change settings such as "Desktop & Dock -> Drag windows to screen edges to tile" as well as the other three settings for automatic windows resizing and tiling?

None of the domains appear to have a key for these settings by default. And I cannot identify what other domain holds these settings. I looked into,

com.apple.dock
com.apple.systempreferences
com.apple.systemsettings.extensions
com.apple.WindowManager

@kamui545
Copy link
Author

Does anyone know how can I change settings such as "Desktop & Dock -> Drag windows to screen edges to tile" as well as the other three settings for automatic windows resizing and tiling?

You can try to find the settings by reviewing the diff with VSCode or another diff tool:

defaults read > before.txt
# change your settings on UI
defaults read > after.txt
code before.txt after.txt --diff

Sadly, keep in mind that some settings might not be stored in defaults, or that changing them via defaults might not be enough for it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment