JavaScript tutorial:
Automating

 

How to automate tasks using AutoScript

AutoScript allows you to automate repetitive tasks (e.g., inserting blocks of code), and implement macro-like capabilities.

You always have tasks that you perform again and again with the files you are working on. For example, this can be inserting copyright or programmer information, replacing bits of text, adjusting templates, etc.

Up until version 6 the main way to automate repetitive tasks was to create short VBScripts to do the job. This drew some well-founded criticism from users:

Why learn another scripting language (VBScript) in order to automate tasks on JavaScript, HTML and other files?
Consistency: built-in VBScript editor was significantly different from the JavaScript-editing environment.

We listened, and the result is here: VBScript has been fully replaced by AutoScript.

An AutoScript is simply a JavaScript operating on your current document to automate repetitive tasks.

You can now apply your knowledge of JavaScript to create scripts that modify the documents you are working on. You only need a single click to apply the same script to another document.

Below is an example:

First, you get the text of the current JavaScript Editor document.
Second, you modify it. In this case, an extra line of text is inserted at the beginning of the document.
Third, you tell the Editor to use the modified text:

AutoScript example

What if you make a mistake? Correcting it is as fast and easy as with any other JavaScript file: your JavaScript Editor automatically opens the file, navigates to the error, and explains it for a speedy correction.

To run an AutoScript to modify the active document:

Press Shift+Ctrl+A (or select AutoScript / AutoScripts from the menu), and

Double-click the script (or click once to select it, and click on the Run button.

To use the same AutoScript again on another open document, click on the Run AutoScript button.

How to automate using macros

Automate common operations within the editor with the macro recording / playing tool. It records your keystrokes, commands and mouse actions, saves them and replays them as needed.

To record a new macro, select Automate / Record Macro from the menu

To select a macro to run, select Automate / Macros from the menu

To repeat the last macro command, select Automate / Play Macro from the menu