ACTION: Add Lines to List, v4.0

Untitled 1


Attribution

This action was inspired by Add to list plus by @dchar. Add to List plus is based on Add to List by @agiletortoise.

In addition to the inspiration, the Add to List plus JavaScript code that presents a dialog to select an existing list, or creates a new one, is used in this action. The viewListAfterSuccess code has similarly been copied.

The action dialog is created and evaluated using MGCheckListPrompt, by @mattgemmell.

Help regarding the Preview (v1.1) was provided by @sylumer, @agiletortoise, and @FlohGro. For more information: End an action, yet still honor the AFTER SUCCESS configuration? - Actions - Help & Questions - Drafts Community

@sylumer suggested that this action be callable from another action. He also provided the necessary guidance.

Overview

This action can be used to add lines from a source draft to target list, i.e., another draft (formatted using markdown). The target list can be: 1) hard-coded, 2) selected from all current lists, or 3) created by this action.

Target lists are drafts that are archived and tagged with a configured value (by default the tag is list). Source drafts can be anywhere in the Drafts application.

Features

  • All lines from the source draft are added individually to the target list.

  • A time prefix (YYYY-MM-DD HH:MM or HH:MM) can be included with each added line.

  • Added lines can be appended or prepended.

  • A horizontal rule (-–) can be added before each group of lines added to the target draft.

  • A date line (YYYY-MM-DD, DayOfWeek) can be included with each group of lines added to the target draft. If this option is selected, the added date and lines are enclosed by horizontal rules. (This action includes logic to prevent horizontal rules on two consecutive lines.)

  • After the target list is updated, it can be displayed in the Drafts editor and/or previewed (i.e., the markdown is rendered). Note: To preview a target list without changing it, run this action with an empty draft in the Drafts editor.

  • Blank lines above and below text in the source draft can be used or ignored. Similarly, interleaving blank lines can be used or ignored.

  • Several of the options mentioned above can be changed at runtime using an action dialog.

  • The default options can be superseded by using an action that calls this action via the Include Action step. The override options in the caller are the same as the options within this action except with an added leading underscore. For example _listTag can be used to override the option listTag. For an example caller, see: Add Lines to List (caller) | Drafts Directory.

Tested With

  • Drafts (iOS), Version 42.2.1 (415) // iOS Version 17.2.1/iPhone 15 Pro Max

  • Drafts (macOS), version 14.6.9 (14D100, Apple Silicon, sandboxed) // Sonoma 14.2.1 (23C71)/MacBookPro18,2

Version History

( expand / collapse )

1.0 - initial version

1.1
a. If the source draft is empty no content is added to the target list but it can still be loaded and/or previewed.
b. The method used to to preview the target list has been changed such that the action ends nornally; thus the action After Success configuration is honored.

2.0
a. The default options can now be superseded by using an action that calls this action via the Include Action step. The override options in the caller are the same as the options within this action except with an added leading underscore. For example _listTag can be used to override the option listTag. For an example caller, see: Add Lines to List (caller) | Drafts Directory.
b. The list dialog prompts and logic were refined.
c. Changed option excludeBlankLines to excludeInnerBlankLines.
d. The icon color was changed from green to red. By default, the icons of calling actions are green.
e. Bug fix: When addCurrentDateToEachBlock = true and prependLines = false, the closing horizontal rule was prepended. It is now appended.

3.0
Changed option addCurrentDateToEachLine to addCurrentTimeToEachLine. If true, a YYYY-MM-DD HH:MM: or a HH:MM: prefix is added to each line before it is added to the list. If option addCurrentDateToEachBlock = true, it will be the latter, HH:MM:.

4.0
a. If the editor is displaying a list, it is previewed (rather than displaying an error).
b. If a specified list is not found, there is an option to create it.
c. If running the action from a blank draft, can optionally open and/or preview the configured list. Added related settings: viewListIfDraftIsEmpty, previewListIfDraftIsEmpty, promptOptionsIfDraftIsEmpty.
d. Change the default value of newListHeading to ‘###’.
e. Added settings theListFontSize (default: 80%) and theListFontFamily (default “Courier New”, Courier, Monaco, “DejaVu Sans Mono”, monospace). The former affects all of the fonts in the draft list, whereas the latter only affects the list entries in the draft list.
f. The draft content send to the Preview is modified to add a \n before and after each HORIZONTAL_RULE. That enables the content to remain condensed (vertically) in the drafts editor, while producing a preview that works better when the ‘Copy Rich Text’ (macOS) option is used.
g. If todays ‘date group’ already exists, new draft lines are added to the existing group (rather than creating a duplicate group).
h. When adding blank lines, a prefix is no longer added to each blank line.
i. The following are properly rendered when a list item is previewed. The latter three were previously supported; the first two were added to this version.
- strike through
- ==highlighted==
- this is strong
- this is italic
- this is strong italic
j. Preview font and background colors modified to work better when using ‘Copy Rich Text’ (macOS). The colors vary depending on Editor > Appearance:
light: body { background: white; color: black; }
dark: body { background: white; color: black; }
k. When Preview is selected, the list markdown content, with one modification, is written to the system clipboard. The modification is that a newline is added before and after each HORIZONTAL_RULE.
l. Bug Fix: The current time is now properly based on localtime.




3 Likes

Greetings, Drafts Community!

I’m a relatively new subscriber to Drafts and this is my first contribution to the forum; it includes a link to the first action that I have uploaded to the Drafts Directory.

I would certainly appreciate feedback. This is the first time I’ve done anything substantial with JavaScript (lots of firsts; you’ve been warned :rofl:), thus I’d be particularly interested in any suggestion related to the embedded code. Thanks in advance!

1 Like

Jim …You are a gem! :smiling_face:

You managed to create a graceful single ‘action’ solution to something I have wanted for ages.

Thank you Sir

1 Like

This is really nice - is there a way (and I’m sure I’m just blinking over something obvious) to make the selections sticky. They always seem to go back to the default

1 Like

Hi, @daschles. Thanks for the feedback.

At the moment there is no way to make the options sticky, however, you can change the defaults. See the first Script step in the action for more information.

I’ve actually created copies of the action, each with different options (and renamed each based on the options). For example, in one case, I’ve hard-coded the list and bypassed the dialog. That enables me to update that list very quickly.

With all that said, I’ll consider adding the sticky feature when I get some spare time. Thanks again for the feedback!

Thanks. Actually, your suggestion to have different versions, including a hard-coded one, is absolutely perfect.

1 Like

Consider having an action X (this list action) that will use its own defaults unless others have been specified.

Then, create other actions (A, B, C) that each contain their own set of override defaults and that then includes action X.

This will mean that if action X is updated you do not need to duplicate it and build A, B, and C each time.

Action X should just require some tweaks to accommodate this.

1 Like

I’ve updated the OP with Version 1.1 of Add Lines to List:

  • If the source draft is empty no content is added to the target list but it can still be loaded and/or previewed.

  • The method used to to preview the target list has been changed such that the action ends nornally; thus the action After Success configuration is honored.

Note: Help regarding the Preview was provided by @sylumer, @agiletortoise, and @FlohGro. For more information: End an action, yet still honor the AFTER SUCCESS configuration? - Actions - Help & Questions - Drafts Community

Done!

@sylumer thanks for the suggestion and guidance.


I’ve updated the OP with Version 2.0 of Add Lines to List:

  • The default options can now be superseded by using an action that calls this action via the Include Action step. The override options in the caller are the same as the options within this action except with an added leading underscore. For example _listTag can be used to override the option listTag. For an example caller, see: Add Lines to List (caller) | Drafts Directory.

  • The list dialog prompts and logic were refined.

  • Changed option excludeBlankLines to excludeInnerBlankLines.

  • The icon color was changed from green to red. By default, the icons of calling actions are green.

  • Bug fix: When addCurrentDateToEachBlock = true and prependLines = false, the closing horizontal rule was prepended. It is now appended.

I’ve updated the OP with Version 3.0 of Add Lines to List:

Changed option addCurrentDateToEachLine to addCurrentTimeToEachLine. If true, a YYYY-MM-DD HH:MM: or a HH:MM: prefix is added to each line before it is added to the list. If option addCurrentDateToEachBlock = true, it will be the latter, HH:MM:.

I’ve updated the OP with Version 4.0 of Add Lines to List:

  • If the editor is displaying a list, it is previewed (rather than displaying an error).

  • If a specified list is not found, there is an option to create it.

  • If running the action from a blank draft, can optionally open and/or preview the configured list. Added related settings: viewListIfDraftIsEmpty, previewListIfDraftIsEmpty, promptOptionsIfDraftIsEmpty.

  • Change the default value of newListHeading to ‘###’.

  • Added settings theListFontSize (default: 80%) and theListFontFamily (default “Courier New”, Courier, Monaco, “DejaVu Sans Mono”, monospace). The former affects all of the fonts in the draft list, whereas the latter only affects the list entries in the draft list.

  • The draft content send to the Preview is modified to add a \n before and after each HORIZONTAL_RULE. That enables the content to remain condensed (vertically) in the drafts editor, while producing a preview that works better when the ‘Copy Rich Text’ (macOS) option is used.

  • If todays ‘date group’ already exists, new draft lines are added to the existing group (rather than creating a duplicate group).

  • When adding blank lines, a prefix is no longer added to each blank line.

  • The following are properly rendered when a list item is previewed. The latter three were previously supported; the first two were added to this version.

    • strike through
    • ==highlighted==
    • this is strong
    • this is italic
    • this is strong italic
  • Preview font and background colors modified to work better when using ‘Copy Rich Text’ (macOS). The colors vary depending on Editor > Appearance:
    light: body { background: white; color: black; }
    dark: body { background: white; color: black; }

  • When Preview is selected, the list markdown content, with one modification, is written to the system clipboard. The modification is that a newline is added before and after each HORIZONTAL_RULE.

  • Bug Fix: The current time is now properly based on localtime.

1 Like