KScript docs
EN

KScript.UI

Desktop windows with buttons, text, inputs and timers, on macOS, Windows and Linux.

Import it at the top of a script:

<import package="KScript.UI" />
import KScript.UI

A file whose root element is <KScript.Window> imports it for you.

A .kscript file names a package only in its import: write Window { }, never KScript.Window { }.

KScript.UI opens desktop windows, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.

ElementsStatements

#<Box>Box

A rectangle: a coloured cell, a card or a panel. It can hold other items, and on_click (or a shorthand such as toggle="$on") makes it clickable.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
on_clickstatementsRuns when it is clicked.
bordera string with { }A border colour.
toggleexpressionFlips a bool field: toggle: $open is open = not $open. Runs on on_click, like on_click: "toggle: …".
callexpressionCalls a function or one of the component's methods: call: $save, with: [$note.id]. Runs on on_click, like on_click: "call: …".
withexpressionGoes with call:.
incrementexpressionAdds 1 (or by:) to a number field: increment: $count, by: 5. Runs on on_click, like on_click: "increment: …".
byexpressionGoes with increment: or decrement:.
decrementexpressionTakes 1 (or by:) from a number field, never going below its min: (stock: int(min: 0)): decrement: $stock. Runs on on_click, like on_click: "decrement: …".
setexpressionSets a field to a value: set: $tab, to: 'notes'. Runs on on_click, like on_click: "set: …".
toexpressionGoes with set: or add:.
addexpressionAdds a value to a list field: add: $draft, to: $tags. Runs on on_click, like on_click: "add: …".
removeexpressionRemoves a value from a list field: remove: $tag, from: $tags. Runs on on_click, like on_click: "remove: …".
fromexpressionGoes with remove:.
resetexpressionPuts a field (or a list of fields) back to its declared default: reset: $draft, or reset: [$title, $done]. Runs on on_click, like on_click: "reset: …".
cycleexpressionMoves a field to the next value of its enum, or of a list given with through:, wrapping round: cycle: $view, through: ['list', 'grid']. Runs on on_click, like on_click: "cycle: …".
throughexpressionGoes with cycle:.
saveexpressionSaves a KScript.Data record: save: $note is $note->save(). Runs on on_click, like on_click: "save: …".
deleteexpressionDeletes a KScript.Data record: delete: $note is $note->delete(). Runs on on_click, like on_click: "delete: …".
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Box width="20" height="20" background="{ $on ? 'lime' : '#222' }" on_click="$on = not $on" />
Box(width: 20, height: 20, background: "{ $on ? 'lime' : '#222' }", on_click: { $on = not $on })

#<Button>Button

A button. Its text is the label; on_click runs when it is pressed, or name the action straight on it: toggle="$open".

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
on_clickstatementsRuns when the button is pressed, e.g. on_click="$count += 1".
variantnormal|primaryprimary makes it stand out.
disabledexpressiontrue greys it out so it can't be used.
toggleexpressionFlips a bool field: toggle: $open is open = not $open. Runs on on_click, like on_click: "toggle: …".
callexpressionCalls a function or one of the component's methods: call: $save, with: [$note.id]. Runs on on_click, like on_click: "call: …".
withexpressionGoes with call:.
incrementexpressionAdds 1 (or by:) to a number field: increment: $count, by: 5. Runs on on_click, like on_click: "increment: …".
byexpressionGoes with increment: or decrement:.
decrementexpressionTakes 1 (or by:) from a number field, never going below its min: (stock: int(min: 0)): decrement: $stock. Runs on on_click, like on_click: "decrement: …".
setexpressionSets a field to a value: set: $tab, to: 'notes'. Runs on on_click, like on_click: "set: …".
toexpressionGoes with set: or add:.
addexpressionAdds a value to a list field: add: $draft, to: $tags. Runs on on_click, like on_click: "add: …".
removeexpressionRemoves a value from a list field: remove: $tag, from: $tags. Runs on on_click, like on_click: "remove: …".
fromexpressionGoes with remove:.
resetexpressionPuts a field (or a list of fields) back to its declared default: reset: $draft, or reset: [$title, $done]. Runs on on_click, like on_click: "reset: …".
cycleexpressionMoves a field to the next value of its enum, or of a list given with through:, wrapping round: cycle: $view, through: ['list', 'grid']. Runs on on_click, like on_click: "cycle: …".
throughexpressionGoes with cycle:.
saveexpressionSaves a KScript.Data record: save: $note is $note->save(). Runs on on_click, like on_click: "save: …".
deleteexpressionDeletes a KScript.Data record: delete: $note is $note->delete(). Runs on on_click, like on_click: "delete: …".
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Button variant="primary" on_click="$count += 1">Add one</Button>
Button(variant: primary, on_click: { $count += 1 }) "Add one"

#<CheckBox>CheckBox

A tick box tied to a true/false variable. Its text is the label.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
bind requireda variable's nameThe variable it shows and changes. Declare it with <let> directly inside <KScript.Window>.The variable it shows and changes. Declare it with $name = value directly inside <KScript.Window>.
on_changestatementsRuns after the person changes the value ($event.value), e.g. on_change="saved = false".
disabledexpressiontrue greys it out so it can't be used.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<CheckBox bind="wrap">Wrap around the edges</CheckBox>
CheckBox(bind: wrap) "Wrap around the edges"

#<Column>Column

Stacks its items top to bottom.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
gapexpressionSpace between items, in pixels.
alignstart|center|end|stretchHow items line up across the container: start, center, end or stretch.
scrolltrue or falsetrue adds a scroll bar when the items don't fit.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Column gap="8">
  <Text>One</Text>
  <Text>Two</Text>
</Column>
Column(gap: 8) {
  Text "One"
  Text "Two"
}

#<Grid>Grid

Lays its items out in equal columns, filling each row in turn: a board, a keypad, a gallery.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
gapexpressionSpace between items, in pixels.
columns requiredexpressionHow many columns.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Grid columns="2" gap="8">
  <Text>One</Text>
  <Text>Two</Text>
</Grid>
Grid(columns: 2, gap: 8) {
  Text "One"
  Text "Two"
}

#<Row>Row

Puts its items side by side.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
gapexpressionSpace between items, in pixels.
alignstart|center|end|stretchHow items line up across the container: start, center, end or stretch.
wraptrue or falsetrue moves items to a new line when they don't fit.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Row gap="8">
  <Text>One</Text>
  <Text>Two</Text>
</Row>
Row(gap: 8) {
  Text "One"
  Text "Two"
}

#<Select>Select

A drop-down list tied to a variable.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
bind requireda variable's nameThe variable it shows and changes. Declare it with <let> directly inside <KScript.Window>.The variable it shows and changes. Declare it with $name = value directly inside <KScript.Window>.
options requiredexpressionThe choices: a list of values, a list of { label, value } maps, or a map from label to value.
on_changestatementsRuns after the person changes the value ($event.value), e.g. on_change="saved = false".
disabledexpressiontrue greys it out so it can't be used.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Select bind="speed" options="{ Slow: 400, Normal: 150, Fast: 40 }" />
Select(bind: speed, options: { Slow: 400, Normal: 150, Fast: 40 })

#<Slider>Slider

A slider tied to a number variable.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
bind requireda variable's nameThe variable it shows and changes. Declare it with <let> directly inside <KScript.Window>.The variable it shows and changes. Declare it with $name = value directly inside <KScript.Window>.
minexpressionThe smallest value (default 0).
maxexpressionThe largest value (default 100).
stepexpressionSnap to multiples of this.
on_changestatementsRuns after the person changes the value ($event.value), e.g. on_change="saved = false".
disabledexpressiontrue greys it out so it can't be used.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Slider bind="volume" min="0" max="10" step="1" />
Slider(bind: volume, min: 0, max: 10, step: 1)

#<Spacer>Spacer

Empty space.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
sizeexpressionHow much space, in pixels (default 12).
Example
<Column>
  <Text>Above</Text>
  <Spacer size="24" />
  <Text>Below</Text>
</Column>
Column {
  Text "Above"
  Spacer(size: 24)
  Text "Below"
}

#<Text>Text

Shows text. { } inserts values.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
sizeexpressionFont size in points (default 14).
weightnormal|bold|lightnormal, bold or light.
colora string with { }Text colour: a name or #hex.
wraptrue or falsetrue wraps long text onto more lines.
text_alignleft|center|rightleft, center or right.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<Text size="24" weight="bold">Hello, {$name}!</Text>
Text(size: 24, weight: bold) "Hello, {$name}!"

#<TextBox>TextBox

A box to type in, tied to a variable. on:, trim: and as: say when and how the typed value is stored, as with @bind on a page.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
bind requireda variable's nameThe variable it shows and changes. Declare it with <let> directly inside <KScript.Window>.The variable it shows and changes. Declare it with $name = value directly inside <KScript.Window>.
oninput|change|blurWhen the typed value is stored: input (as you type, the default), change or blur (when focus leaves or Enter is pressed).
trimtrue or falsetrue trims spaces from both ends before the value is stored.
as'string'|'int'|'number'|'bool'Reads the typed value as this type before it is stored, and refuses one that isn't: as: int.
placeholdera string with { }Grey text shown while it is empty.
on_enterstatementsRuns when Enter is pressed.
on_changestatementsRuns after the person changes the value ($event.value), e.g. on_change="saved = false".
multilinetrue or falsetrue allows several lines.
disabledexpressiontrue greys it out so it can't be used.
widthexpressionWidth in pixels.
heightexpressionHeight in pixels.
paddingexpressionSpace inside the edge, in pixels.
marginexpressionSpace outside the edge, in pixels.
backgrounda string with { }A colour: a name such as teal, or #1e293b.
radiusexpressionHow rounded the corners are, in pixels.
tooltipa string with { }Text shown when the pointer rests on it.
Example
<TextBox bind="name" trim="true" placeholder="Your name" on_enter="$greeting = 'Hi ' + $name" />
TextBox(bind: name, trim: true, placeholder: 'Your name', on_enter: { $greeting = 'Hi ' + $name })

#<Timer>Timer

Runs on_tick again and again while it is in the layout (and when is true). The next tick is counted from when the last one finished.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
every requiredexpressionHow long between ticks: 500ms, 1s. A plain number is seconds.
whenexpressionOnly tick while this is true, e.g. when="$playing".
on_tick requiredstatementsRuns on every tick.
Example
<Timer every="100ms" when="$playing" on_tick="$frame += 1" />
Timer(every: 100ms, when: $playing, on_tick: { $frame += 1 })

#<KScript.Window>Window

Opens a desktop window. Its direct <let>, <fn> and <import> children run once and hold its state; the rest is the layout, drawn again after every on_* handler. The script continues when the window is closed.Opens a desktop window. Its direct $name = value, fn and import children run once and hold its state; the rest is the layout, drawn again after every on_* handler. The script continues when the window is closed.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
titlea string with { }The title bar text.
widthexpressionStarting width in pixels (default 800).
heightexpressionStarting height in pixels (default 600).
themedark|light|systemdark, light or system (the default).
backgrounda string with { }The window's colour.
paddingexpressionSpace around the layout, in pixels (default 16).
gapexpressionSpace between the window's top-level items (default 12).
resizabletrue or falsefalse fixes the size.
Example
<KScript.Window title="Counter" width="300" height="200">
  <let name="count" value="0" />
  <Text size="32">{$count}</Text>
  <Button on_click="$count += 1">Add one</Button>
</KScript.Window>
import KScript.UI

Window(title: 'Counter', width: 300, height: 200) {
  $count = 0
  Text(size: 32) "{$count}"
  Button(on_click: { $count += 1 }) "Add one"
}

Commands

Commands are called with @, in any expression: @name(arguments).

#@close_window()

Closes the window, so the script carries on after </KScript.Window>.
Example
<KScript.Window title="Are you sure?" width="300" height="140">
  <Text>Delete the draft?</Text>
  <Button on_click="@close_window()">Cancel</Button>
</KScript.Window>
import KScript.UI

Window(title: 'Are you sure?', width: 300, height: 140) {
  Text "Delete the draft?"
  Button(on_click: { @close_window() }) "Cancel"
}