XMLRPC call to NodeJS to use xml2json function

In this example I like to use XMLRPC call to NodeJS and to use xml2json.

So I made a example. The parameter is the XMLString

set XMLString to "<?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><double>4.2</double></value>
</param>
</params>
</methodResponse>"

tell methodCall
	return (its methodName:"xml2json" params:{XMLString})
end tell

script methodCall
	on methodName:methodName params:params
		tell application "http://localhost:9090"
			call xmlrpc {method name:methodName, parameters:params}
		end tell
	end methodName:params:
end script

Here is XMLRPC server to be run with NodeJS

var xmlrpc = require('xmlrpc')

// Creates an XML-RPC server to listen to XML-RPC method calls
var server = xmlrpc.createServer({ host: 'localhost', port: 9090 });
console.log('XML-RPC server listening on port 9090');

// Handle methods not found
server.on('NotFound', function(method, params) {
    console.log('Method ' + method + ' does not exist');
})

server.on('xml2json', function(err, params, callback) {
	var parser = require('xml2json');
	var xml = params[0];
	var result = parser.toJson(xml);
	callback(null, result);
	return result
})

server.on('myObject', function(err, params, callback) {
    const myObject = {
        a: 'someString',
		b: 42,
		c: false
    };
    var result = Object.values(myObject);
	console.log(typeof result);
	return typeof result
})

The response from the XMLRPC call is:
“{"methodResponse":{"params":{"param":{"value":{"double":"4.2"}}}}}”

I’d like to understand what the purpose of this code is, given that XML has nowadays fallen very much out of use on the Web – what exactly is this XMLRPC server supposed to do in real life? And is this JSON not awfully complicated (aside: why is it using params to indicate several values, but then not an Array, but an Object to contain the values)?

Also, I’d suggest that you use const instead of var in your JavaScript if the values can’t change. That allows for easier error catching.

I will do my best to explain.

  1. XMLRPC is an remote procedure call protocol. It allow a user to send request to a server to return a response. The parameters is inputs to some method that the server use to return the response back to the caller.

  2. You could execute event-driven functions in other language (Python, JavaScript…)

  3. Why use XMRPC call, its build-in to AppleScript, you only need to setup a server with functions.

  4. User who do not know how to build framework to make xml2json parser. Or find it complicated to use other approaches. Could use a module from NodeJS that do everything for you.

  5. You are able to use modules from PyPi and NPM with your AppleScript code in a fast approach.

  6. If its easier to use python or javascript, why not use it and do less in AppleScript.

XMLRPC call is not so much different and if you call other AppleScript with run script with parameters the paremeters is your input and the script is your method.

XMLRPC call could also make prototype coding very fast if your main code is AppleScriipt.

If the hope is that Apple will exchange AppleScript, why not do it yourself I do.

Its also very common for application that use low level language use high level language for scripting. In other words it doesn’t compile code to machine code. Its easier to update only the part of the code it effect.

But the most important thing I think.
Users do not need to be full dedicated developer to do amazing things quickly.

Exactly. Why user Applescript at all in this context?

Exchange for what? I doubt that they’ll do anything about AS – neither amend it (no interest) nor sunset it (too much code). Personally, I don’t touch AS with a ten foot pole. Though JXA is even buggier, at least JavaScript is a grown-up language.

Who’s the user here? The developer needs to speak at least AS and JS/Python.

Exactly. Why user Applescript at all in this context?

They maybe like to select a file in finder, get its contents and convert it from xml to json.
Many people like to have a shortcuts to everything and many people feel comfortable with that approach.

Exchange for what?

You could run JavaScript with your AppleScript, and you do not need to use JXA.

Who’s the user here? The developer needs to speak at least AS and JS/Python.

Would you not agree 2023 its faster to learn, to do more things in less time and it was 10 years ago. And there are many reason for that… but I do not think people are more smarter today and they where 10 years ago.