The power of script

Scripts are computer programs. Often they are small, dedicated applications that perform a single task. However, it is perfectly possible to build big and complex applications using script.

Essentially a script is a set of computer instructions in a text file. This text file can be executed, often by double clicking the file icon. A separate computer program, a so called interpreter, reads the instructions in the text file and ‘executes’ them one by one.

The instructions in the script are written in a programming language. Windows supports a number of scripting languages out of the box. Well known examples are JScript, Visual Basic Script and DOS batch (,bat) files. The programming language we will use in the examples is called JScript. At the end of this article will provide links to JScript and other scripting resources.

The following screenshot shows a JScript file opened in the notepad application. The file is shown in the Explorer Window.

A script file

The script is executed by double clicking the icon in the Explorer window. The running program should look like this:

The running script

Scripting and Hotkeys

You can use Qliner Hotkeys to start scripts written in any language. Scripts are an easy and powerful way of extending Qliner Hotkeys. To make Hotkeys start a script simply drag and drop a script on the desired key. In this case pressing Windows + 1 would trigger your script.

Drag and drop the script on the keyboard

Doing useful stuff

Now let us make something useful. Script will allow you to do almost anything a human user could do with a computer. One example is that you can have a script send key strokes to the current application.

The following example will send the key strokes “Hello World!” to the running application. Type in or download the following script and assign a hotkey to it. Then start notepad and trigger the hotkey. The text “Hello World!” should appear in notepad.

WScript.Sleep(300);

typeString("Hello World!");

function typeString(stringToType){
   var wsh = WScript.CreateObject("WScript.Shell");
   for(var i = 0; i < stringToType.length; i++){
      wsh.SendKeys("{" + stringToType.substr(i, 1) + "}");
      WScript.Sleep(10);
   }
}

You can also send key combination that trigger actions in the document, like Alt + F4 to close a window, Ctrl + A and then Ctrl + C to copy all content in a Window, etc.

Making it configurable

It is possible to make a script configurable so you can use the same script for different purposes. It is possible to send command line arguments to a script. The command line arguments can then be used as input for the script.

The following JScript example sends the text in the command line argument as keystrokes to the currently active application. Download the example here.

WScript.Sleep(300);

for(var i = 0; i < WScript.Arguments.count(); i++){
    typeString(WScript.Arguments.Item(i));
}

function typeString(stringToType){
   var wsh = WScript.CreateObject("WScript.Shell");
   for(var i = 0; i < stringToType.length; i++){
      wsh.SendKeys("{" + stringToType.substr(i, 1) + "}");
      WScript.Sleep(10);
   }
}

If you configure a hotkey for the script, double click the script icon on the keyboard to modify the arguments. Please consider the following screenshot.

Notice the configured argument

When the hotkey is triggered while notepad is active the output should look like this:

Output from configuration

Creating a poor man’s Quotes

The following example creates a minimalist version of Qliner Quotes (but then again: the original is much, much longer than 24 lines of code). The script below (download here) takes a file name as an argument. The file (in our case) contains humorous quotes. A random line is selected from that file and then typed in to the active application. This is what the script look like:

WScript.Sleep(500);

if(WScript.Arguments.count() == 1){
   var filename = WScript.Arguments.Item(0);
   var lines = new Array();
   var filesystem = new ActiveXObject("Scripting.FileSystemObject");
   var file = filesystem.GetFile(filename);
   var stream = file.OpenAsTextStream( 1, 0 );
   var count = 0;
   while( !stream.AtEndOfStream ){
      lines[count] = stream.ReadLine();
      count++;
   }
   stream.Close();
   var randomLine = lines[ Math.floor(Math.random()*lines.length) ];
   typeString(randomLine);
}
 
function typeString(stringToType){
   var wsh = WScript.CreateObject("WScript.Shell");
   for(var i = 0; i < stringToType.length; i++){
      wsh.SendKeys("{" + stringToType.substr(i, 1) + "}");
   }
}  

This is what the configured key should look like.

Quotes hotkey configuration

The quotation file ‘Humor.txt’ was downloaded from here, but any text file should do.

And this is what the output could look like.

A random quote ;)

Conclusion

Scripts allow you to automate everyday tasks. It is easy to use and very powerful technology. Combine it with Qliner Hotkeys and all this power is at the tips of your fingers. Please remember that the examples given here are very simple. Also be aware that you can use any scripting language with Qliner Hotkeys, not just JScript.

Please consider the following links for further reading:


AND