用户工具

站点工具


侧边栏

Navigation

drafts

Drafts

快捷获取笔记,分享无处不在

介绍文章: 强大如怪物:顶级文本处理应用 Drafts 4 深度评测

Action 及 Script

Action 和 Script 是 Drafts 最大的特色,根据这俩特性,我们可以写出很多 workflow 类的操作,来更方便的处理和分享你的笔记。

Action Step: Prompt

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.

The configurable options for Prompt steps are:

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.

Keys, Tags and Multiple Prompts

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 [[myselection_button]] and [[myselection_text]], etc.

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

Action Step: Script

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.

Script steps are written in JavaScript and have access to all basic JavaScript library functions (including Regular Expressions) and a few special methods and objects created by Drafts.

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.

The "draft" Object

The current draft upon which the action is being performed is exposed to the script step JavaScript context as a “draft” object. The draft object has properties which correspond to the values of the draft. In some cases, not all of these properties have values – for example, if an action is being performed via URL scheme on a text without a draft stored in the Drafts database.

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.

”draft" Object Properties

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.

”draft" Object Functions

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.

Other Utility Functions

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.

alert(message) : Show message dialog with message, similar to standard Javascript browser alert.

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

应用实例

The Hit List ( prompt + script 给 The Hit List 添加多 Tag)

官网

更多详见:The Hit List

Drafts 的 prompt 相当于是个对话框,可以设置多个 button 来当选项,但是并不提供多选,所以要想给 The Hit List 添加多个 tag,我们只能添加多个 prompt,来实现,如下图设置:

drafts2thl设置1

设置 prompt:

设置 prompt

通过 Drafts 提供的自有 JS 函数来设置 prompt 的返回值:

通过 Drafts 提供的自有 JS 函数来设置 prompt 的返回值

将 prompt 的返回值通过 URL Scheme 发送给 THL

将 prompt 的返回值通过 URL Scheme 发送给 THL

这样在选择该 action 后会依次弹出两个对话框,选择你要添加的项就 OK 了,不添加就选 none,你可以设置更多的 prompt 来添加更多的 tag。

drafts.txt · 最后更改: 2015/12/30 23:29 由 dr.j

Bitnami