2718.us blog » rdat http://2718.us/blog Miscellaneous Technological Geekery Tue, 18 May 2010 02:42:55 +0000 en hourly 1 http://wordpress.org/?v=3.0.4 AppleScript’s XML-RPC Doesn’t Get Along with UTF8 http://2718.us/blog/2009/02/14/applescripts-xml-rpc-doesnt-get-along-with-utf8/ http://2718.us/blog/2009/02/14/applescripts-xml-rpc-doesnt-get-along-with-utf8/#comments Sun, 15 Feb 2009 00:53:02 +0000 2718.us http://2718.us/blog/?p=131 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:

  1. if class of theReturnedValue is "data" then
  2.  try
  3.   (* this will fail on a data object and then we will pull the (hex) bytes out as text
  4.   and bring them back as a utf8 string object *)
  5.   theReturnedValue as text
  6.  on error errmess – extract the data from the error message
  7.   set bytesString to text ((offset of "«" in errmess) + 10) thru ((offset of "»" in errmess) - 1) of errmess
  8.   set theReturnedValue to (run script "«data utf8" & bytesString & "»")
  9.  end try
  10. 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.

]]>
http://2718.us/blog/2009/02/14/applescripts-xml-rpc-doesnt-get-along-with-utf8/feed/ 1