首页 > 解决方案 > 使用 Bash 脚本自定义 macOS 的 Dock

问题描述

我想使用 Bash 脚本(没有 AppleScript)自定义 macOS 的 Dock。

目前我有代码(从这个答案中获取和修改):

#!/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

这会更改默认 Dock:

默认码头

至:

结果码头

但我也希望它删除垃圾箱旁边的下载文件夹以及出现在运行应用程序的垂直线之间的重复图标(在这种情况下只有 iTerm),这些图标保存/保存在 Dock 中(左侧垂直线)。像这样:

预计码头


此外,在进行更改后,代码更新 Dock 的方式是杀死 Dock 进程,然后自动重新启动。我觉得应该有一种更快的方法来做到这一点,它不会使 Dock 下降,然后桌面变黑一会儿,打开任何最小化的应用程序,然后使 Dock 上升(录制)。

标签: bashmacosdock

解决方案


在比较了defaults read com.apple.dock手动从 Dock 中删除图标之前和之后的打印内容后,我注意到我只需要删除recent-apps(对于重复的图标)和persistent-others(对于文件夹)键:

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

推荐阅读