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?).
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.
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.
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.