MacOS – Modifying .plist with shell changes .plist order

command linemacosplistterminal

I wrote a simple shell command and saved it as .command. The shell command is simply: defaults write /Users/Milo/Desktop/MyPlist "My Key" "My Entry". The issue that I'm having is that after running that command, it messes with the order of the plist file. Normally this wouldn't be a problem, except that I wrote an application that parses the plist, and it does so according to the order of the keys in the file. I don't want to change my app just to accommodate a seemingly random ordered plist that gets returned after I run the command. How can I make the command not reorder my plist?

Best Answer

The order of key value pairs within a Property List, plist, dictionary structure is not specified and can not be relied upon.

Ideally, your application needs to parse the property list without relying on the order of the dictionary pairs.

If the order is critical to your application, consider changing the property list structure to an array of dictionaries; where each dictionary contains a single key value pair.

If you can not change your application, and the property list is formatted as XML, then you may want to investigate other command line XML manipulation tools.