More Examples
Moving the mouse
- You can use
Mouse.move(1,1);
andMouse.click(1,1);
to send mouseclick events. - Simulating mouse clicks in your script is not recommended, because relying on pixel coordinates is fragile. Your script might not work if the window is in a slightly different position.
- You can use
Sending keyboard events
This is one of the most useful functions in LnzScript.
You can often use Tab to walk through the elements in a dialog.
Alt-key shortcuts can be useful also, especially for menu items that have no keyboard shortcut
How to send the alt-key or control-key:
Keyboard.send('hello'); //sends these keys to the active program Keyboard.send('<Alt>{F4}') // sends Alt+F4, usually quits current app. Keyboard.sendRaw('4 is < 5') // in order to send the literal characters < or { Keyboard.send('<Ctrl>s') // sends Ctrl+S, maybe in order to save Time.sleep(100); // wait 100ms for the save dialog to be ready
Opening a process
The best way to write the path to a file is
@"C:\path
, otherwise you'll have to double the slash like this"C:\\path"
You can use
Process.open
to start a processYou'll usually need to wait until the program has finished loading. I usually use
waitUntilActive
andsleep
like this:Process.open('notepad.exe') Window.waitUntilActive('Notepad') // wait until the active window has a title of Notepad Time.sleep(500) // wait another .5 seconds for it to fully load
Other ways to start a process:
Process.openFile(@'c:\documents\essay.docx') Process.runCmdLine(['mkdir', @'c:\test\myfolder']); var result = Process.runAndRead(['svn', 'status']);
Working with files
Some examples:
var s = File.readFile(@'c:\dir\myfile.txt') //get array of files in the folder sorted by name var arFiles = File.dirListFiles(@'c:\', '*', 'name');
Windows
There are different ways to specify a window,
Window.close('') The currently active window. Window.close('Calc') Window whose title starts with "Calc" Window.close('"Calculator"') Window whose title is exactly "Calculator" Window.close('%ulator%') Window with title containing "ulator" obj = {title:'Calculator', class:'SciCalc'} Window.close(obj) Instance of open "SciCalc" class with title "Calculator"
Example: Sort lines of text in the clipboard
var strClipboard = Clipboard.getText(); if (strClipboard) { var arr = strClipboard.replace(/\r\n/g, '\n').split('\n'); arr.sort(); var strResult = arr.join('\r\n'); Clipboard.setText(strResult); }
Example: Open command-line to the current Explorer directory
Also an example of how to include code
First, download get-explorer-directory from here
Then create a script like this and save it as open_cmd_here.jsz
include('get-explorer-directory.jsz'); Window.activate( {'class':'CabinetWClass'}); // activate the most-recently-used Explorer window. Time.sleep(100); var strDir = getCurrentExplorerDirectory(); if (strDir) { Process.openFile('cmd.exe', strDir); }
As a final touch, you can download an open-source program called Clavier+ that lets you set global keyboard shortcuts in windows. Use Clavier+ to assign a keyboard shortcut that will run open_cmd_here.jsz, and this can be a pretty useful shortcut.
Example: Parsing .url shortcuts in a directory and printing out URLs
File.cd(@'C:\folder'); var arFiles = File.dirListFiles('.', '*.url') for(var i = 0; i < arFiles.length; i++) { var pageName = arFiles[i].slice(0, -4) var url = File.readFile(arFiles[i]).split('URL=')[1].split('\n')[0] print(pageName) print(url + '\n') }
Interacting with Controls
Let's say you want to type text into the text field in a program. Using the mouse coordinates to click on the field is fragile, because if the window has moved, it won't click in the right place. Using the Tab key to move to the field is better, but is still fragile in case the initial focus isn't what you expected.
The robust way to refer to the text field is to refer to it by its id. Then you can be certain you are referring to the right element.
You can refer to a control by ClassName and ID. You can then extract its text, simulate a click, send an event, and even move/hide/resize it. You can use tools that come with LnzScript to determine the ClassName or ID of a control. Open Screencasts and watch the video to see how to do this.
// click on the control with Class=Button, number=6 Control.click('Calc', 'Button6'); Control.setText('Calc', '403', '1337');
Example: Interactive Scripts
var res = Dialog.askYesNo('Question', 'Are you ready?'); if (res == Dialog.YES) print('ok'); include('otherfile.js'); // import another script. // a function that is available when include'd by another script: myFunctionExported = function() { Keyboard.send('ok'); } if (__name__ == 'main') { print('I was not included by another script'); } // fun demo function moveCursor(x, y) { Mouse.move(x,y, true, 1); } for (var i = 0; i < 500; i++) { moveCursor(i*2, Math.sin((i/50.0)*6)*300); } include('<std>') // imports some JavaScript helpers if ('abc'.startsWith('a')) { print('yes') } print('my args are '); printarr(argv);
- Run LnzScript outside of the editor by running something like
lnzscript.exe /f script.js
- Run LnzScript outside of the editor by running something like
Advanced script information
Add the line
include('<std>')
to enable more JavaScript features (prototype methods). You can then use.startsWith()
/.endsWith()
for strings,.max()
and.min()
for arrays, andprintarr(a)
for printing arrays. See documentation in the API Reference under String and Array.There are two special variables. The variable called
argv
is an array containing command-line parameters. The variable__name__
contains either "included" if the current script was included or "main" if the script was run directly (similar to Python's__name__
).LnzScript code can include another file with the
include()
function. To make a variable private to a script file, usevar myVar = 1
. To make a variable public, usemyVar = 1
. To make a function private, use the syntaxfunction foo(x,y) { return x+y }
. To make a function public, use the syntaxfoo = function(x,y) { return x+y }
Scripts with the extension .jsz are associated with lnzscript, so that you can double-click a script to run it. Useful: if you drag and drop a file into a .lnz script, the script can know the dropped file(s) by checking
argv
.In order to write an object-oriented program, JavaScript uses prototype-based inheritance which is slightly different than the way classes work in a language like Java. This is described online many places as seen by a Google search for object-oriented JavaScript.
<!--* (Opening Firefox Tabs)(./flashvids/fftabs.html) Shows how to use Time.sleep, Window.move, Window.resize, File.writeFile, and generating a shortcut to a script.-->
<!--* (Automating Visual Studio)(./flashvids/vstudio.html) An example of automating a user-interface. In this example a script is written that goes through a Visual Studio C++ solution and sets all of the projects from Multi-byte character set to Unicode.-->
Back