Peter - How in the heck do you parse the Calculations fields?

Peter - I’ve been trying to do some reverse engineering, parsing your Plists. I’ve figured out the vast majority of it. But how do you parse Calculation fields?

Variables in these fields don’t need “%”, so you don’t have that as a delimiter. Variable names can have embedded spaces, so you can’t delimit by spaces. There are of course all the characters that can’t be used in variable names, so those are delimiters, but you have functions like MIN(), TIME, etc, and operators like MOD. And what happens if someone has a variable name with spaces in it, and the last part is MOD or something like that?

I’m trying to work out how to find every variable that is used in a macro. I realize there are some things I’ll never get, especially like dynamic references, But I can get most everything, if I know how to parse Calculation fields.

And I know it’s not your job to help people reverse engineer your code, but any insights you care to share would be appreciated. Thanks.

Variables and function names are from the same space (ie, letter followed by alphanumeric or space).

“Wombat MOD Kangaroo” would be a valid variable name, so MOD, PI, SQRT, XOR, OR, AND are special cased within symbol names.
So variable names cannot legitimately include those sub-tokens. Since functions are case sensitive and capitalized, they would have to be included, with spaces, in capitals, so in practice it doesn’t happen.

Good luck, Keyboard Maestro has a full recursive descent calculation parser built in (I’ve written compilers and calculation engines before, so that stuff is easy for me).

Should have know.

So then, I guess you need to expose that for use rom the outside, then, right? :slight_smile:


Just as a matter of curiosity, may I ask why you want this?

Sure. It started out with me wanting to be able to automatically generate “cleanup variables” actions. And for this purpose, it’s really not an issue if I can’t parse Calculation fields, because you can’t set a variable inside a calculation, so chances are, if it’s a variable to be cleaned up, it’s already referenced elsewhere in an action that’s easier to parse.

It’s morphed into something to help make sure that if I rename a variable in a macro, I caught it everywhere.

Then I thought I might be able to create something to do a search and replace of variable names within a macro. This might be possible even without parsing calculation fields - it would just require the user’s input for these things.

So I’m just trying to see what my limitations are. If I could handle almost every situation, it could be useful for other things, too - things I haven’t thought of yet, but would undoubtedly come up if I had the tool.

I welcome your thoughts.

This is not as easy as a true find/replace, but I just want to make sure you are aware that you can search for variable names in the KM Editor Search box, and it will highlight (in blue for me) every action that uses that variable.

1 Like

I guess I didn’t realize that. Pretty cool. True, its not everything I want but that doesn’t mean it won’t be useful. Thanks!

The calculation engine is exposed from the outside via the calculate AppleScript command in the Keyboard Maestro Engine.

But no, the syntax parsing is not exposed.

1 Like

Yeah, I figured as much.

But one thing I’ve learned about KM: Ask. I’m constantly amazed at the things it’s capable of, that I didn’t even imagine it could do.

1 Like