Different ways to call Objective-C methods from AppleScript

With this topic I like to show different ways to access same Objective-C methods.
Incliuding: JavaScript, Python, XMLRPC call, AppleScriptObjC, do shell script, run script and command-line.

To run the below script you need to edit the path to your desktop, I didn’t use tilde.

use framework "Foundation"
use scripting additions

(********************************
* JavaScript to use Objective-C methods
*********************************
* function writeStringToFile(stringPath,stringText) {
* $.NSString.stringWithString(stringText).writeToFileAtomicallyEncodingError(stringPath,true,$.NSUTF8StringEncoding,null);
* }
* writeStringToFile("/Users/f.gustafsson.user/Desktop/someFileJS.txt","hello from JavaScript")
*
* The JavaScript above could be run directly in Script Editor by using JavaScript 1.1 language.
*)


(**
* Lets do the same things and use run script command. The script use a mix of AppleScript and JavaScript to call objective-c methods.
*)
set thePath to POSIX path of (path to desktop) & "someFileJS.txt"
set writeToFileJS to "function writeToFile(strPath, strText) {
// $.NSString.alloc.initWithUTF8String(strText).writeToFileAtomicallyEncodingError(strPath, false,$.NSUTF8StringEncoding, null);
$.NSString.stringWithString(strText).writeToFileAtomicallyEncodingError(strPath, false,$.NSUTF8StringEncoding, null);
}
writeToFile('" & thePath & "'," & "'" & "hello from JavaScript" & "'" & ")
"
run script writeToFileJS in "JavaScript"

(**********************************
* Python to use Objective-C methods
***********************************
* from Foundation import *
*
* theString = NSString.stringWithString_('hello from python')
* theString.writeToFile_atomically_encoding_error_('/Users/f.gustafsson.user/Desktop/someFilePyObjC.txt',True,NSUTF8StringEncoding,None) 
*
* From command line with module PyObjC we could run the script above.
*)

(**
* If we like to run the above script inside AppleScript to call objective-c method we could use do shell script.
*)
do shell script "/usr/bin/python3 <<EOF
from Foundation import *

theString = NSString.stringWithString_('hello from python')
theString.writeToFile_atomically_encoding_error_('/Users/f.gustafsson.user/Desktop/someFilePyObjC.txt',True,NSUTF8StringEncoding,None) 
EOF
"

(**
* Here we use XMLRPC call instead to do the same things as above scripts.
*
** The XMLRPC function in python using Twisted server.
* def xmlrpc_writeStringToFile(self, *args):
*        """
*        """
*        theString = NSString.stringWithString_(args[1])
*        theString.writeToFile_atomically_encoding_error_(args[0],True,NSUTF8StringEncoding,None)
*        return 0
*)
-- its methodCallWithMethodName:"writeStringToFile" withParameters:{"/Users/f.gustafsson.user/Desktop/someFileXMLRPC.txt", "hello from XMLRPC call"}
on methodCallWithMethodName:methodName withParameters:parameter
	tell application "http://localhost:7080"
		call xmlrpc {method name:methodName, parameters:parameter}
	end tell
end methodCallWithMethodName:withParameters:

(**
* Here we use the standard AppleScriptObjC call to objective-C method.
*)

-- writeStringToFile("/Users/f.gustafsson.user/Desktop/someFileAppleScriptObjC.txt", "hello from AppleScriptObjC")
on writeStringToFile(stringPath, stringText)
	set theString to current application's NSString's alloc()'s initWithString:stringText
	theString's writeToFile:stringPath atomically:true ¬
		encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
	return 0
end writeStringToFile