While the ease of making XML-RPC calls in AppleScript is wonderful for, say, writing a LiveJournal Client in mostly AppleScript Studio, it seems to be doing something really messed up with UTF8 strings returned by the server—they come into AppleScript as raw data objects, which it seems can’t be cast into any other type and can’t be passed easily into a Cocoa method to convert them. The easiest way to properly decode them seems to be the following:
-
if class of theReturnedValue is "data" then
-
try
-
(* this will fail on a data object and then we will pull the (hex) bytes out as text
-
and bring them back as a utf8 string object *)
-
theReturnedValue as text
-
on error errmess – extract the data from the error message
-
set bytesString to text ((offset of "«" in errmess) + 10) thru ((offset of "»" in errmess) - 1) of errmess
-
set theReturnedValue to (run script "«data utf8" & bytesString & "»")
-
end try
-
end if
This checks the class of the returned value and, if it’s a raw data object, attempts to cast it as text which raises an error, then extracts the string of hexadecimal values from the error message and puts it into a proper UTF8 object, making everything happy again.
If anyone wants to tell me I’m wrong and there’s a simpler fix, I’d love to hear it, since this is essentially unworkable.
Trackbacks & Pingbacks 1
[...] might have been wrong, or at least not entirely right, when I said that Applescript’s XML-RPC was doing something screwy with UTF8-encoded responses to XML-RPC reques…. I’m not sure if it’s LiveJournal (and other sites based on their code), or if [...]