Friday, January 28, 2022

#1 2021-11-12 02:55:07 am

chrillek
Member
From:: Berlin
Registered: 2021-11-09
Posts: 5
Website

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

Offline

 

#2 2021-11-13 12:31:49 am

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: JavaScript equivalent of performSelectorOnMainThread?

Are you already calling Obj-C methods as outlined here?
https://github.com/JXA-Cookbook/JXA-Coo … 9-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

Offline

 

#3 2021-11-13 12:40:20 am

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: JavaScript equivalent of performSelectorOnMainThread?

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

Offline

 

#4 2021-11-13 04:35:52 am

chrillek
Member
From:: Berlin
Registered: 2021-11-09
Posts: 5
Website

Re: JavaScript equivalent of performSelectorOnMainThread?

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.

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


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.

Offline

 

#5 2021-11-13 03:30:48 pm

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: JavaScript equivalent of performSelectorOnMainThread?

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.

Offline

 

#6 2021-11-13 03:33:03 pm

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: JavaScript equivalent of performSelectorOnMainThread?

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

Offline

 

#7 2021-11-13 11:57:21 pm

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: JavaScript equivalent of performSelectorOnMainThread?

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

Offline

 

#8 2021-11-14 04:24:01 am

chrillek
Member
From:: Berlin
Registered: 2021-11-09
Posts: 5
Website

Re: JavaScript equivalent of performSelectorOnMainThread?

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.

Offline

 

#9 2021-11-14 03:11:52 pm

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: JavaScript equivalent of performSelectorOnMainThread?

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
}

Offline

 

#10 2021-11-15 09:38:06 am

chrillek
Member
From:: Berlin
Registered: 2021-11-09
Posts: 5
Website

Re: JavaScript equivalent of performSelectorOnMainThread?

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.

Offline

 

#11 2021-11-15 01:04:03 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

Re: JavaScript equivalent of performSelectorOnMainThread?

@chrillek

Maybe you are looking for something like this:
https://github.com/parmanoir/jscocoa

I have not test myself and it has not been maintained for long time.


if you are the expert, who will you call if its not your imagination.

Offline

 

#12 2021-11-16 03:19:08 am

chrillek
Member
From:: Berlin
Registered: 2021-11-09
Posts: 5
Website

Re: JavaScript equivalent of performSelectorOnMainThread?

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)