How to replace a value in an plist array using plutil

command lineplist

I'm trying to change a value in an array using plutil, but I get the error

Failed to insert value new value 2 at key path PARENT.0.KEY_IN_ARRAY with error -[__NSCFConstantString characterAtIndex:]: Range or index out of bounds

Here's a sample plist to illustrate the problem:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>SIMPLE</key>
  <string>value1</string>
  <key>PARENT</key>
  <array>
    <dict>
      <key>KEY_IN_ARRAY</key>
      <string>value2</string>
    </dict>
    <dict>
      <key>KEY_IN_ARRAY</key>
      <string>value3</string>
    </dict>
  </array>
</dict>
</plist>

Modifying the SIMPLE value is easy:

$ plutil -extract SIMPLE xml1 -o - sample.plist 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>value1</string>
</plist>

$ plutil -replace SIMPLE -string "new value 1" sample.plist 

$ plutil -extract SIMPLE xml1 -o - sample.plist 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>new value 1</string>
</plist>

Getting the array value works fine:

$ plutil -extract PARENT.0.KEY_IN_ARRAY xml1 -o - sample.plist 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>value2</string>
</plist>

but I get the error when I try to modify this value:

$ plutil -replace PARENT.0.KEY_IN_ARRAY -string "new value 2" sample.plist 
sample.plist: Could not modify plist, error: Failed to insert value new 
value 2 at key path PARENT.0.KEY_IN_ARRAY with error -[__NSCFConstantString 
characterAtIndex:]: Range or index out of bounds

Best Answer

I think PlistBuddy is the recommended tool here:

/usr/libexec/PlistBuddy -c "Set :PARENT:0:KEY_IN_ARRAY valueX" sample.plist 

The command modifies the value of KEY_IN_ARRAY in the first dict of PARENT. The one in the second dict would be changed with:

/usr/libexec/PlistBuddy -c "Set :PARENT:1:KEY_IN_ARRAY valueY" sample.plist 

The -c switch executes the command directly. The file mustn't be a binary plist!