Benches macOS

2

defaults write -array

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

hello,

i'm having some trouble writing a complex array using defaults. here is an example you can try to see what i'm referring to:


defaults read com.apple.dock persistent-apps

i just used this example because i think everyone will have it and to avoid pasting a lot of code. if you run this you will see it's quite a long and nested array. what i'm trying to do is to read from my pref file and then go through each item in the array keeping what i want and then write back to it. my question is if i try to write back a long and nested array to my pref file how should i quote it or what kind of escape characters do i maybe need to use? i'm actually doing this in cocoa which is just a unix wrapper in this case and i can read my file fine, but writing back i can never get it to go through. the read results in cocoa are exactly like in terminal starting and ending with ( & { and i'm thinking something is missing when i go to write back an array. hope that makes sense and thank you! 🙂

rick

note: i'm aware the code to write is like below. it's just the last part giving me trouble.


defaults write com.mycompany.myfile arrayname -array ???

2 replies

Sign in or join Beachball Help to reply.

  1. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    MacOSXHints covered something similar a while back in
    this thread.

    The upshot was that you should use single quotes around the entire value, double-quotes around literal strings, and parentheses to indicate list items, e.g.:

    defaults write com.apple.dock persistent-others -array-add '{ "tile-data" = { "list-type" = 1; }; "tile-type" = "recents-tile"; }'

    "

  2. 0

    rick722

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks camelot and glad you could get what i was saying. 🙂 that does help and i'll see if i can get it to work. the results i get by using the cocoa wrapper look just like the results from terminal and i can hold it in an array. the tricky part is after i edit the array the way i want is to write it back. i'll follow your suggestion and see if it works...i hope so!

    thanks again,

    rick