Saturday, August 8, 2020

#1 2020-07-24 03:15:08 pm

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

AS -> JS -> AS with JavaScriptCore Framework

I have been doing some simple JavaScript examples and not only in Safari.
This ones use JavaScriptCore Framework.

We could also send AS arguments to the JavaScript code, execute it... and return AS value.
Nice way to run simple JavaScript functions.

Applescript:

use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions

set theString to "heLLo world!"
set {theNumber, idx} to {"9.656", 2}

(**
* JS function upperCase(str)
*)

set upperCaseJS to "function upperCase(str) {
//
return str.toUpperCase()
}
//
upperCase('"
& theString & "')"

----------

(**
* JS function lowerCase(str)
*)

set lowerCaseJS to "function lowerCase(str) {
//
return str.toLowerCase()
}
//
lowerCase('"
& theString & "')"

----------

set titleCaseJS to "function titleCase(str) {
//
return str.replace(/\\b\\w/g, function (txt) {return txt.toUpperCase(); });
}
//
titleCase('"
& theString & "')"

----------

set theDegToRadJS to "function degToRad(degrees) {
//
return degrees * (Math.PI / 180);

}
//
50 * (degToRad(60))"


----------

set theToFixedJS to "function toFixedFunc(inputString, idx) {
var x = inputString;
return x.toFixed(idx);
}
//
toFixedFunc("
& theNumber & "," & idx & ")"

----------

set theEncodeURI to "function theFuncURI() {
const uri = 'https://mozilla.org/?x=шеллы';
const encoded = encodeURI(uri);
return encoded;
try {
return (decodeURI(encoded));
} catch (e) {
console.error(e);
}
       
}
// Run JS Script function
theFuncURI()"


----------

(**
* JavaScriptCore Framework
*)


set theJSContext to current application's JSContext's new()
-- set theEvaluateScript to ((theJSContext's evaluateScript:"Math.PI")'s toObject() as text)
set theEvaluateScript to ((theJSContext's evaluateScript:upperCaseJS)'s toObject() as text)
log its theEvaluateScript

set theJSContext to current application's JSContext's new()
set theEvaluateScript to ((theJSContext's evaluateScript:lowerCaseJS)'s toObject() as text)
log its theEvaluateScript

set theJSContext to current application's JSContext's new()
set theEvaluateScript to ((theJSContext's evaluateScript:titleCaseJS)'s toObject() as text)
log its theEvaluateScript

set theJSContext to current application's JSContext's new()
set theEvaluateScript to ((theJSContext's evaluateScript:theDegToRadJS)'s toObject() as text)
log its theEvaluateScript

set theJSContext to current application's JSContext's new()
set theEvaluateScript to ((theJSContext's evaluateScript:theToFixedJS)'s toObject() as text)
log its theEvaluateScript

set theJSContext to current application's JSContext's new()
set theEvaluateScript to ((theJSContext's evaluateScript:theEncodeURI)'s toObject() as text)
log its theEvaluateScript


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.


Filed under: JavaScriptCore

Offline

 

#2 2020-07-25 01:02:16 am

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

Re: AS -> JS -> AS with JavaScriptCore Framework

We could also doit like this.

Applescript:

use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions

-- The JS Math function follow JavaScript syntax.
set theMathFunc to {"Math.sin(Math.PI / 2)", "2 * Math.sin(Math.PI / 180)", "Math.cos(Math.PI / 180)"}

-- The JavaScript code to be piped to JavaScriptCore framework.
set theMathFuncJS to "function mathFunc() {
//
var x = "
& (item 1 of theMathFunc) & ";
var y = "
& (item 2 of theMathFunc) & ";
var z = "
& (item 3 of theMathFunc) & ";
return [x,y,z]
}
//
mathFunc()"


-- ASOC to execute and give the result back in AS value.
set theJSContext to current application's JSContext's new()
set theEvaluateScript to ((theJSContext's evaluateScript:theMathFuncJS)'s toObject() as list)
its theEvaluateScript


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)