Skip to content

Scripting API

Namek edited this page Jun 16, 2016 · 3 revisions

Scripting API

The Console is built on top of Nashorn which already provides multiple things, but most of all, it connects with Java classes very well. Besides Nashorn, The Console itself provides few functions available to use across both single-file scripts and modules.

Globals variables and functions

args.length
args[0], args[1], ...
Utils.audioFilePlayer.play(filePath)
Utils.exec(filePath)
Utils.execAsync(filePath)
Utils.getClassName(object)
Utils.openUrl(encodeURI(someUrl))
Utils.requestUrl(url) //HTTP GET
Utils.completePath(absolutePath)
console.log(text)
console.log(text, color)
console.error(text)
console.clear()
console.hide()
console.window
assert(bool, string)
assertInfo(bool, string)
JavaClass(className) // Java: Class.forName(className);
System // Java: System.class

Sources for globals:

Script Storage

Every script can store it's configuration or whatever. To see some examples go see filesystem.

  1. Get your storage:

    Storage = Storage.getGlobalStorage("myscript")
  2. Get variable

    var path = Storage.get("path")
    console.log(path)
    
  3. Overwrite variable and save Storage

    var newPath = ...
    Storage.set("path", newPath)
    Storage.save()

Assertion

You can assert whatever you like, e.g. script arguments.

When first argument of assert/assertInfo is not true, it stops whole scripts and displays given text to the console:

  1. red text:

    assert(args.length == 0, "Please, no args.")
  2. white text:

    assertInfo(args.length == 0, "Please, no args.")

Nashorn (Java 8 extension)

https://gist.github.com/WebReflection/9627010

https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html