I didn’t find any info in Macscripter but sharing Shane’s code from LNS forum:
https://forum.latenightsw.com/t/is-it-possible-to-call-javascript-from-applescript/291/3
I found this very handy
the ability to call Java Script functions from 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:
I also figured out how to call functions in my Applescript App from Java using:
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 :
-[__NSDictionaryM localizedDescription]: unrecognized selector sent to instance 0x6000016500b0
any solutions to this? I’ve tried so many different things.
thanks
Have you added the JavaScriptCore framework to your project?
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 .
I got the same error… but this key maybe work for you OSAScriptErrorMessageKey
use framework "Foundation"
use framework "OSAKit"
use scripting additions
set thePath to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
set theScript to current application's OSAScript's alloc()'s initWithContentsOfURL:theURL |error|:(missing value)
set {theResult, theError} to (theScript's executeAndReturnError:(reference)) as list
-- log theError's allKeys() as list
(**
* OSAScriptErrorAppAddressKey, OSAScriptErrorAppNameKey, OSAScriptErrorExpectedTypeKey,
* OSAScriptErrorRangeKey, OSAScriptErrorNumberKey, NSLocalizedDescription,
* NSLocalizedFailureReason, OSAScriptErrorBriefMessageKey, OSAScriptErrorOffendingObjectKey,
* OSAScriptErrorMessageKey
*)
if theResult = missing value then error (theError's valueForKeyPath:"OSAScriptErrorMessageKey") as string
theResult as anything