JavaScript equivalent of performSelectorOnMainThread?

Hi,
I apologize for posting a JavaScript question here, but it seems to be the best place I found.

The ObcJ definition of performSelectorOnMainThread is

  • (void)performSelectorOnMainThread:(SEL)aSelector
    withObject:(id)arg
    waitUntilDone:(BOOL)wait;

so in JXA, I’d have to call it like this

performSelectorOnMainThreadWithObjectWaitUntilDone(selector, id, wait)

In AppleScript, one passes “methodName:” as the first parameter. Does anybody know what the equivalent would be in JavaScript?
I tried “methodName:”, methodName, $.NSSelectorFromString(“methodName”), but ScriptEditor, osascript etc. keep complaining about “Incomprehensible message”.
Also, I’m calling the method on the current application, because using $.performSelector… throws an error – maybe that is wrong, too?

Thanks in advance for any clarification or pointers

Are you already calling Obj-C methods as outlined here?
https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/Using-Objective-C-(ObjC)-with-JXA

Just follow the same formatting.
Your gonna have to test creating the selector via
NSSelectorFromString:

And to make sure that your wanting to call this on
An Objective-C class or NSObject subclass.

If your wanting to call this on your own JavaScript function
Your asking in the wrong place.

If your wanting to call your JavaScript function from Obj-C / ASObj-C that’s
a different senario. There examples here, search for them.

When I needed this I ended up having my JavaScript code call back a
Call to currentApplication and hadn’t a method javascriptDone and included
The processed data.

The other option you could use is using. NSNotificationCenter
And post a notification with a Name and userInfo Dict

Here’s how to call JS from ASObj-C
And small example how to call the other way.
Calling the other way is based on an AppleScript app.
So when you ask for currentApplication you get the mainScipt

https://macscripter.net/viewtopic.php?id=46580

Thanks for your answer.

Yes, I’m running that in the context of an JXA-ObjC script and I followed the naming convention outlined in the JXA cookbook. Short example:


ObjC.import(‘Foundation’);
ObjC.import(‘AppKit’);
ObjC.import(‘Carbon’);

const app = Application.currentApplication();
app.includeStandardAdditions = true;

function foo(para) {
return true;
}

const id = 1;
app.performSelectorOnMainThreadWithObjectWaitUntilDone(foo, id, true);

gives me “message incomprehensible (-1708)”
same with “foo” and $.NSSelectorFromString(“foo”). I guess that neither of these work because the Selector concept does not carry over to JXA. $.NSSelectorFromString just returns the same string fed to it.

I don’t quite understand what you mean by this. The original AppleScript code I took this from used “my performSelectorOnMainThread…” , which (according to my research) should refer to the current application. That’s why I was calling it on app here.

As I asked your trying to execute a OBJ-C function in a JS environment.

Who is the reciever of the performSelector method.
It’s the current App.
Does your current App have the Foo method?

You JS is NOT your current app.
I don’t know how your call “self” in JS ?
You could try that but I doubt it will work as it’s not NSObject

You need to ask JavaScript folks how to do this.

Also you haven’t defined the id object your passing in the function either.

Re the Obj-C object here is an example where I’m calling
PerformSelector on a Class Instance Object.

https://macscripter.net/viewtopic.php?id=46910

Thanks, I think I got it.

There is a this object in JS, but what that refers to depends very much on context. It seems that there’s no way to do what I want.

So be it.

Well the first question I’ll ask is Why do you need this?

I’m pretty sure most Of JSX stuff
Is happening on the main thread.

Why is the purpose of the wait option?

  1. you could have a return value even if it’s a Boolean
    IE theResult = foo(bar);
    Once you have the result returned you know the function
    Competed.

  2. if there is more processing going one.
    Include 2 more functions.

fooCheck {
…do your testing here.
If complete call fooDone()
}

fooDone {
…do what
}

I was trying to re-write the Dialog Toolkit Plus in JS. The original AppleScript code uses performSelectorOnMainThread and I assumed that it is necessary in JS, too. Maybe not.

Thanks for pointing that out. I actually managed to get the AS-ObjC code (kind of) translated to JXA-ObjC, but running it gives me an error apparently coming from deep down in ObjC.

So I probably just scrap it. Some parts of JXA are simply too broken to be usable.