bashmacosdock

Customize macOS's Dock with a Bash script


I want to customize macOS's Dock with a Bash script (without AppleScript).

Currently I have the code (taken and modified from this answer):

#!/bin/bash

defaults delete com.apple.dock persistent-apps

dock_item() {
    printf '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>%s</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>', "$1"
}

defaults write com.apple.dock persistent-apps -array \
    "$(dock_item /Applications/iTerm.app)" \
    "$(dock_item /Applications/Visual\ Studio\ Code.app)" \
    "$(dock_item /Applications/Slack.app)" \
    "$(dock_item /Applications/Google\ Chrome.app)" \
    "$(dock_item /System/Applications/Utilities/Screenshot.app)"

killall Dock

Which changes the default Dock:

default Dock

To:

resulted Dock

But I also want it to remove the Downloads folder next to the Trash bin and the duplicated icons that appear between the vertical lines (in this case only iTerm) of the running applications which are kept/saved in the Dock (to the left of the vertical lines). Like this:

expected Dock


Also, the way the Dock gets updated by the code after the changes are made is by killing the Dock processes which is then automatically restarted. I feel like there should be a faster way to do it that doesn't animate the Dock going down, then the desktop turning black for a moment, opening any minimized applications and then animate the Dock going up (recording).


Solution

  • After comparing what defaults read com.apple.dock prints before and after manually removing the icons from the Dock I noticed that I just have to delete the recent-apps (for the duplicated icons) and persistent-others (for the folder) keys as well:

    defaults delete com.apple.dock recent-apps
    defaults delete com.apple.dock persistent-others