Domanda

Having the following .plist file:

/usr/bin/sudo /usr/bin/defaults read /Library/Preferences/com.apple.systempreferences.plist

{
    DisabledPreferencePanes =     (
        "com.apple.preference.general",
        "com.apple.preference.mouse"
    );
}

Using the defaults command line tool, if I want to delete for example only the com.apple.preference.mouse line from the plist how do I do that?

È stato utile?

Soluzione

The output of the defaults command, in your OP, shows a single key, DisabledPreferencePanes, as an array with two elements. Unfortunately defaults cannot explicitly delete a single element in an array containing multiple elements, in this case com.apple.preference.mouse.

Since the target com.apple.systempreferences.plist file in /Library/Preferences only has the single key as an array, the entire file and thus the array can be overwritten without the target element in the array, e.g.:

    NOTE: This form of the command overwrites the entire target file.

sudo defaults write /Library/Preferences/com.apple.systempreferences.plist '{ DisabledPreferencePanes = ("com.apple.preference.general"); }'

If the target .plist file had other keys, you could overwrite just the target key, e.g:

    NOTE: This form of the command overwrites just the target array.

sudo defaults write /Library/Preferences/com.apple.systempreferences.plist DisabledPreferencePanes -array com.apple.preference.general

That said, I prefer to use PlistBuddy because a single element of the array can be deleted, e.g.:

sudo /usr/libexec/PlistBuddy -c "Delete :DisabledPreferencePanes:1" /Library/Preferences/com.apple.systempreferences.plist
  • In PlistBuddy, array items are specified by a zero-based integer index.

PlistBuddy can also be easier to use in a shell script where one could code it to find a target element of an array and delete it. This could not be done with defaults as it can only write (or overwrite) an array without the target element in it.


Notes:

  • System Preferences should be closed when modifying its releated .plist files.

  • Immediately after modifying the target file, in this use case, you need to terminate all occurrences of cfprefsd.

    • As one is owned by root you'll need to use sudo in Terminal, e.g.:

      sudo killall cfprefsd
      
    • If you do not do this, the edited file may/will be overwriten by its original copy in memory, thus making the changes null and void.

  • cfprefsd will reload on its own afterwards.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a apple.stackexchange
scroll top