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.

// get the currently selected text
let text = editor.getSelectedText()
// replace selected text with uppercased version
editor.setSelectedText(text.toUppercase())

Behavior and Interface

isActive: boolean

Is editor current focused for editing.

linkModeEnabled: boolean

Access or set current link mode status.

pinningEnabled: boolean

Access or set current pinning status for editor.

preferredTabString: string

Returns the current tab string is use. This could be 2 spaces, 4 spaces, or \t depending on the editor preferences for the current syntax. Useful in actions, such as indent/outdent actions, which need to insert or remove indentation and want to match the options of the current syntax.

recentDrafts: [Draft]

Array of recent drafts. This is the same list as used in the navigation features of the editor, and is in reverse order, so that the first index in the array is the previous draft loaded in the editor.

typewriterScrollingEnabled: boolean

Access or set current typewriter scrolling status.

  • Request focus for the editor. This will dismiss other views and show the keyboard on the currently loaded draft. Useful if an action opens user interface elements or otherwise causes the editor to resign focus and you would like to return to editing at the end of the action's execution.

    Returns void

  • Opens the arrange mode view with the passed text for arranging. Returns the arranged text if the user makes changes and taps "Done", the original text if the user cancels.

    Parameters

    • text: string

      The text to arrange

    Returns string

    String containing result of arrange. If user cancels, it will be the same as the original text passed.

  • Resign focus for the editor. If the editor text view is currently focused for editing (e.g. showing keyboard), resign focus.

    Returns void

  • Open dictation interface, and return the result as a string. The string will be empty if user cancels.

    Parameters

    • Optionallocale: string

      the preferred locale can be passed in the format "en-US" (U.S. English), "it-IT" (Italian-Italian), "es-MX" (Mexican Spanish), etc.

    Returns string

    The accepted dictation text.

  • Loads an existing draft into the editor.

    Parameters

    Returns void

  • Creates a new blank draft and loads it into the editor.

    Returns void

  • Load next draft in draft list in editor.

    Returns void

  • Load previous draft in draft list in editor.

    Returns void

  • Apply redo action to editor, if one is available.

    Returns void

  • Save any current changes to the draft.

    Returns void

  • Open document scanning camera to scan documents and OCR, returning the result as a string. The string will be empty if user cancels, or document scanning is not available.

    Returns string

    The text recognized in the OCR of the scanned document.

  • Open arrange mode in editor. This is a non-blocking method and returns immediately. It is intended only to mimic the tapping of the arrange button. Use editor.arrange(text) to wait for a result.

    Returns void

  • Open dictation mode in editor. This is a non-blocking method and returns immediately. It is intended only to mimic the tapping of the dictate button. Use editor.dictate() to wait for a result and use it in further scripting.

    Returns void

  • Open find mode in editor.

    Parameters

    • OptionalpreferAdvancedFind: boolean

      If true, skip native find implementation and option Drafts advanced find directly.

    Returns void

  • Open Opens audio transcription window. If file is selected and transcribed, returns text.

    Returns string

    The text recognized in the audio transcription.

  • Apply undo action to editor, if one is available.

    Returns void

Deprecated

focusModeEnabled: boolean

replaced by pinningEnabled.

Other

draft: Draft

The Draft object currently loaded in the editor.

linkedItems: [linkedItem]

Convenience method to return the linked items in the text, as located by the syntax definitions' linkDefinitions. In Markdown syntaxes, these map to [[wiki-style]] cross-links.

navigationMarkers: [navigationMarker]

Array of navigation markers in the text. Navigation markers are defined by the syntax definition in use in the editor, and are used in the Navigation feature.

urls: [string]

Convenience method to scan the text for valid URLs, and return all found URLs as an array. This will return valid full URL strings - both for http(s) and custom URLs found in the text.

  • Expand the range provided to the nearest beginning and end of the lines it encompasses.

    Parameters

    • location: number
    • length: number

    Returns textRange

  • Get the current selected text range extended to the beginning and end of the lines it encompasses.

    Returns textRange

  • Get text range that was last selected.

    Returns string

  • Get the full text currently loaded in the editor.

    Returns string

  • Get the substring in a range from the text in the editor.

    Parameters

    • location: number
    • length: number

    Returns string

  • The next navigation marker in the editor, relative to the character location. This is a convenience method to assist in navigating by marker.

    Parameters

    • location: number

    Returns navigationMarker

  • The previous navigation marker in the editor, relative to the character location. This is a convenience method to assist in navigating by marker.

    Parameters

    • location: number

    Returns navigationMarker

  • Update the text selection in the editor by passing the start location and the length of the new selection.

    Parameters

    • location: number
    • length: number

    Returns void

  • Replace the contents of the last text selection with a string.

    Parameters

    • text: string

    Returns void

  • Replace the contents of the editor with a string.

    Parameters

    • text: string

    Returns void

  • Replace the text in the passed range with new text.

    Parameters

    • location: number
    • length: number
    • text: string

    Returns void

Tasks

completedTasks: [Task]

Array of completed task lines found in the content, based on active syntax definition for the draft. See Task documentation for usage details.

incompleteTasks: [Task]

Array of incomplete task lines found in the content, based on active syntax definition for the draft. See Task documentation for usage details.

tasks: [Task]

Array of task lines found in the content, based on active syntax definition for the draft. See Task documentation for usage details. Includes all found tasks, regardless of status.

  • Update the text representing the task to a next valid state as defined by syntax. If this task has only two states, this is effectively a toggle, if more than two states exist, the next state will be set, including cycling around to the initial state. Note that the task object is not updated to reflect changes made.

    Parameters

    Returns boolean

    boolean If true, advance was successful

  • Update the text representing the task to a completed state as defined by syntax. Note that the task object is not updated to reflect changes made.

    Parameters

    Returns boolean

    boolean If true, completion was successful

  • Update the text representing the task to a initial state as defined by syntax. Note that the task object is not updated to reflect changes made.

    Parameters

    Returns boolean

    boolean If true, reset was successful