MojoPad

Scripting: Scriptlets

Scriptlets are little pieces of JavaScript embedded in a page that run when the page is exported, previewed, or used as a template. The syntax is VoodooPad's:

<% writer.write('Hello from a scriptlet') %>
<%= new Date().getFullYear() %>

The = form writes the expression's value; the plain form runs statements and writes whatever you pass to writer.write(). (In this manual the tags are typeset so they won't run; in your pages, just type them normally.)

Nothing runs until you say so

Code in a document is gated per document, and MojoPad asks once. Scriptlets, plugins and Event pages all wait behind the same question: do you trust this document to run code? Until you answer yes, none of them run — which is what stops a wiki somebody sends you from executing anything the moment you open it.

So if a plugin you just wrote appears to do nothing, this is the first thing to check. The answer is remembered per document, and only pages you wrote yourself are eligible in the first place — a page that arrived from a followed folder, an import, an agent or a link never runs, however it is named. See Only pages you wrote will run under Scripting: Event Pages.

What a yes covers, and what it does not

Saying yes lets a document’s code read and change pages in that document. It is not a yes to everything MojoPad can do. Code in a page runs inside the same window you are reading in, which means it can put things on screen — and it used to mean it could press things on screen as well, including buttons that reach outside the wiki.

It cannot do that anymore. MojoPad tells the difference between a button you pressed and one pressed for you, and the ones that matter answer only to you:

  • Anything that leaves MojoPad — opening a link in your browser, opening a file in another app, revealing something in the Finder.
  • Putting the wiki on your network, and turning on the microphone.
  • Any command run by name from the command palette — which is every menu in the app, publishing and exporting included.
  • Answering a question MojoPad asked you — a list to pick from, a right-click menu, a confirmation.
  • Switching this window to another wiki, or opening one in a new window.

What a document’s code still does, because that is the point of saying yes: read and write its own pages, move you around inside the wiki, add files, and ask for what it needs. Reaching the web is part of that — and it asks you before it reaches a site you have not already allowed for this wiki. The question offers to Remember that site for the wiki, checked unless the address came from a redirect; say yes with it checked and the script reaches that site from then on without asking again. Settings ▸ Sharing ▸ What you have agreed to lists every answer you have kept, and forgets any of them.

None of this asks you anything new. Press the button yourself and it works exactly as it always did.

The scriptlet environment

ObjectMeaning
writer.write(s)append output where the scriptlet stands
destinationweb, pdf, epub, page, preview, or template — vary output by where it's going
page.namethe page being rendered

Errors render inline in red rather than breaking the export. Scriptlets run during every export, HTML preview, and template instantiation — they're how a page can contain the current date, a computed table, or different footers for web and PDF.

The HTML preview of a rich text page (⌃⌘P) is for reading what the scriptlets make. Nothing pasted or dropped on it is saved, so the page keeps its scriptlets as you wrote them; press ⌃⌘P again to write.