engines
engines
Stability: 2 - Stable
The engines module contains a number of functions related to the scripting environment, script running, and scripting engine, including running other scripts, closing scripts, etc.
For example, to get the directory where the script is located.
toast(engines.myEngine().cwd());
engines.execScript(name, script[, config])
- ``name` {string} The name of the script to run. This name has nothing to do with the name of the file, it is just the name displayed in the task manager.
script
{string} The content of the script to be run.config
{Object} The configuration item to run.delay
{number} The number of milliseconds to delay execution, default is 0loopTimes
{number} The number of times to run the loop, default is 1. 0 is an infinite loop.interval
{number} The time interval between runs of the loop, default is 0path
{Array} | {string} Specifies the directory where the script will run. These paths will be used to find module files when requiring.
Runs the script script in a new scripting environment. returns a ScriptExecution object.
The new script environment specifies that the variables in the script are not shared with those of the original script, and that the script will be run in a new thread.
The simplest example is as follows.
engines.execScript("hello world", "toast('hello world')");
If you want to run it in a loop, then.
//run the script every 3 seconds, loop 10 times
engines.execScript("hello world", "toast('hello world')", {
loopTimes: 10,
interval: 3000
});
Scripting with strings is very inconvenient and can be combined with the ``Function.toString()` method to execute a specific function:
function helloWorld(){
// Note that the variables here are not shared with the variables in the body of the script
toast("hello world");
}
engines.execScript("hello world", "helloWorld();\n" + helloWorld.toString());
If variables are to be passed, then these can be wrapped into a function at
function exec(action, args){
args = args || {};
engines.execScript(action.name, action.name + "(" + JSON.stringify(args) + ");\n" + action.toString());
}
// The function to be executed, is a simple addition
function add(args){
toast(args.a + args.b);
}
//execute 1 + 2 in a new script environment
exec(add, {a: 1, b:2});
engines.execScriptFile(path[, config])
path
{string} The path of the script to run.config
{Object} The configuration item to run.delay
{number} The number of milliseconds to delay execution, default is 0loopTimes
{number} The number of times to run the loop, default is 1. 0 is an infinite loop.interval
{number} The time interval between runs of the loop, default is 0path
{Array} | {string} Specifies the directory where the script will run. These paths will be used to find module files when requiring.
Runs the script file path in a new scripting environment. returns a ScriptExecution object.
engines.execScriptFile("/sdcard/script/1.js");
engines.execAutoFile(path[, config])
path
{string} The path to the recording file to run.config
{Object} Run configuration itemdelay
{number} The number of milliseconds to delay the execution, default is 0.loopTimes
{number} The number of loops to run, default is 1. 0 is an infinite loop.interval
{number} The time interval between runs of the loop, default is 0path
{Array} | {string} Specifies the directory where the script will run. These paths will be used to find module files when requiring.
Runs the recording file path in a new scripting environment. returns a ScriptExecution object.
engines.execAutoFile("/sdcard/script/1.auto");
engines.stopAll()
Stops all running scripts. Including the current script itself.
engines.stopAllAndToast()
Stops all running scripts and shows the number of stopped scripts. Include the current script itself.
engines.myEngine()
Returns the script engine object of the current script (ScriptEngine)
[new in v4.1.0] In particular, the object can be used to get his runtime parameters, including external parameters, intent, etc. via execArgv
. For example.
log(engines.myEngine().execArgv);
Normal scripts usually run with empty parameters, while those started by broadcast of timed tasks can get the start intent.
engines.all()
- Returns {Array}
Returns an array of all currently running script engines ScriptEngine.
log(engines.all());
ScriptExecution
The object returned when executing a script, through which you can get the execution engine, configuration, etc., or you can stop this execution.
To stop the execution of this script, use ``execution.getEngine().forceStop()`.
ScriptExecution.getEngine()
Returns the script engine object (ScriptEngine) that executes the script.
ScriptExecution.getConfig()
Returns the script's runtime configuration (ScriptConfig)
ScriptEngine
Script engine object.
ScriptEngine.forceStop()
Stops the execution of the script engine.
ScriptEngine.cwd()
- Return {string}
Returns the path of the script execution. For a script file, it is the folder where this script is located; for other scripts, such as string scripts, it is null
or the value set at execution.
ScriptEngine.getSource()
- Returns ScriptSource
Returns the script object currently being executed by the script engine.
log(engines.myEngine().getSource());
ScriptEngine.exit(eventName[, . .args])
eventName
{string} event name... .args
{any} event parameters
Sends an event to the script engine that can be listened to in the events module of the corresponding script and perform event handling in the script's main thread.
For example, the script receiver.js would read as follows.
//Listen to the say event
events.on("say", function(words){
toastLog(words);
});
//Keep the script running
setInterval(()=>{}, 1000);
Another script in the same directory can start him and send this event.
//run the script
var e = engines.execScriptFile(". /receiver.js");
//wait for the script to start
sleep(2000);
//Send events to the script
e.getEngine().exit("say", "Hello");
ScriptConfig
The configuration of the script during execution.
delay
- {number}
The number of milliseconds to delay execution
interval
- {number}
The time interval between runs of the loop
loopTimes
- {number}
The number of times the loop will run
getPath()
- Returns {Array}
Returns a string array representing the path the module looks for when the script is run.