Text soap (clean text) macro does not work. Going bananas

@DanThomas
Hello,
The following macro was created by Dan Thomas. I am sure that it is perfect, and it is entirely my fault that I cannot make it work.
Procedure:
1- I highlight text, then copy to the clipboard
2- I run the macro
3- I expect to see the stripped text in the final display window. Instead I see the content of the clipboard, ie no search and replace.
I can't make sense of it because if I follow the value of the variable plaintext, from ➤ clipboard ➤ search and replace ➤ display in window, I should get a stripped version.
I will add the bells and whistles at the end. I just want to make the core of the macro run for the moment.
thanks very much !

Please provide a real-world example of the source data (the text you are highlighting) and the desired result after the macro has run.

1 Like

Thank you very much for looking into my question:

1- the initial text is:
Annotations of download.truelaunchbar.com
highlight [page 1]: True Launch Bar
highlight [page 8]: Creating shortcuts using Drag&Drop
highlight [page 9]: Creating plugins
highlight [page 11]: create a virtual folder, click on the “Browse…
highlight [page 12]: True Launch Bar supports all icon sizes. By default,
It is in BBEdit, so should be plain text.

I tested the BBEdit search/replace (grep) which works fine, so the expression (Highlight [page [^]]*]: ) must be correct.
result:
Annotations of download.truelaunchbar.com
True Launch Bar
Creating shortcuts using Drag&Drop
Creating plugins
create a virtual folder, click on the “Browse…
True Launch Bar supports all icon sizes. By default,

2- with the KBM macro, the output is identical to the input. At least I know that the final window does display clipboard contents

thanks very much for your time and help

I think I see the problem. Your regex is searching for “Highlight,” with a capital “H”, but your sample text is “highlight,” with a lowercase “h.” Your macro and regex should work fine in KBM once you change the Search and Replace Variable's search action to use case-insensitive regex (in your macro screenshot, the search action is set to case sensitive). This is the same reason why, again going from your screenshots, your search worked in BBEdit but not in KBM; because BBEdit’s grep search wasn’t set to be case sensitive.

1 Like

Fantastic ! It works now. Thank you so much !

If you don't mind I would have 2 follow-up questions:

1-I need to do multiple replacements, all similar to the one below
in the example, the expression used was (highlight [page [^]]]: )
I want to do the same with:
(text [page [^]]
]: )
(underline [page [^]]*]: )
Should I create a box as per below for each?

2- would you happen to know the syntax to delete empty lines?

thanks again very much

Glad it worked! To answer your other questions:

  1. It looks like one thing the text you want to delete has in common is that each line begins with a word (either “highlight,” “text,” or “underline” in your examples) followed by "[page [NUMBER]]: ". If that’s the case, you could try this catch-all regex which should match everything before "[page [NUMBER]]: ":

^\w+ \[page [^\]]*\]:

Try replacing your current (Highlight \[page [^\]]*\]: ) regex with that and see if it doesn’t work to delete lines that start with text and underline as well.

  1. I do indeed. There are, of course, multiple ways to do so, as there often are with regex, but @JMichaelTX recently showed me a good simple one that is proven to work well in KM:

(?m)^\n

1 Like

thank you for your answer and suggestion.

1- ^\w+ [page [^]]*]: It works fine, that you

2- thank you for the delete line suggestion. How would you manage multiple different replaces including the delete line: one box as per below for each ?

the delete empty line does not work either. Text the same. thanks

Sorry, I should have thought to include the (?m) flag before the catch-all regex as well. The delete empty line regex is working fine for me in my test macro though:

Text Soap.kmmacros (2.3 KB)

[quote="ronald, post:7, topic:7379"]
thank you for the delete line suggestion. How would you manage multiple different replaces including the delete line: one box as per below for each ?
[/quote]

I'm afraid I'm not entirely sure what you meant by this, but if it was "How do I use multiple search-and-replace patterns for the same variable?" then hopefully my sample macro answers that question!

1 Like

yes, your sample macro answers the question: multiple sequential search and replace box.
Thank you so much !!

1 Like

Is there any way to pass on (or return) the variable PlainText to the clipboard at the end ?
My intention is to:

  • add select all + copy at the beginning of the macro
  • finish by pasting PlainText
    To replace all with soaped text. Or is there is nicer way to do it?
    thanks again

Happy to help! And yes, those actions should work just fine to return the soaped text to the original document. As you guessed, you can just use an “Insert Text by Pasting” action that contains the PlainText variable for the last step.

1 Like

thanks very much and have a nice day!

1 Like

sorry, the last part does not work. I think that there is a step missing. thank you

You mean it’s failing to paste the cleaned-up PlainText variable? From what I can tell, it should work fine given the last step you’ve screenshotted; it certainly works when I duplicate it in my sample macro. I don’t suppose it’s failing to work because you haven’t yet added the “Select All” “Copy” actions at the beginning of the macro, is it? If that’s not why, the more specific details you can offer about what you’re doing, what the expected result is, and what results you’re actually getting, the easier it is for us to figure out what’s going wrong.

1 Like

you are right. Very sorry. I am officially a complete idiot. So sorry to have taken your time.
By the way, the macro you created is extremely useful to clean up annotations summaries in a PDF.
Just in case you wanted to add it to the KBM library.

Heh, no worries! We’ve all made facepalming mistakes like that at one point or another :slightly_smiling_face:
One of this forum’s whole reasons for existence is to help each other out, so don’t be afraid to ask more questions in the future if you’re stuck :slightly_smiling_face:

1 Like

thank you very much .

In the meantime another problem came up:

The following expression works fine when the annotations are made on an iPad.
(?m)^\w+ [page [^]]*]:

I just discovered that the syntax of annotations with the same app but on a MAC look different:
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
Highlight [133]: Click Options to select additional options for controlling which target items will be processed. To process all the files in subfolders of each target folder, mark the Search Nested Folders checkbox.

Underline [133]: You can also choose to process only text files or to process all file types. If you have graphics or other types of files in the target folders, you should restrict processing to only text files. This setting works in addition

Text [133]: testing

Note: I added the bold
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

So basically, there is a word of variable size (Highlight, Underline, Text) followed by a single space, the page number in brackets, then ‘:’ and a single space

How do you suggest I modify the expression (?m)^\w+ [page [^]]*]:
I assume that because the fixed word Page is missing, it may require multiple search replace, unless one can define everything before [133]: keeping in mind that 133 is a variable page number.

thanks very much

Okay, it now seems that while the text within the brackets may change, the brackets themselves (still followed by a word at the start of the line) are still always present, so that means we can ignore the "page" part of the previous regex entirely and modify the pattern to capture everything within brackets after a word at the start of a line, regardless of whether it's a word, a number, or any combination:

(?m)^\w+ \[[^\]]*\]:

1 Like

Fantastic. All works fine. thank you so much !!!

1 Like