1

I'm tying to create a plist using "defaults write", unfortunately I can't use plutil or any other utility.

HEX data: 62706c69 73743030 d4010203

This is the plist I need to create

<?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>Architectures for arm64</key>
    <dict>
        <key>org.my.test</key>
        <array>
            <data>
            YnBsaXN0MDDUAQID
            </data>
            <string>x86_64</string>
        </array>
    </dict>
</dict>
</plist>

I'm using this syntax:

defaults write ~/Desktop/org.my.test '{"Architectures for arm64" = {org.my.test = (-data <62706c69 73743030 d4010203>, "x86_64");};}'

and I'm getting the error: 2021-06-26 09:21:09.280 defaults[27260:1348922] Could not parse: {"Architectures for arm64" = {org.my.test = (-data <62706c69 73743030 d4010203>, "x86_64");};}. Try single-quoting it.

I can't figure out where should I put the single-quoting

can anyone please help me?

2
  • Can't you just create the XML structure directly (it's all text after all)?
    – nohillside
    Commented Jun 26, 2021 at 8:30
  • No because I need to add information the the already existed plist
    – Dimitrios
    Commented Jun 26, 2021 at 8:31

3 Answers 3

2

-data is a parameter to defaults when writing a single data value.

You're writing an entire property list in one go, so don't include this argument as part of the property list string. Remove -data from your command and it works fine writing the entire string as the contents of the property list file.

When writing a full property list, data is encoded as hex in angle brackets as you have done.

0

You could also use defaults import.

That allows you to specify the data as the very xml text that you're showing in your question. The inverse command, for seeing the results, is defaults export -.

-1

If you look through the man page for defaults, it has options for creating dictionaries and arrays, and adding elements to existing arrays and key-pairs to existing dictionaries.

  -array      Allows the user to specify an array as the value for the given preference key:

                       defaults write somedomain preferenceKey -array element1 element2 element3

                 The specified array overwrites the value of the key if the key was present at the time of the write. If
                 the key was not present, it is created with the new value.

     -array-add  Allows the user to add new elements to the end of an array for a key which has an array as its value.
                 Usage is the same as -array above. If the key was not present, it is created with the specified array as
                 its value.

     -dict       Allows the user to add a dictionary to the defaults database for a domain.  Keys and values are speci-
                 fied in order:

                       defaults write somedomain preferenceKey -dict key1 value1 key2 value2

                 The specified dictionary overwrites the value of the key if the key was present at the time of the
                 write. If the key was not present, it is created with the new value.

     -dict-add   Allows the user to add new key/value pairs to a dictionary for a key which has a dictionary as its
                 value. Usage is the same as -dict above. If the key was not present, it is created with the specified
                 dictionary as its value.

A structural approach may work better than trying to cram it all in with punctuation.

1
  • 1
    I understand your point of using specific options to default to write each value, but so far this answer doesn't explain how to use these options to write data, as given in the question. OP can successfully write arrays and dictionaries already using their method. Breaking down the plist from the question, OP needs to write an array, so from your answer I can take away that -array/-array-add are the right options, but how can one add some data? The man page snippet only says element1. Perhaps this answer could explain what element1 to use to add data to an array with those options?
    – grg
    Commented Jun 26, 2021 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .