Given I run this command:
$ defaults write com.example.encoding room -string "Baño"
plutil
shows that it is properly stored in UTF-8, and the character is correct:
$ plutil -convert xml1 ~/Library/Preferences/com.example.encoding.plist -o -
<?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>room</key>
<string>Baño</string>
</dict>
</plist>
I can also get this value from PlistBuddy
:
$ /usr/libexec/PlistBuddy -c "Print :room" ~/Library/Preferences/com.example.encoding.plist
Baño
However - if I try using defaults read
, the value comes back garbled - the special character (ñ
) does not get returned correctly:
$ defaults read com.example.encoding room
Ba\361o
Is there some way to set the encoding that defaults read
uses?
Is there some way to set the encoding that defaults read uses?
No. This isn’t really a text encoding issue — defaults
does the right thing at that level — but rather an escaping issue. defaults read
processes the incoming string as UTF-16 and then escapes as follows:
-
For ASCII printables, there’s no escaping.
-
If the value is below U+0100 it uses
\ooo
, whereooo
is on octal sequence. -
Otherwise it prints,
\uhhhh
, wherehhhh
is a hex sequence.
You can see the last point in action here:
% defaults write com.example.encoding room -string "Baño😀"
% defaults read com.example.encoding room
Ba\361o\ud83d\ude00
The emoji here is U+1F600 GRINNING FACE, whose UTF-16 surrogate pair is d83d de00.
Some more digging reveals that:
-
This encoding is what you get when you call
data(using:allowLossyConversion:)
withString.Encoding.nonLossyASCII
-
There’s no way to override this in the
defaults
command.
If you’d like this to change, I encourage you to file an enhancement request against that command. Please post your bug number, just for the record.
There’s a couple of ways you might work around this but it kinda depends on your situation. Can you explain more about your intended workflow here?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"