JavaScript Scripts

With scripts in JavaScript, you automate repetitive tasks: creating shapes in grids, changing many objects at once, calculating values. This chapter shows the Scripts docker, the script folder, and the scripting interface.

Scripts docker

Open the docker with Tools > Scripts > Script Editor or via Window > Dockers > Scripts.

Area Function
Drop-down list at the top (New Script) or a script from the script folder; selecting one loads it into the editor
Run button runs the script in the editor; so does Ctrl+Enter in the editor
Save in Script Folder button asks for a name and saves it as a .js file
Open Script Folder button opens the folder in the file manager
Editor the source code; the first time you open it, it contains a sample script
Output whatever the script outputs with console.log; error messages appear in red

Writing and running a script

  1. Choose (New Script) or a saved script.
  2. Write or change the code in the editor.
  3. Press Ctrl+Enter or click Run.
  4. If you do not like the result, Ctrl+Z undoes the entire run.

Good to know:

Script folder and Scripts menu

Saved scripts are stored as .js files in the script folder. The Tools > Scripts menu contains:

If a script run from the menu fails, the docker opens with the error message.

Under Tools > Options > Workspace > Scripts, you set:

Setting Meaning
Script folder: folder for scripts; empty = folder Skripte in the program data folder (on Windows %APPDATA%\Tangenta\Tangenta\Skripte)
Abort script after: time limit in seconds (1–3600)

Tip: Store scripts you use often in the script folder. They then appear directly in the Tools > Scripts menu.

Interface basics

Scripts are JavaScript (ECMAScript 7) and work with the document through the tg object.

The tg object

Property / function Meaning
tg.pageCount number of pages
tg.currentPage current page (starting at 1), can be set
tg.pageWidth, tg.pageHeight size of the current page in mm
tg.version version of the scripting interface
tg.rectangle(x, y, w, h, radius?) rectangle, optionally with rounded corners
tg.ellipse(x, y, w, h) ellipse
tg.polygon(x, y, w, h, corners?, star?) polygon or star (default 5 corners)
tg.line(x1, y1, x2, y2) line
tg.polyline([[x, y], …], closed?) polyline
tg.text(x, y, text, pt?, font?) artistic text (default 24 pt)
tg.selection() selected objects
tg.select(obj) or tg.select([obj, …]) set the selection
tg.objects() all objects on the current page
tg.find(id) object by its number
console.log(…), tg.print(…) output in the docker

Objects

The functions of tg return objects with these properties and functions:

Property / function Meaning
id, type number and kind (rectangle, ellipse, polygon, curve, text, table, image …)
x, y, width, height bounding rectangle; setting these moves or scales the object
fill, stroke, strokeWidth fill color, outline color, outline width in mm
text contents of a text object
name name in the Object Manager
move(dx, dy), moveTo(x, y) move, relative or to a position
rotate(degrees, cx?, cy?) rotate counterclockwise; without a center, around the middle
scale(sx, sy?) scale around the center
duplicate(dx?, dy?) copy; returns the new object
remove() delete
properties(), get(name), set(name, value) read and set property bar values, e.g. points for a polygon, rows/columns for a table

Examples

Grid of circles, color by column:

const colors = ["crimson", "orange", "gold", "seagreen", "royalblue"];
for (let r = 0; r < 4; r++)
    for (let c = 0; c < colors.length; c++) {
        const k = tg.ellipse(20 + c * 30, 20 + r * 30, 24, 24);
        k.fill = colors[c];
        k.stroke = null;
    }

Move all selected objects 5 mm to the right and give them a red outline:

for (const o of tg.selection()) {
    o.move(5, 0);
    o.stroke = "red";
}

Output page information:

console.log(tg.currentPage, "/", tg.pageCount, "–", tg.pageWidth, "x", tg.pageHeight, "mm");

Tip: The AI Assistant draws simple shapes itself using scripts like these.