Drafts Scripting Reference

Editor

A global “editor” object is available in all action scripts. This object allows manipulation of the main editing window in Drafts, altering the text, text selections, or loading a different draft into the editor, etc.

Typically scripting actions that work like custom keyboard commands and similar will utilize the editor functions to manipulate text.

NOTE: Generally speaking, editor methods are best used for quick text manipulations of the type used in the extended keyboard. Most substantial updates to draft content are better applied using the draft object.

Instance Properties

Instance Functions

Examples

var sel = editor.getSelectedText(); // retrieve selected text
var selRange = editor.getSelectedRange(); // retrieve range of that selection

if (!sel || sel.length == 0) {
  // if there was no selection, insert Markdown bold (**)
  editor.setSelectedText("**");
  // update the selected range to the end of the inserted text
  editor.setSelectedRange(selRange[0]+2,0);
}
else {
  // If there was a selection, wrap that text with Markdown bold (**) and replace selection
  editor.setSelectedText("**"+sel+"**");
  // Update the selected range to the end of the inserted text
  editor.setSelectedRange(selRange[0]+selRange[1]+4,0);
}