Skip to content

Scripting

flarom edited this page Sep 26, 2025 · 12 revisions

Cohesion allows the use of scripts to create automations inside of documents. With scripts, you can insert text, create files, manipulate blocks, handle input fields, persist data in memory, fetch API data, and more.

Script blocks are created by titling a codeblock as JavaScript or JS. The rendered codeblock will have run icon that executes the script when clicked.

Note

The HTML <script> tag is disabled. The only way to run a script is by clicking the run icon of a JavaScript codeblock.



UI Tools

ui.notify

Shows a toast notification at the top of the screen.

Syntax

ui.notify("Hello World!");

ui.modal.text

Shows a text input modal and returns the string the user entered.

Syntax

async function promptName() {
    const name = await ui.modal.text("Enter your name:", "Default value");
    ui.notify("You wrote: " + name);
}

promptName();

Document Functions

insert

Inserts text in the document at a given position.

Syntax

insert(Text, Position);

Positions available:

  • documentPosition.cursor → at cursor position
  • documentPosition.end → at end of document (default)
  • documentPosition.start → at start of document
  • documentPosition.start_safe → at start, after the meta block

Example

insert("Hello World!", documentPosition.start);
// Inserts text at the top

insert("Goodbye World!");
// Inserts text at the bottom (default)

file.new

Creates a new document (with confirmation from the user).

Syntax

file.new("Document content here");

Example

file.new(`
# Title
This is a paragraph
`);

Memory Functions

The memory object allows you to save values that persist until cleared.

  • memory.set(key, value) → stores a value
  • memory.get(key, fallback) → retrieves a value (or fallback if missing)
  • memory.remove(key) → deletes one value
  • memory.clear() → deletes all memory values
  • memory.list() → returns an object with all stored values

Example

memory.set("username", "Alice");
ui.notify(memory.get("username")); // Alice

memory.remove("username");

Block Functions

Blocks are Markdown structures like:

> [!Note](block-id)
> Example content

The block object lets you create and manipulate them.

block.make

Creates a new block with a type, id, and optional content.

block.make("Hello", "intro-block", "Hello World!");

block.set

Replaces all content of a block.

block.set("intro-block", "New content");

block.add

Appends new lines at the end of a block.

block.add("intro-block", "Added at the bottom");

block.addStart

Adds new lines at the beginning of a block.

block.addStart("intro-block", "Added at the top");

block.getContent

Retrieves the block’s inner content. Modes: "plain", "plain-with-markers", "raw"

const text = block.getContent("intro-block");
ui.notify(text);

block.get

Gets the entire block including header.

const blockData = block.get("intro-block");

block.rename

Renames the block’s type.

block.rename("intro-block", "Warning");

block.getName

Gets the block’s current type.

const type = block.getName("intro-block");

block.remove

Deletes the block.

block.remove("intro-block");

Field Functions

Fields are HTML inputs inside the document.

Create Fields

field.make.textbox("name", "Alice", 50); /*id, value, max lenght*/
field.make.slider("volume", 30, 0, 100); /*id, value, min, max*/
field.make.spinner("count", 1, 0, 10); /*id, value, min, max*/
field.make.date("birthdate"); /*id*/
field.make.time("alarm"); /*id*/
field.make.select("options", ["A", "B", "C"], "B"); /*id, options, default*/

Manage Field Values

  • field.getValue(id) → get the current value(s)
  • field.setValue(id, value) → update value(s)
  • field.keepValue(id) → keeps field values synced inside the document

Example

const volume = field.getValue("volume");
ui.notify("Volume is " + volume);

field.setValue("volume", 75);

Network Functions

network.fetch

Fetches data from an API and runs a callback with the JSON response.

Syntax

network.fetch(URL, Callback);

Example

network.fetch("https://pokeapi.co/api/v2/pokemon/pikachu", function(data) {
    const info = `
Name: ${data.name}
Height: ${data.height}
Weight: ${data.weight}
Type: ${data.types.map(t => t.type.name).join(", ")}
    `;
    block.add("pokemon-info", info);
});

Date/Time Parsers

strftime

Formats the current date/time with strftime syntax.

const now = strftime("The time is %H:%M");
ui.notify(now);

Learn the basic syntax of markdown and the Cohesion flavored markdown

Learn more advanced markdown applications

Learn how to use metadata on your documents to better organize your files

Learn how to create scripts and automation inside of Cohesion documents

Learn how to use commands to speed up your writing

Clone this wiki locally