Wednesday, November 20, 2019

#1 2018-11-11 09:24:55 am

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

Using Javascript as Library - Shane's Code

I didn't find any info in Macscripter but sharing Shane's code from LNS forum:
https://forum.latenightsw.com/t/is-it-p … ript/291/3

I found this very handy
the ability to call Java Script functions from Applescript:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "OSAKit"
use scripting additions
property myJSLib : missing value

try
   script "JSTest.scpt"
on error from jsLib
   set jsLibPath to POSIX path of (jsLib as alias)
   set {my myJSLib, theError} to current application's OSAScript's alloc()'s initWithContentsOfURL:(current application's |NSURL|'s fileURLWithPath:jsLibPath) |error|:(reference)
   if myJSLib is missing value then error (theError's localizedDescription()) as text
end try

my callFunction:"doIt" withArgs:{2, 2} inJSLib:myJSLib
--> 4

on callFunction:funcName withArgs:argsList inJSLib:myJSLib
   set {theResult, theError} to myJSLib's executeHandlerWithName:funcName arguments:argsList |error|:(reference)
   if theResult is missing value then error (theError's localizedDescription()) as text
   -- hack to convert from descriptor to correct AS class
   return item 1 of ((current application's NSArray's arrayWithObject:theResult) as list)
end callFunction:withArgs:inJSLib:


Filed under: Library, Script, Java, Osa

Offline

 

#2 2018-11-11 09:30:10 am

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

Re: Using Javascript as Library - Shane's Code

I also figured out how to call functions in my Applescript App from Java using:

Applescript:


app = Application.currentApplication();
app.myASFunction(myJSVariables);

which worked awesome in my App from Script Debugger.

But when I moved everything into Xcode.

The function call from Javascript fails: with :

Applescript:

-[__NSDictionaryM localizedDescription]: unrecognized selector sent to instance 0x6000016500b0

any solutions to this?  I've tried so many different things.

thanks

Offline

 

#3 2018-11-11 04:52:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6036

Re: Using Javascript as Library - Shane's Code

Have you added the JavaScriptCore framework to your project?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#4 2018-11-11 06:39:42 pm

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

Re: Using Javascript as Library - Shane's Code

No I did not!

I figured that OSAKit would handle that?!?
Call to JavaScript functions are working fine.

I did notice that when I inspected open files in active monitor that
Javacore framework was being loaded.

I'm wondering if it has to do with multi threading now happening?
Or more than one JVM?

How can I call back from java using the
Perform selector on main thread?

Next thing I'm going to try is to see if I can post
A notification from JavaScript.
I briefly understand how to utilize:
$.NS______  in JavaScript .

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)