Wilcox Development Solutions Blog

AppleScript XML-RPC Bug (and workaround!) Found

April 08, 2005

Today I found a fun bug in AppleScript’s XML-RPC client support.

Read on to find out what I discovered when you try to handle errors from an XML-RPC script. I also present a workaround, which is both obvious and not obvious.

Follow me through a little journey, told in Applescript.

Simple Call

First, let’s see a XML-RPC script, which will return an error. We don’t catch the error and so it stops the script

`on workCall() tell application ”http://betty.userland.com:80/RPC2” call xmlrpc {method name:“examples.getStateListss”, parameters:{1}} end tell end workCall

workCall()`

The Error

Now, obviously this doesn’t work for certain production scripts (like those that work without user intervention). The typical answer is put code that might fail into a try / on error block like so:

`on bugCall() tell application ”http://betty.userland.com:80/RPC2” try call xmlrpc {method name:“examples.getStateListss”, parameters:{1}} on error errMess number errNum log errMess log errNum end try end tell end bugCall

bugCall()`

However, this gives you a parameter error when you try this. This is very odd, annoying, and yes, I have filed a bug on it (Apple employees can find the full bug report at Radar #4083408.)

The Workaround

So I was talking with Matthew Strange today about the bug, and he suggested restructuring the script so it looked like this:

`on workAround() try tell application ”http://betty.userland.com:80/RPC2” call xmlrpc {method name:“examples.getStateListss”, parameters:{1}} end tell on error errMsg number errNum log errMsg log errNum end try end workAround

workAround()`

This kind of makes sense, because you tend to get scope errors in Applescript sometimes. Something like the following might error out:

`to doIt() beep end

on run() tell application “myApp” doIt() end tell end run`

This script, or something similar, may error out with a message stating that “MyApp doesn’t know how to handle doIt()“. You see, Applescript gets kind of confused here and thinks that it should tell myApp to call this function. Of course, myApp has no idea what to do - this function is in the script itself. Perhaps something similar is happening here with the error code information.

Conclusion

This bug has distracted me for at least half a day, probably more. Trying to determine who’s to blame, server or client. When I saw this error happen with Userland’s servers (the creators of XML-RPC), as well as my own server, I knew it was a client issue.

I tried an experiment in Cocoa with the WebServices.framework level, which worked fine. I was all ready to spend a developer incident to track this bug down when I discovered the workaround.

So there’s All You Ever wanted to know about using Applescript’s XML-RPC client interface, and handling errors (“faults”) returned by the server.


Written by Ryan Wilcox Chief Developer, Wilcox Development Solutions... and other things