Delete last four lines

Hi,

I’m copy a text from the iOS books app and always get 4 lines of text with reference of book etc. at the end of the draft. I like to delete the four last lines via a script. I found the „delete lines“ action where the cursor is in the line but I have no idea how to get this for the last four lines. Maybe someone can point me to a side where I can find some information or a similar script.

Regards
Mike

This little bit of JavaScript should do the job if you place it in a script step in a Drafts action.

draft.content = draft.processTemplate("[[line|1.." + (draft.content.split(/\n/).length - 4) + "]]");
draft.update();

The first line splits the draft by newline characters, counts the number of lines, subtracts 4 from it, then uses that to build a ‘line’ template. The ‘line’ template is evaluated to return the 1st to 4 from the last line lines of draft content. The drafts content is set equal to that … effectively knocking off the last 4 lines of the content.

The second line simply forces the draft to be updated.

Ref.

Hope that helps.

3 Likes

Great. Thank you. Works like a charm.

I’d love to see the [[line]] template support negative numbers for grabbing from the end of the draft. So then this could be solved with:

draft.content = draft.processTemplate('[[line|1..-4]]')
draft.update()

Instead, this code crashes Drafts, :frowning_face:

I’ll take a look at that…obviously should not crash.

1 Like

If you want to avoid the (admittedly probably minor) overhead of processTemplate(), you could just do this:

draft.content = draft.content.split(/\n/).slice(0, -4).join('\n')
draft.update()
1 Like

FYI, fixed the negative index crash and added support for negative index in the [[line]] tag for the 5.5 release.

5 Likes