Problem using "do shell script" with defaults

I am trying to get the defaults command to take a value for a key that contains a square bracket, and each time I do it returns a parse error.
As entered in the terminal, the command appears:

defaults write test.plist theString [

Does anyone know if this is a known bug or what I am doing incorrectly?

Firstly, thanks to have point this very cool feature, I hadn’t saw.

I’m not sure what you mean but in case, in fact when in the man there is [ stuff ] in the syntax description of a command, this means that stuff is optional not that you have to use [].

Check further in the man, as often you have some example :
defaults write com.companyname.appname ‘{ “Default Color” = (255, 0, 0); “Default Font” = Helvetica; }’

and for read :
defaults read com.companyname.appname “Default Font”

That works fine and it’s top cool.

In applescript the brutal :
do shell script "defaults write com.companyname.appname.as ‘{ “Default Color” = (255, 0, 0); “Default Font” = Helvetica; }’ "
log (do shell script “defaults read com.companyname.appname.as “Default Font””)

Works fine too. Now there are many options for defaults that could be cool to use. Quote that the command “quoted form your string” will put single quotes arround you string, this could perhaps help.

Yes, but the strings I am trying to store happen to have [ in them and I cannot get it to work. (I did read the man page and perform a couple of google searches, but have turned up nothing about why the command I am trying doesn’t work.)

The trick that I happened to be missing is that value strings containing a [ must be wrapped in double quotes, and then within single quotes. I think the double quotes prevent the shell from interpreting the [. (Vice versa does not work…the value ends up being stored with single quotes attached. And having this problem reminds me of when I used to do much more unix shell scripting…and I had a variable that was wrapped in single, double, and backquotes once. Completely crazy.)

Ha ok then try that, seems work fine :
defaults write com.companyname.appname ‘{ “Default Color” = (255, 0, 0); “Default Font” = “[Helvetica]”; }’

and
read com.companyname.appname “Default Font”
returns
[Helvetica]

I hope it’s what you wanted. That single quoting usage is very non common for a unix command, at least for the few I know.

Yes, that will work fine, but since you included the braces, it will replace the entire plist file with only what you have included.

ok :-). Anyway it’s the same principle.

Single quote are required by the command by its syntax (that seems VERY special, normally quotes are out of the syntax to let the user manage himself values - single quote forbidde any expension), it will apply its own parsing inside it. But inside it it let you a last chance to quote something to take it as it is, it’s the double-quote and it’s usefull only for special values (to see if special keys are allowed).

So to set only one value, it’s very similar :
write com.companyname.appname “Default Font” ‘“[Helvetica2]”’
defaults read com.companyname.appname “Default Font”
returns :
[Helvetica2]

I’m not enough experimented but I feel they became crazy and wanted simplify the user task but made too uncommon choices. Ha well once you get it it seems ok afterall.