Help creating a filter macro

As a first exercise, I'm trying to remove comments from lines of code that were copied to the system clipboard, then place the uncommented code back on the system clipboard.
My macro is based loosely on this post:

It usually does nothing, or it spews text of repeated lines, text that I previously placed in the macro for troubleshooting, etc. (as can be seen in the screen shot, the 'aaaaaaaaaaThis is some text...' in the next to the last action).
Thanks for any insight.

(Checked my regexp on some sample code at regex101.com using the PCRE flavor, and it works fine.)

Duplicated this macro to create a digits to time filter (i.e. 0100 → 01:00). Ran it on some sample text, and the text on the clipboard became text from the previous macro. Something strange going on here.

In this image, the text I right-clicked and ran the filter on became the text at the top of the clipboard history.

And here's the filter. Note the same aberrant text as the filter discussed in my previous post.

If you will post your exact source text, in a KM Code Block, then I will try to help.
Also, it would be most helpful for you to post the actual Regex101 snippet link, rather than just the main web site.

Thanks for your help.

% x=(1:20)';
% y=randn(20,1);
% [bhat p wh se ci t_stat]=robust_fit(x,y,1);
% figure;plot(x,y,'.'); hold on; plot(x,bhat(1)+bhat(2).*x,'r')
% plot(x,[ones(length(x),1) x]*(([ones(length(x),1) x]'*[ones(length(x),1) x])\([ones(length(x),1) x]'*y)),'k')
% legend('Data','Robust','Non-robust')

Still don't understand the 'aaaaaThis is some text', as it is not in the macro, and is not the current text on the System Clipboard.

Actually, according to your screenshot, that exact data is on the Clipboard:

I'll take a look at your problem tomorrow.

That was true when the first screenshot was made.
As you can see below, it's not the case now, yet the 'aaaaaaaThis is some text' persists.

I don't know how you are running your macro, but the Clipboard screen shot you show contains an IMAGE as the FIRST Clipboard, which is the System Clipboard, which is what will be used in your macro.

My point is simply that the text doesn't change, no matter what is on the clipboard.

This Regex should work in a KM Search and Replace action.

Search for:
(?m)^(\h*)[%#\/]+(\h*)

Replace with:
\1\2

For Regex Details, see regex101: build, test, and debug regex

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Remove Code Comment Characters [Example]

-~~~ VER: 1.0    2020-04-27 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Remove Code Comment Characters [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Example Output


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Remove Code Comment Characters

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

REFERENCE:

  1. Help creating a filter macro
  2. regex101: build, test, and debug regex

REQUIRES:

  1. KM 8.0.2+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
    .
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • ALL Actions that are shown in the magenta color

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

Thanks, @JMichaelTX!

1 Like