MojoPad

Scripting: Plugins

Any page named Plugin: Something is a JavaScript program. It appears in the Plugin menu and in the Scripts palette, and runs with full access to the document through the mojo API. The Scripts palette also has a script runner (⌘⏎ to run) for one-off experiments.

The mojo API

CallDoes
mojo.pages()id, name, type, tags, aliases of every page
mojo.read(ref)plain text of a page (by name or id)
mojo.readHtml(ref)stored HTML of a page
mojo.write(ref, text)replace a page's content
mojo.append(ref, text)append to a page
await mojo.create(name, text?)create a page (no-op if it exists); returns its id
mojo.exists(ref)does a page answer to this name?
mojo.navigate(ref)open a page
await mojo.alert(msg) / await mojo.prompt(label)talk to the user
mojo.getMeta(ref, key) / mojo.setMeta(ref, key, value)read/write page meta (the Meta palette)
mojo.today()today's date, formatted per your settings
mojo.log(…)collect output shown after the run

A complete plugin

Create a page named Plugin: Stale Pages containing:

const cutoff = Date.now() - 90*24*3600e3
for (const p of mojo.pages()) {
  if (!mojo.read(p.name)) continue
  mojo.log(p.name)
}

…then run it from the Plugin menu. Scripts are plain modern JavaScript (await works at the top level). Locked encrypted pages are invisible to scripts, like everywhere else.

Built-in plugins

The Plugin menu also ships with Word Count, Alphabetize, Strike Out and Move to Bottom (⌃⌘⌫), Apply Default Font to Page, Record Audio… (embeds a voice memo right in the page), and Send Page by Email.