Create a macro to delete blank lines

Hello,
I created a macro which copies outlines created in outlineedit into a word processing app.
All works fine except that the pasted outline contains a lot of blank lines.
What would be the simplest way to delete those blank lines and compact the text?
Would replace all in Markdown with RegEx be best? Would someone know the RegEx expression I should use? I imagine that I should run the action after the copy and before the paste, so that the blank lines are deleted within the clipbboard?
example of the pasted text below.
thanks very much

〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

this is a test

  • item 1

  • item 2

       ⁃ testing again
    
  • item 3

       ⁃ sub item 3
    
       ⁃ sub item 3 bis
    
  • item 4

1 Like

You’re on the right track. Add a “Search and Replace (system) Clipboard” action in between the “Copy” and “Paste” actions that uses this regex: (?m)^\n
to search, while leaving the “Replace” field blank (and remember to set the search action to use regular expressions, not string matching).

1 Like

thanks very much

1 Like

So... obviously I bungled and it does not work.
Please note that I checked that the first action Copy as Text to Clipboard works fine, so that's not the problem.
The problem seems to be with the blank line deletion because if I paste at the end, I get the entire text including the blank lines as per below
thank you

this is a test

  • item 1

  • item 2

       ⁃ testing again
    
  • item 3

       ⁃ sub item 3
    
       ⁃ sub item 3 bis
    
  • item 4

  • item 5

Without seeing your full macro I can’t say for sure, but I’m guessing this is happening because you aren’t pasting the PlainText variable once it’s been searched and replaced. You don’t need to set the clipboard’s contents to a variable for this kind of text filtering; you can search and replace the clipboard directly after the initial “Copy as Text to Clipboard” menu action. If you do that, you don’t need to worry about pasting the right variable, as a simple “Paste” action will do the trick.

1 Like

thank you. what is the action to s/r the clipboard directly?

It's called exactly that, "Search and Replace Clipboard", and can be found in the "Clipboard" section of KM's actions:

1 Like

thanks very much.

1 Like

I am very sorry to bother you again.
I am going bananas.

The following image is the entire macro.
I tested actions 1 and 2, so they are not the problem.
The problem is that the search and replace is not taking place
below: snapshot of macro and what happens when I paste (Cmd-V) after running the macro

this is a test

  • item 1

  • item 2

       ⁃ testing again
    
  • item 3

       ⁃ sub item 3
    
       ⁃ sub item 3 bis
    
  • item 4

  • item 5

  • item 6

  • item 7

It could be that there are some whitespace characters in the "blank" lines. So, you might try this RegEx:
(?m)^\s*\n

If that does not fix the issue, then please post an example of the exact text that is copied in your first Action. We need that to test the RegEx.

1 Like

I tried what I believe is your sample data in this test macro, which should replicate what you have based on what you've told us:


And it gave me these results, as expected:

this is a test
item 1
item 2
⁃ testing again
item 3
⁃ sub item 3
⁃ sub item 3 bis
item 4
item 5
item 6
item 7
Assuming this sample data is the same as the one you were using, and you're still having trouble with the current version of the macro, try uploading the entire macro here for us to take a look at. You can do so in one of two ways, though I personally recommend doing both:

  1. Export the macro by selecting it in the editor, then going to File > Export Macros…
    Once you've done that, you can just drag the resulting macro file into the text field of a new post to upload it to the forum.

  2. Copy the full macro as an image while it is selected in the editor by going to Edit > Copy as > Copy as Image, then paste (or drag from a clipboard history viewer, such as KM's built-in one) into the text field of a new post.

Once you've done either (though again, preferably both) of those things, and confirmed and/or shared a representative sample of the kind of data you're working with, we should be able to get your issues straightened out.

1 Like

I will do further testing. thanks very much for your reply.

@gglick
I would like to thank both of you very much for your input and help.
I have been doing some testing and have a crazy question.
The macro works fine if I copy the test above into the clipboard (Cmd-C) and run the macro. My conclusion is that the macro works, the text in the clipboard is processed correctly by both your RegEx S/R suggestions.
What is funny is that the macro does not work when the copy is done in outlineedit via the Menu as per below (there is no concept of Cmd-C with this app).
Crazy question: are there different clipboards. Could this app put the text in a different clipboard? It does not seem to make sense because I can paste into another app with Cmd-V, no problem.
thanks again very much

Please do a File > Copy as Text in your app, and then paste into a KM forum post using a code block, which uses 3 backticks ` on a line before and after the text.

--- Paste your text here ---

By putting the text in a code block it will preserve all of the special characters (if any) that are in the text.

1 Like

thanks very much for your time and patience! I pasted text below with ```before and after

testing

- test 1

- test 3

         ⁃ test 4

                  • testing

                           ◦ test 2

         ⁃ tester

- retest

         ⁃ test


```

Hmm, I don't see much in the way of blank lines on this latest sample data. By any chance, when you said "blank lines," did you mean the white space on lines like "test 4" "testing" and so on? Because that's a different regex altogether, and would explain why the one to delete completely blank lines isn't giving you the results you want. Try adding a new "Search and Replace Clipboard" action to your macro after the copy and before the paste that uses this regex:

(?m)^\s+

and looks like this:


Doing so should then give you results like this:

testing

  • test 1
  • test 3
    ⁃ test 4
    • testing
    ◦ test 2
    ⁃ tester
  • retest
    ⁃ test
1 Like

I suspect your text may contain special characters for line breaks.

Here's a new macro that replaces all of these special line breaks with \n, and removes the "blank" lines.

For your example text above, heres the result:

Give this a try a let us know if it works for you now.
The SEARCH regex is (including the line breaks shown):
\ [ \t]*\ {1,}

The REPLACE regex is:
\n

For RegEx details, see:

##Macro Library   Remove Blank Lines that use Special Return Char


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/f/f313359cb0dec07232a8917daadbcc208be6de85.kmmacros">Remove Blank Lines that use Special Return Char.kmmacros</a> (2.7 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---


<img src="/uploads/default/original/2X/e/eecc56b51a5fbec48643afdc56f6e007421c0970.png" width="474" height="906">
1 Like

No, not at all !. I would lose the essential outlining created by Outlineedit.
thanks very much for your input.

1 Like

thank you very much.

We are on the right track. I am very grateful.

please find below the initial outline and the final window displayed by the macro, so 't's working perfectly, except for the fact that I need to add a step to 'send' the cleaned text back to the clipboard. I am starting to wonder if that was the problem from the beginning, but I have so many versions and so many disabled actions that it is hard to reconstitute the past.

With your macro, the text is cleaned but the clipboard itself remains unchanged, and I don't need the original spaced outline in the clipboard

Would you be so kind as to suggest how to replace the clipboard with test_source_data at the end?

thanks again

Simple:

See Set Clipboard to Text action (KM Wiki).

1 Like