globals

October 22, 2022

globals

Global variables and functions are available in all modules. However, the following variables have scope only within the module, see module for details.

  • exports
  • module
  • require() The following objects are specific to CloudControl. Some built-in objects are part of the JavaScript language itself, and they are also global.

Some functions in modules can also be used globally directly for ease of use, and these functions are not described here. For example, the timers module has functions such as setInterval, setTimeout, etc.

sleep(n)

  • n {number} milliseconds

Suspend the running time for n milliseconds. 1 second is equal to 1000 milliseconds.

// pause for 5 seconds
sleep(5000);

setClip(text)

  • text {string} text

Sets the clipboard contents. This clipboard is the system clipboard, which can be used by "pasting" in the input box of the general application.

setClip("Clipboard text");

getClip()

  • Returns {string}

Returns the contents of the system clipboard.

toast("Clipboard content is:" + getClip());

toast(message)

  • message {string} The message to be displayed

Display the message message in a bubble for a few seconds. (The exact time depends on the Android system, usually it is 2 seconds)

Note that the display of the message is executed "asynchronously" and the program does not wait for the message to disappear before continuing. If the command is executed in a loop, there may be a situation where the script stops running and there is still a constant bubble message. For example:

for(var i = 0; i < 100; i++){
  toast(i);
}

After running this program, it will finish executing quickly and keep popping up messages, which cannot be stopped by closing all scripts in Task Manager. To ensure that the bubble message before continuing execution you can use.

for(var i = 0; i < 100; i++){
  toast(i);
  sleep(2000);
}

Or modify the toast function to.

var _toast_ = toast;
toast = function(message){
  _toast_(message);
  sleep(2000);
}
for(var i = 0; i < 100; i++){
  toast(i);
}

toastLog(message)

  • message {string} The message to be displayed

Equivalent to toast(message);log(message). The message message is displayed and output in the console. See console.log.

exit()

Stops the script from running immediately.

Stopping immediately is done by throwing ScriptInterruptedException, so if you use try.... .catch to catch the exception of the exit() function, the script will not stop immediately, but will still run for a few lines before stopping.

random(min, max)

  • min {number} lower bound of the interval to generate the random number
  • max {number} the upper bound of the interval generated by the random number
  • return {number}

Returns a random number between [min..... . max]. For example random(0, 2) may produce 0, 1, 2.

random()

  • return {number}

Returns a random floating point number in [0, 1).

requiresApi(api)

  • api Android version number

Indicates that this script requires the specified version of Android API to run. For example, requiresApi(19) means the script needs to run on Android 4.4 and above.

Calling this function will determine the version number of the device running the script, and throw an exception if it does not meet the requirement.

You can refer to the following comparison table of Android API and version:

Platform version: API level

Android 7.0: 24

Android 6.0: 23

Android 5.1: 22

Android 5.0: 21

Android 4.4W: 20

Android 4.4: 19

Android 4.3: 18

requiresAutojsVersion(version)

  • version {string} | {number} The version or version number of CloudControl

Indicates that this script requires the specified version of CloudControl to run. For example, requiresAutojsVersion("3.0.0 Beta") means that the script needs to run on CloudControl 3.0.0 Beta and above.

Calling this function determines the version of CloudControl on which the script is running, and throws an exception if it does not meet the requirement.

The version parameter can be an integer, e.g. requiresAutojsVersion(250), or a string, e.g. 3.0.0 Beta, 3.1.0 Alpha4, 3.2.0, etc.

You can get the current CloudControl version number and version by using app.autojs.versionCode and app.autojs.versionName.

runtime.requestPermissions(permissions)

  • permissions {Array} String array of permissions

Dynamically request permissions for Android. For example.

// Request GPS permissions
runtime.requestPermissions(["access_fine_location"]);

Although Android has many permissions, they must be written to Manifest to be requested dynamically. To prevent abuse of permissions, CloudControl can currently only request two additional permissions.

  • access_fine_location GPS permission
  • record_audio Recording permission

You can add permissions to CloudControl and the applications packaged with CloudControl through the APK editor.

See Permissions Overviewopen in new window for a list of all Android permissions. (not used).

runtime.loadJar(path)

  • path {string} jar file path

Load the target jar file, the classes of that jar file will be available after successful loading.

// load jsoup.jar
runtime.loadJar(". /jsoup.jar");
// use jsoup to parse html
importClass(org.jsoup.Jsoup);
log(Jsoup.parse(files.read(". /test.html")));

(jsoup is a Java implementation of a library for parsing the Html DOM, which can be downloaded at Jsoupopen in new window)

runtime.loadDex(path)

  • path {string} dex file path

Load the target dex file, after successful loading will be able to use the classes of that dex file.

Because loading jar is actually converting jar to dex and then loading it, loading dex file will be much faster than jar file. You can use the dx tool of Android SDK build tools to convert jar to dex.

context

Global variable. An android.content.Context object.

Note that this object is ApplicationContext, so it can't be used to create interface, dialog, etc.

Last update:
Contributors: Bruce