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
=
15.5
i.e.: remove the text, and then multiply each "line", and then add the lines together. Any suggestions...?
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.
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.)
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.
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
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.