Skip to content

Scripting

Sami Ylönen edited this page Jan 26, 2016 · 26 revisions

Simple script

Note: This scripting part is not done, and can be quite buggy.

In the Scripts page, click "Add script".

Add a name for the script, and add source file(s) by clicking "Add source".

For each source file you add, write a file name for it.

You can reorder the scripts when you've added all you need. Execution order matters, place libraries first.

Press save.

You can now find the script entry, folder and js files from DATAROOT\Scripts.

The .js file you intend to be your main "executable" needs to have this methdod

function execute() {
	
}

When done you can call this.

executionCallback(true) // true = request rerun of the script.

execute() will be called by the engine when it's run.

Methods available

See https://github.com/samiy-xx/keysndr/blob/master/Portal/scripts/app/keys.js for possible keys

/*
* Write a message to log
* log(string);
*/
log("message");

/*
* Pause script execution
* pause(int)
* milliseconds
*/
pause(1000); //pause for 1000 milliseconds 

/*
* Notify scripting engine that script is finished
* executionCallback(bool)
* boolean false for no rerun required, try for rerun required
*/
executionCallback(true);

/*
* Get a color on screen at given x and y position
* getPixelColor(int, int);
* x and y positions on the screen
* returns array [int, int, int, int] Red, Green, Blue, Alpha in range 0 - 255
* var redChannel = getPixelColor(50, 50)[0];
*/
getPixelColor(50, 50);

/*
* Get window rectangle properties
* getWindowRectangle()
* returns array [int, int, int, int] Left, Top, Right, Bottom
* var windowX = getWindowRectangle()[0];
*/
getWindowRectangle();

/*
* Sending a key asynchronously
* sendInput(string, int, func);
* key to send, keep key down for milliseconds, callback
*/
sendInput("Tab", 50, function() {
   // Do something
});

/*
* Sending a key and modifiers asynchronously
* sendInputMod(string, string[], int, func);
* key to send, modifier keys, keep key down for milliseconds, callback
*/
sendInputMod("Escape", ["LControlKey"], 50, function() {
   // Open windows start menu
});

/*
* Sending a key synchronously
* key to send, keep key down for milliseconds
* sendInputSync(string, int)
*/
sendInputSync("Escape", 50);

/*
* Sending a key and modifiers synchronously
* key to send, modifier keys, keep key down for milliseconds
* sendInputModSync(string, string[], int)
*/
sendInputModSync("Escape", ["LControlKey"], 50);

/*
* Sending a string asynchronously
* sendString(string, int, func);
* string to send, keep each key down for milliseconds, callback
*/
sendString("Message", 200, function() {
  // Do something
});

/*
* Sending a string synchronously
* string to send, keep key down for milliseconds
* sendStringSync(string, int)
*/
sendStringSync("Message", 200);

/*
* Move mouse cursor to absolute position
* moveMouse(int, int);
* x and y on screen
*/
moveMouse(5, 5);

/*
* Move mouse cursor relatively from current position
* moveMouseRelative(int, int);
* x and y from your current mouse cursor position
*/
moveMouseRelative(5, 5); // Moves mouse relative x, y from current position

/*
* Click mouse button asynchronously
* clickMouse(int, int, func);
* mouse button, keep mouse button down for milliseconds, callback
* 1 = left, 2 = right, 3 = middle
*/
clickMouse(1, 100, function() {
  // Do something
});

/*
* Click mouse button synchronously
* clickMouseSync(int, int);
* mouse button, keep mouse button down for milliseconds
* 1 = left, 2 = right, 3 = middle
*/
clickMouseSync(1, 100);

Script input

Access script input from script. Inputs can be defined in management portal for each script.

function execute() {
    log("width = " + getParameterValue("width"));
    log("height = " + getParameterValue("height"));
}

function getParameterValue(k) {
    for (var i = 0; i < scriptParameters.length; i++) {
        var p = scriptParameters[i];
        if (p.key === k)
            return p.value;
    }
    return null;
}

Libs included in scripting engine

C# System namespace, KeySndr.Base and KeySndr.Common

Interval like functionality

var Utils = importNamespace("KeySndr.Base.JsUtils");
var timer = new Utils.JsTimer();
var timesRun = 0;
timer.Tick(function() {
   sendInput("Tab");
   if (timesRun == 5) {
      timer.Stop();
   }
   timesRun++;
});
timer.Start(0, 400); // Delay to start, interval. Milliseconds

Use the script

When editing/creating an action. Right next to the "add sequence" button is also a "add script" button. Click that and select the script you created.

The script is run when the action is executed.

Clone this wiki locally