Help replacing multiple consecutive blanks lines

I have some text that I’m sending to Drafts from Shortcuts.

I can’t seem to figure out a way to get Shortcuts to do this, so maybe Drafts can do it:

Given a large text file which has several blocks of text…

…and those blocks of text sometimes have multiple blank lines at the start or end (or both the start and end)…

…I would like to replace those blank lines with no more or less than ONE blank line.


Things I have already tried…

In Shortcuts, I have tried doing a search and replace for \n or \r or even ^$.

None of those did anything. It appears to me that (at least in the current version of Shortcuts (12.1, I think), there is no way to do a search/replace for a new line. Unless I missed it somehow, which I guess might be possible.


Things I have NOT already tried…

Anything related to JavaScript, because I don’t speak JavaScript (yet?).


Any help would be appreciated. Thanks!

An action with a single Script step of this may be what you’re looking for:

r = editor.getText().replace(/^\n+/, '\n');
r = r.replace(/\n+$/, '\n');
editor.setText(r);
1 Like

There’s also this one in the Drafts directory. Not instantly apparent from its title of “Single space,” but it does remove blank lines.

https://actions.getdrafts.com/a/1LF

1 Like

Ok, so here is where I admit that I’m not sure how to use this in Drafts. (99% of what I do in Drafts is just write something and then copy/paste it somewhere else, so this is new to me.)

I tried to create a “New Action” and gave it a name, then I put it into a “Run JavaScript” step.

Then I went back to the note that had the blank lines in it, and I tried to run the new action… and something popped down from the top of the screen to suggest that it ran, but nothing happened.

So… any idea what I might have missed?

Thanks! This comes very close, but it removes all of the blank lines. I want to leave one blank line, even if there were previously anywhere from 1-10 blank lines. (Sort of like what uniq does to text in a shell.)

Well, it doesn’t matter, because I misread your needs. That script got rid of extra blank lines at the beginning and end of a draft only, so it may not be surprising you didn’t see any change. Try this one instead. It’s simpler, and I think it actually does what you want.

https://actions.getdrafts.com/a/1Ql

2 Likes

Works perfectly! Thanks!

Purely to tie up a loose end, here’s an example of doing it in Shortcuts.

It actually uses the same regex approach as above, but also an additional piece to account for lines purely containing whitespace (e.g. a rogue tab or space) - resolving them down to empty lines and then processing the empty lines; the same approach can of course be applied in Drafts.

1 Like

Oh, that’s excellent. I love seeing how people are tackling problems in Shortcuts, too, since it’s so new.

Using a text field as a newline replacement was a very clever idea. I’m definitely going to be stealing that one!

Well, not exactly new :wink:

I’ve actually used the same newline ‘technique’ in Workflow workflows previously. For some time in fact (example).

Fair point. I have to admit that I didn’t pay much attention to Workflow as I was sure Apple would eventually kick it out of the App Store.

Oops. Got that one just a little bit wrong.

2 Likes