LNS Home
Sample XML-RPC Server
SampleServer Home
Products
x Script Debugger 4.5
Site Contents
bullet Mark’s Blog
Product Registration
Bug Reporting
x Freeware
Contacting Us

Sample XML-RPC Server

Download version 1.0 (544k)

I feel that one of the most significant new scripting features of Mac OS X 10.1 is its native support for the XML-RPC and SOAP protocols. XML-RPC allows you to invoke code running in any virtually and programming language and operating system. To try and illustrate how cool this is, I've put together a simple XML-RPC server written in a few lines of Java that you can run on Mac OS X, Windows or Unix and which you can invoke using AppleScript.

All source code is included, so you can extend the server to do something useful if you have some Java knowledge.

How It Works

Step 1: the launch the server.

One Mac OS X, this simply involves double-clicking SampleServer.app. On a Windows system with a Java run-time installed, you need to enter the following command into a command window:

java -jar SampleServer.jar

Step 2: open one of the client scripts and run it.

Sample
script test
   
   property mServerAddress: ""
   
   --
   --   Create an AppleScript object that looks as much as possible like the
   --   Exposed class in the Sample XML-RPC Server
   --

   
   on test()
      using terms from application "http://localhost/"
         tell application("http://" & mServerAddress & ":8081/")
            return call xmlrpc {method name:"test.test"}
         end  tell
      end using terms from
   end test
   
   on getName()
      using terms from application "http://localhost/"
         tell application("http://" & mServerAddress & ":8081/")
            returncall xmlrpc {method name:"test.getName", parameters:{true}}
         end  tell
      end  using terms from
   end getName
   
   on concatinate(s1, s2)
      using terms from application "http://localhost/"
         tell application("http://" & mServerAddress & ":8081/")
            return call xmlrpc {method name:"test.concatinate", parameters:{s1, s2}}
         end  tell
      end  using terms from
   end
concatinate
end
  script

--   Figure out which machine we are talking to
set test's mServerAddress to text returned of ¬
   (display dialog "Server Address (e.g. www.somedomain.com or 192.168.1.100):" default answer "localhost")

--   Invoke some methods
{test's test(), test's getName(), test's concatinate("abc", "123")}

Step 3: the server receives the requests and processes them

The Java code that responds to these XML-RPC messages looks like this:

Sample
package com.latenightsw.SampleServer;

public class Exposed {
    // The public methods in this class are the ones which may be invoked via XML-RPC...
    public String test() {
        XMLRPCServer.Log("Exposed.test() called...\n");
        return "Hello World";
    }

    public String getName(boolean flag) {
        XMLRPCServer.Log("Exposed.getName(" + (flag ? "true" : "false") + ") called...\n");
        return "Mark Alldritt";
    }

    public String concatinate(String s1, String s2) {
        XMLRPCServer.Log("Exposed.concatinate(\"" + s1 + "\", \"" + s2 + "\") called...\n");
        return "**" + s1 + "**" + s2 + "**";
    }
}

Step 4: AppleScript receives the response


Copyright © 1998-2009 Late Night Software Ltd. - All Rights Reserved.