I’ve got a subroutine that returns 1 or 0. I’d like to use the return value in a calculation.
That’s fairly easy, but it’s a two-step procedure and that seems a little inelegant to me. If we could drop a subroutine call into a calculation field—or a text field for that matter—that would be slick!
Can anyone suggest a clever way to address this? Thanks.
I've been wanting that for years, maybe decades. Let me know if you can solve it.
Implementing that in KM would be possible, in theory, but one of the side effects is that order of operation in a calculation could now change the result of the calculation. That wouldn't be clean. And The Architect likes "clean."
However maybe I do have a clever idea. (This idea may have to be modified a little if your subroutine has parameters, which you didn't specify.) I think it would be possible for you (or anyone) to create a plug-in action that contains a few parameters. Like this:
The name of the subroutine
The name of the variable you want to set
The calculation that you want to evaluate (with some symbol used to insert the value returned by the subroutine)
While this isn't a perfect idea, it does solve part of your problem, in which you said it was a "two-step procedure". My idea reduces the work to a single step, instead of two steps.
I've never written a plug-in action, but maybe I could try, with a little help from AI.
Since I haven't done it before, I'm not sure how much work it will be.
Although I don't have your particular use-case, here's my situation. I often have code that says, "1. Find this Image on screen. 2. If there's an image, call this macro, else do nothing." (I wish it were possible for Find Image to return an error if there's no image found, which would simplify my problem, but I don't think it can do that.)
I think I should try to create a plug-in that implements that idea for me in a single action.
Another option would be to move the entire calculation in an AppleScript action, within which you could use KM tokens, call your subroutine, etc.
But, aside from it being one action rather than two, that's way less elegant calling the sub then using the result!
That's probably the least of the problems here. After all, there's a set order of precedence in calculations and this would, presumably, be a function -- EXECUTESUBROUTINE().
But this will get messy as soon as you have subroutine parameters, which need to be referenced by name (or maybe ID). So the calling a sub called "SUB - Add Two Numbers" with two parameters would look something like:
EXECUTESUBROUTINE(SUB - Add Two Numbers, firstNum:Local_a, secondNum:Local_b)
...which will quickly bloat your calculation fields.
I've since learned that KM Plug-ins are 100% external scripts, and therefore cannot use Find Image or any other internal KM Engine action. So I can't address my issue with plug-ins.
Calculations and Token text expansion have to be able to return a result immediately (it's stretched a bit for some AppleScript requests which assume that the AppleScript will respond more or less immediately - and bad things happen if that assumption is violated).
So macro actions could never be used as a way to provide return values for calculations or token text.
That said, it would be nice to have a way to add your own tokens and functions, so it's something that is on my radar, but it would have to be done in quite a different way to just using macro actions, some solutions might be:
A whole new method of specifying them
Use an existing language (JavaScript/AppleScript/etc - but that has timing problems)
Use a subset of allowable actions/conditions.
But there is definitely no easy way of doing this, so it's not something that is likely to happen any time soon.
Even as I was asking about this, I was giving some thought how to make any implementation as general and versatile as possible. Good to know that you’re way ahead of me Peter.
As @Nige_S suggested: “Get over yourself and use the two-step call!” (Paraphrasing, of course!)