Writing Custom Scripts

Advanced users can write custom scripts using JavaScript to manipulate text within TextBuddy.

From the TextBuddy “Scripts” menu, open the scripts folder. This is where you can place custom JavaScript files. (Each file must have a .js extension.)

The scripts you place in that folder will appear under the “Scripts” menu and also available using the ⌘R shortcut key.

A TextBuddy script looks like this:

// All three functions are optional.

// Called first with entire text document
function pre(text) {
  // do something with text
  return someUpdatedText;

// Called next with each line of the document modified by you
function perLine(lineOfText, lineNumber) {
  // do something with the line of text
  // and its lineNumber
  return someUpdatedLineOfText;

// Called last with entire text document as modified by you
function post(text) {
  // do something with text
  return someUpdatedText;

TextBuddy will first call the pre() function with the current text. You can return any text you like.

Next, the perLine() function will be called for every line in the text. Return each line.

Last, post() will be called.

All three functions are optional. And each one builds on the output from the previous one.