Action 和 Script 是 Drafts 最大的特色，根据这俩特性，我们可以写出很多 workflow 类的操作，来更方便的处理和分享你的笔记。
Prompt action steps allow you create custom dialogs which are run as part of an action to capture user input via buttons or an optional text field.
Key (default: prompt): The key is used to create Drafts tags based on the text input and/or buttons pressed when the prompt is displayed to the user. See discussion below.
Title (default: Prompt): A short title of the prompt window.
Message (default: blank): A longer text to appear in the prompt and explain the input desired, if necessary.
Include text field (default: OFF): If ON, the prompt will include a single line text input field which can be used to take information from the user.
Text field default (default: blank): The default text to place in the text field, if included. This field can contain Drafts tags, like
[[title]] to set a default value. After the prompt is displayed, the value in the text field will be accessible via the a
[[prompt_text]] tag in scripts and templates used in steps after the prompt step.
Include cancel button (default: ON): If ON, a “Cancel” button will automatically be included in the prompt. The cancel button will cancel execution of the action and no further steps after the prompt will be run.
Buttons (default: OK): A string listing buttons to include in the prompt. Multiple buttons can be listed by using the | character as a separator, for example “Red|Green|Blue” would result in three buttons appearing in the prompt. After the prompt is displayed, the name of the button tapped by the user will be available in the
[[prompt_button]] tag in scripts and templates used in steps after the prompt step.
Prompts use Drafts tagging system to expose values from the prompt for use in action steps which occur after the prompt in the same action. The tags used are constructed using the “Key” value for the prompt. If using a single prompt in an action, the default “prompt” key is fine and will result in two new tags being available:
[[prompt_button]] : will contain the name of the button pressed in the prompt.
[[prompt_text]] : will contain the text in the optional text field.
If using multiple prompts in the same action, you can distinguish results by the changing the “Key” on the prompt steps. A key of “myselection” will result in tags
These tags can then be used in subsequent steps. A common application would be to prompt for a filename to use, then use the
[[prompt_text]] tag as the filename value in a Dropbox step. The Save to Dropbox as… action in the directory demonstrates this example.
If you wish to use the results of prompt in an script action step, you can retrieve the values using the “draft.getTag('tagName');' function.
Have more questions? Submit a request
Script action steps allow you to manipulate a draft, and perform basic logical operations effecting the action being performed, and the draft itself. The scope of what is possible with Script steps is limited, they are not intended to be a full scale scripting engine.
The library of special Drafts functions will likely grow over time, but is basic for the initial release.
The best way to start learning about scripting in Drafts is to download some examples from the Drafts Action Directory.
Properties on the draft object can be manipulated within the script. For example, the “content” property contains the text of the draft, and assigning a new string value to “draft.content” will alter that value.
Changes to the “draft” object by the script step will exist for the life of the current action, unless a call to “draft.commit()” is made at the end of the script step to persist these changes. This means you can safely manipulate the draft content in ways which you only wish to apply to action steps occurring after the script step within the same action. For example, performing a Regular Expression search to pull all links from a draft and list them. A script step at the beginning of the Action could create this list, set “draft.content” to the new string, then a second action step could save that result to a file in Dropbox – all without changing the original draft.
If you want to save the changes, just call “commit(draft)” in the script step.
content: The text content of the draft.
createdDate: Date the draft was created.
createdLatitude/createdLongitude: Location coordinates of draft creation.
modifiedDate: Date the content of the draft was last changed.
modifiedLatitude/modifiedLongitude: Location coordinates for last modification of draft content.
accessedDate: Date the draft was last opened, even if the content was not changed.
selectionStart: The location in the string where the last cursor selection in the editor started.
selectionLength: The length (in characters) of the last cursor selection in the editor.
archived: Boolean value to get/set whether the draft has been archived.
flagged: Boolean value to get/set the flagged status of the draft.
defineTag(tagName, tagString) : Define a custom template tag for use in action steps happening later in the same action. For example, a script step could calculate the number of words in a draft, then call “draft.defineTag(“wordCount”,100)”. Action steps occurring after the script step in the same action would be able to use “
[[wordCount]]” as a tag in their templates and have it evaluated to the value defined in the script step.
getTag(tagName) : Returns the current value of a custom tag defined elsewhere, for example in a prompt step in the same action, or in another script step.
processTemplate(template) : Runs the passed template string through the Drafts template engine, expanding any standard
[] drafts tags, encoding, markdown and even expanding fenced TextExpander snippets.
commit(draft) : Persist changes made to the draft object to the database. By default, changes will only exist in the life span of the current action, calling commit() will permanently update the draft.
getClipboard(): Returns current contents of the system clipboard.
setClipboard(string): Set the system clipboard to the string passed.
stopAction() : Set a flag to stop further execution of steps after the current script step within the Action. This can be used to validate inputs in a script, and cancel execution of the action if it does not fit the requirements.
cancelAction() : Similar to 'stopAction' but will stop further execution of the action without an error.
markdown(string, useXHTML) : Returns string converted to HTML through the Markdown engine. If “useXHTML” is true, output will be XHTML complaint (useful for Evernote ENML compatibility), otherwise HTML5 output will be used.
encodeHTMLEntities(string) : Encodes the string to HTML safe entities, e.g. converting & to &
decodeHTMLEntities(string) : Decodes HTML entities. Have more questions? Submit a request