macOS-defaults命令

defaults 命令介绍

在 macOS 中,defaults 命令用于读写用户的偏好设置(preferences)。这些偏好设置通常存储在用户的 ~/Library/Preferences 目录下的 .plist(Property List)文件中。

所有可能用到的 domain 和 key https://www.defaults-write.com/

defaults 命令读写的配置文件

  1. 用户偏好设置文件

    • 这些文件位于 ~/Library/Preferences 目录下,文件名通常以应用程序的 Bundle Identifier 命名。例如,Safari 浏览器的偏好设置文件是 com.apple.Safari.plist
  2. 全局偏好设置文件

    • 这些文件位于 /Library/Preferences 目录下,适用于所有用户。例如,系统级别的网络设置可能存储在 /Library/Preferences/SystemConfiguration 目录下。

defaults 命令的常见用法

读取偏好设置

读取某个应用程序的偏好设置:

1
defaults read com.apple.Safari

读取全局偏好设置:

1
defaults read -g

读取特定键的值:

1
defaults read com.apple.Safari HomePage

写入偏好设置

写入某个应用程序的偏好设置:

1
defaults write com.apple.Safari HomePage -string "https://www.example.com"

写入全局偏好设置:

1
defaults write -g AppleInterfaceStyle -string "Dark"

删除偏好设置

删除某个应用程序的偏好设置:

1
defaults delete com.apple.Safari

删除特定键的值:

1
defaults delete com.apple.Safari HomePage

示例

假设你想更改 Finder 的显示隐藏文件的设置,可以使用以下命令:

1
2
3
4
5
6
7
8
# 显示隐藏文件
defaults write com.apple.finder AppleShowAllFiles -bool true

# 隐藏隐藏文件
defaults write com.apple.finder AppleShowAllFiles -bool false

# 重启 Finder 以应用更改
killall Finder

注意事项

  • 使用 defaults 命令时要小心,因为错误的操作可能会导致应用程序或系统行为异常。
  • 某些偏好设置的更改可能需要重启相关应用程序或系统才能生效。
  • defaults 命令主要用于用户级别的偏好设置,对于系统级别的配置文件,可能需要使用其他工具或手动编辑配置文件。

通过 defaults 命令,你可以方便地读取和修改 macOS 应用程序和系统的偏好设置,从而定制你的使用体验。


macOS-defaults命令
https://dnacore.github.io/post/1cfc3eaf-a7bf-49fe-85ec-85100ec40385.html
作者
DNACore
发布于
2019年1月28日
更新于
2024年10月31日
许可协议