Scripted Text Generation
Scripted Text Generation
Section titled “Scripted Text Generation”The Plain text backend runs a JavaScript script and displays whatever main()
returns as formatted text. It is useful for generating reports, configuration
files, data summaries, or any other text-based output — all from code.
Your first script
Section titled “Your first script”Open the Modelling tab, click New project, and choose Plain text as the backend. A starter file is created for you. Hit ▶ Run to see the output.
"use strict";
function main() { return 'Hello, world!'}
module.exports = { main }main() can return any value. Strings are displayed verbatim; anything else is
serialised with JSON.stringify and pretty-printed.
Template literals
Section titled “Template literals”JavaScript template literals make multi-line text easy to construct:
function main() { const name = 'JavaSculpt' const version = '1.0'
return `=== ${name} v${version} ===
Built with scripted text generation.Current time: ${new Date().toISOString()} `.trim()}Parameters
Section titled “Parameters”Export a getParameterDefinitions() function to expose live controls.
The parameter values are passed to main() and the output updates automatically
whenever a control is changed:
const getParameterDefinitions = () => [ { name: 'greeting', type: 'text', caption: 'Greeting', default: 'Hello' }, { name: 'name', type: 'text', caption: 'Name', default: 'World' }, { name: 'count', type: 'number', widget: 'slider', caption: 'Repeat', default: 3, min: 1, max: 10 }, { name: 'upper', type: 'boolean', caption: 'Uppercase', default: false },]
function main({ greeting, name, count, upper }) { let line = `${greeting}, ${name}!` if (upper) line = line.toUpperCase() return Array(count).fill(line).join('\n')}
module.exports = { main, getParameterDefinitions }Generating structured data
Section titled “Generating structured data”Return a plain object or array to get pretty-printed JSON output:
function main({ itemCount }) { return { generated: new Date().toISOString(), items: Array.from({ length: itemCount }, (_, i) => ({ id: i + 1, label: `Item ${i + 1}`, value: Math.round(Math.random() * 100), })), }}Building tables
Section titled “Building tables”Pad strings to fixed widths to produce aligned plain-text tables:
function main() { const data = [ { name: 'Alice', score: 98 }, { name: 'Bob', score: 74 }, { name: 'Charlie', score: 85 }, ]
const header = 'Name Score' const divider = '-'.repeat(header.length) const rows = data .sort((a, b) => b.score - a.score) .map(r => r.name.padEnd(10) + r.score.toString().padStart(5))
return [header, divider, ...rows].join('\n')}Using require for workspace files
Section titled “Using require for workspace files”Scripts can require other .js files from the workspace. Place shared
utilities or data in a lib/ folder and import them:
function formatDate(d) { return d.toLocaleDateString('en-AU', { dateStyle: 'long' })}module.exports = { formatDate }// main scriptconst { formatDate } = require('./lib/utils')
function main() { return `Report generated on ${formatDate(new Date())}`}Async main
Section titled “Async main”main() may be async. This lets you await Promises from workspace
libraries or injected dependencies:
async function main({ url }) { const data = await fetchData(url) // hypothetical workspace helper return JSON.stringify(data, null, 2)}Downloading
Section titled “Downloading”Click ↓ Download in the viewer toolbar to save the output as a .txt
file. The filename is derived from the script name.
Next steps
Section titled “Next steps”- Use Edit parameters… in the toolbar to add or edit parameter definitions
visually, without writing
getParameterDefinitionsby hand. - Combine multiple workspace files to build more complex generators — e.g., a templating engine, a CSV exporter, or a config file generator.