ClassList of ObjC JavaScript

I post a similar script for Python to get the ClassName of Objective-C bridge on Macscripter.net Here is a similar approach I find on stackoverflow for ObjC for JavaScript. It runs in Script Editor.
Maybe its useful to someone…

You need to run it as JavaScript…

ObjC.bindFunction('objc_getClassList', ['int', ['void**', 'int']])
ObjC.bindFunction('malloc', ['void**', ['int']])
ObjC.bindFunction('class_getName', ['char *', ['void*']])

const numClasses = $.objc_getClassList(undefined, 0)
const classes = $.malloc(8*numClasses)
$.objc_getClassList(classes, numClasses)
for (i=0; i<numClasses; i++) {
  console.log("classes[" + i + "]: " + $.class_getName(classes[i]))
}

What is the purpose? How to you use it?

This is copy of the comment from stackoverflow

The objc_getClassList function is expecting us to provide it with a buffer of memory to copy the class list into. Normally, JXA would treat the return type of malloc as a C array of unsigned chars, but using bindFunction we can cast malloc’s return type to a C array of pointers, and make objc_getClassList’s first argument match that type. Then, it’s just a matter of indexing into the buffer (of type Ref) and passing that value into class_getName.

You could also use bindFunction like this…

ObjC.import('Cocoa')

ObjC.bindFunction('sin',['double',['double']])
$.sin(1)

That will return: 0.8414709848078965

You could have access to a lot of low-level Apple system functions through the Objective-C object. And specially when the documentation of Objective-C bridge from JavaScript is very limited or portly executed.
JXA Objective-C bridge

ex. You could learn how to use bindFunction

And remember this is executed in Script Editor not Xcode and still we could use things that properly are not possible in AppleScript-ObjC bridge.

From my understanding you like to use JavaScript, Have you ever try to have access to Objective-C API with your code ??

And by using run script in “JavaScript”

You could do something like this to access Objective-C bridge
from AppleScript.

run script "ObjC.import('Cocoa');ObjC.bindFunction('sin',['double',['double']]);$.sin(1)" in "JavaScript"

To get you started if you have never done JavaScript ObjC bridge before.

First ASObjC

use framework "Foundation"

set str to current application's NSMutableString's alloc()'s init()
str's appendString:"hello world!"
str as string

Next JXA ObjC

ObjC.import('Cocoa')
str = $.NSMutableString.alloc.init
str.appendString('hello world!')
str.js

Or we could do this…

ObjC.import('Cocoa')
app = Application.currentApplication()
app.includeStandardAdditions = true

str = $.NSMutableString.alloc.init
str.appendString('hello world!')
str.js

app.displayDialog(str.js)

Did little more research, I believe JavaScript JXA 10.10 it was possible to build Cocoa application with Objective-C bridge. It looks like it broken at least my try didn’t work. Script Editor crash…

Importing the entire Cocoa framework is a bit overkill, and you don’t utilise anything from it that you don’t already have access to by not importing it, i.e. delete this line:

ObjC.import('Cocoa');