defaults - macOS


Overview

The defaults command in macOS is used for interacting with the defaults system, which manages user preferences. This command allows you to read, write, and delete macOS user defaults from the command line. It is particularly useful for automation scripts, system administrators, and developers who need to modify application settings programmatically or explore system and application configurations.

Syntax

The basic syntax of the defaults command is as follows:

defaults [command] [domain] [key] [value]
  • [command]: The operation to perform, such as read, write, or delete.
  • [domain]: The domain identifier, usually in reverse DNS format (e.g., com.apple.finder), or a path to a plist file.
  • [key]: The preference key to read, write, or delete.
  • [value]: The value to set for the key when writing preferences.

Options/Flags

  • read: Reads the value of a specific default for a domain.
    • Usage: defaults read <domain> <key>
  • write: Writes a value to a specific default for a domain.
    • Usage: defaults write <domain> <key> <value>
  • delete: Removes a default from a domain.
    • Usage: defaults delete <domain> <key>

Additional flags:

  • -currentHost: Only access defaults for the current host.
  • -host : Specifies a specific host.
  • -int, -float, -bool, -string: Specifies the type of data being written when using the write command.

Examples

  1. Reading a Default Value:

    defaults read com.apple.dock autohide
    

    This command reads the auto-hide setting for the Dock on macOS.

  2. Writing a Default Value:

    defaults write com.apple.screencapture location ~/Desktop
    

    Sets the default location for screen captures to the Desktop.

  3. Deleting a Default Value:

    defaults delete com.apple.dock autohide
    

    Removes the auto-hide setting from the Dock preferences, reverting it to its default state.

Common Issues

  • Permissions Errors: Make sure you have the necessary permissions to modify specific preference settings.
  • Incorrect Domain or Key: Errors can occur if the domain or key does not exist. Double-check the identifiers you are using.
  • Type Mismatches: When writing values, ensure the data type matches the expected type for the preference key.

Integration

defaults can be combined with shell scripts or other command-line tools to automate setup or configuration tasks. Here’s an example script to configure multiple system preferences:

#!/bin/bash
defaults write com.apple.dock autohide -bool true
defaults write com.apple.screencapture type -string "PNG"
killall Dock  # Restarts the Dock to apply changes
  • plistbuddy: An alternative tool for managing plist files.
  • open: Useful for opening plist files directly with the default GUI application.

For more detailed and developer-focused information, refer to the Property List Programming Guide from Apple.