Delete lines from a Draft

Hello, I’m trying to build an action to delete lines 2 and 3 from a draft (action executed by the new Apple Shortcut app) but I don’t know how (and if it’s possible)

Thanks for the help

This line of script should do the trick.

draft.content = draft.processTemplate("[[title]]") + "\n" + draft.processTemplate("[[line|4..]]");

Hope that helps.

1 Like

Hey there–I have a similar issue and I think your script gets me close, but not quite all the way – I’d like to everything except lines 1 & 2 from a draft. The action would likewise be triggered by Shortcuts. Any chance you can help with that?

Use this template tag to return the third line onwards.

[[line|3..]]

Triggering an action from Shortcuts is just a case of using the Drafts URL scheme. Take a look at open and runaction.

An alternative, though not as neat, would be to use get and strip the first two lines in Shortcuts - e.g. by splitting the returned string by newline and then recombining it save for the first two, or a bit of regular expression use to remove the first two lines (or remove first line and then repeat on that result).

Hope that helps.

Hey, I’m sorry, I somehow had a typo and left out the most important word of my sentence–I would like to delete everything except for the first two lines. The intent would be to use the “open” to grab all contents (to process in Shortcuts) and then using open & run action to delete everything except the first two lines.

draft.content = draft.processTemplate("[[line|3..]]") + "\n"

Is that right?

Yes. That should do it, except you have no need for adding a newline at the end each time, do you?

Assuming not, this should suffice.

draft.content = draft.processTemplate("[[line|3..]]")

To clarify how the tag ranges work…they specify ranges of lines. You want to keep the first two lines, so you want this:

draft.content = draft.processTemplate(“[[line|..2]]”);
draft.update(); // to save the changes

The line tag can take a specific line number, or a range from a line number to another. If the first or last line number is omitted, it is assumed to be the first or last, examples:

[[line|2..5]]: Lines 2 through 5
[[line|..2]]: The first two lines
[[line|3..]]: The third line to the end of the draft

Negative indexes are supported and count back from the end, so:

[[line|2..-3]]: The second line through the third to last line.

2 Likes

Thanks Greg! I feel like an idiot because I know the problem is my end, but copy & pasting your code isn’t working—that is, when I go to run it, it fails and doesn’t do anything to the draft (just the red drop-down alert). What have I got wrong?

If you cut and paste, the forum added smart quotes. Fix those to be straight quotes.