Save extensions
A save extension runs each time a drawing is saved (File › Save or Save As…), after the file is written. It is given everything known about the drawing - what it is, its notes, variables, layers, elements and totals - and the file, so it can record the drawing somewhere else: in an index of your drawings, or a database.
Declaring one
onSave({
name: "Drawing index",
description: "What each drawing is, for the database.",
saved(drawing, file) {
print(`Indexed ${file.fileName}`);
return {
file: file.path,
saved: file.saved,
category: drawing.classification.category,
style: drawing.classification.style,
tags: drawing.classification.tags,
customer: drawing.notes["Customer"] ?? null,
length: drawing.variables.L,
cut: drawing.totals.lengthByLayer["Cut"] ?? 0
};
}
});
| Property | Meaning |
|---|---|
name, description |
Its name, in messages, and what it does. |
saved(drawing, file) |
Runs when the drawing has been saved. |
drawing is everything known about the drawing, as drawingInfo() gives it. file is
the file written:
| Property | Meaning |
|---|---|
path |
The whole path. |
name, fileName, folder |
Its name without and with its extension, and the folder it is in. |
format |
Its extension, in lower case: "piq". |
size |
Its size in bytes. |
saved |
When, in UTC: "2026-11-03T14:05:00Z". |
user, machine |
Who saved it, and on which computer. |
What it gives back
What saved returns - an object, an array, text - is kept as JSON with the drawing's window, for the app to pass on;
returning nothing is fine. If you keep a drawing index, it is written there too, with the
drawing's entry (saveExtensions). Extensions are sandboxed: one can't reach a database or a file itself.
What it prints with print(…) or console.log(…) is shown in the Interactive window.
Notes
- Every save extension runs on every save, in turn. One that goes wrong says so in the Interactive window, with the file and line; the drawing is saved anyway, and the others still run.
- A save extension only looks at the drawing: one that tries to change it (
setNote,classify,line…) is stopped with a message. - File › Extensions › Reload Extensions says how many save extensions were found.
See also: Extensions, Drawing API, CLASSIFY, Saving.