Clear out words, multiplikation, addition


Disclaimer: I am not a programmer. I am not particularly good at making Macros in KM.

Right now I am using a "for each function" to copy numbers from clipboard and add them together. I got help putting together the macro from this forum. Now I need to "upgrade" the macro. this feels a bit complicated tbh. Is there a way to clear out "text" and then multiply and add from a "matrix".

I am helping a friend who is getting his data from a program where the output to the clipboard looks like this when pasted into "notes":

1 Week 0.5
12 Weeks 1
3 Weeks 1

Maybe it's not doable, but what I want is to add it together like this:

1 x 0.5
12 x 1
3 x 1


i.e.: remove the text, and then multiply each "line", and then add the lines together. Any suggestions...?

If it's always multiplication of the numbers in the line, and addition of the lines, then you could do something like this:

clip Calc.kmmacros (2.8 KB)

but I think there may already be some more general clipboard arithmetic macros on this forum.

Expand disclosure triangle to view JS source
return kmvar.local_SampleClip
.flatMap(x => {
    const s = x.trim();

    return 0 < s.length
        ? (() => {
            const multiplicandStrings = s.split(/\s*[x | \\*]\s*/u);

            return multiplicandStrings.some(isNaN)
                ? []
                : multiplicandStrings.reduce(
                    (a, v) => a * Number(v),
        : [];
    (a, x) => a + x,

Assuming the input string is always in the form of <n> Weeks <n>

The following bash script will work

while IFS= read -r line; do
    line_tot=`echo "$line" | awk '{print $1 * $NF}'`
    total=`echo "$line_tot + $total" | bc`
done <<< "$KMVAR_weeks"
echo $total

Keyboard Maestro Actions.kmactions (1.2 KB)

As you say you're not a programmer, I'm going to offer an alternative solution that works, but is way less efficient than the one presented by @ComplexPoint—their method will be much faster than this one, especially for larger numbers of rows. The version I'm going to present still has some level of complexity in one action, but might be easier for a non-programmer to follow.

Download Macro(s): __Multiply clipboard text.kmmacros (5.2 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.5
  • Keyboard Maestro v11.0.2

The one piece of complexity in my version is the regular expression search that finds the numbers on each row. The weird text in the search is used to find and capture a group (as denoted by the surrounding parentheses) at the start of each line (the carat) that is followed by a space, then whatever is between that space and the next space is found but ignored (not surrounded by parentheses), and finally, another capture group for anything from there to the end of the line (the dollar sign).

This puts the two numbers (the two capture groups) into two variables, which are first multiplied, then that value is added to a running grand total.

For this to work, the macro makes a very important assumption: The clipboard contains only text that matches the example you provided: Number-space-text-space-Number, with nothing else before or after the numbers, and no rows that contain just text.

If that's the case, this should work for you, but as noted, it's a very ineffecient way to do it, as it processes row by row. But it may be easier for you to follow along, as it's just a series of Keyboard Maestro actions without any real programming code. (The regular expression complexity is unavoidable, as it's the only way to extract the numbers.)



This worked, thanks!

However, the problem is the formatting of the original data. This is what causes my problems primarily. The data is copied from a program called "Movie Magic Budgeting", and I just can't for my life figure out how the data is formatted.

When I paste it into OSX Numbers, and then copy to system clipboard it works. At one point, it worked pasting into Text Edit, but not with your solution. My old hack was to open text edit - new doc - paste - copy - close etc.

Hi again, thanks everybody for chiming in.
It turns out that the "raw data" when converted to plain text in Text Edit looks like this:


What do you get if you do the following:

  1. Copy the text from the source app
  2. In Keyboard Maestro, make a new one-step macro:

Display Text in a Window: %SystemClipboard%

  1. Run the macro and copy/paste the output in the window here. I'm just curious as to what Keyboard Maestro sees directly from the source app.


Screenshot 2024-03-20 at 17.36.59

It works for any other text from other apps. But even a single digit or word from MMB does not work, which is weird. Since I CAN paste it into Text Edit for instance

EDIT: Just to be clear, if I copy straight from MMB into KM, I get nothing.
But If I copy from MMB and paste into TextEdit and then copy from TextEdit and paste into KM it works

Weird. If you copy from MMB, then paste into a text field (such as the Display Text input area), what do you get?


1 Like

Your solution is similar to mine, but I just want to paste my solution in here for your reference to see my variation, which I think is simpler.

Of course, he's having a new problem now, which I opine may relate to how macOS handles tables in clipboards. In his first post where he says what "notes" contains, I'm guessing that he's showing plain text but it was actually a table that was inserted into notes.

Let me check!

I like it! I didn't even think about writing out the formula in the regex then evaluating it—creative!


OK, well, clearly this is a fun problem. How about this solution:

  • Replace week with *
  • Replace consecutive numbers separated by white space with +
  • Ask Keyboard Maestro to calculate the result.



What I suggest is that we take a closer look at the pasteboard types which MMB is creating.

If you download and enable this macro:

Then you can:

  1. Copy some relevant material in MMB
  2. Run the macro above
  3. Show us here what it reveals (in a display text window) about the formats in the clipboard
1 Like

Good idea! This is what I get:)

Screenshot 2024-03-21 at 17.45.32

Cool, will try!

This actually worked great. I am running with this for now. I still need to go via TextEdit to use it, but that's fine for now.

Maybe an action like this

in the beginning of your macro could work instead of the TextEdit-Workaround?