This macro moves the mouse to the location on the main screen of the "word" that you pass to this macro. I generally use this approach to replace my Find Image actions when they attempt to locate a "word" on the screen. This means that, with no changes, this program can be used to locate a location on the screen even if the colours of the word changes (eg, DarkMode, LightMode, and more.) This program also reduces the size of your macro file, since it lets you reduce the number of images saved, which can even make the KM Editor more responsive.
The new Apple OCR code is very fast (about 600% as fast as the old OCR in KM v10, and MUCH more accurate) so this routine is viable as a means to move the mouse. This macro has two separate methods for locating a word. The first method, which is extremely fast, is that it "remembers" the location of any word, using a dictionary, and does a quick check to see if the word is still in the same location. If so, it is practically instantaneous. The second method it to do a modified "binary search" of the screen looking for the "word", and on a 2020 M1 Mac, this takes a variable amount of time between 5 and 10 seconds. That may seem slow, but in most cased its memory will be valid, and this 5-10 seconds will rarely have to be repeated.
This macro is written as a subroutine and it has two parameters: (1) the word, and (2) the approximate font size of the word. The second parameters helps the algorithm to know when to stop searching. Without that second value, it might end up stopping way too early and missing the correct location of the word.
I just rewrote this entire macro from scratch, in an hour or two, because my older version was using the OCR in KM v10, which meant that the full search tended to take 30-45 seconds, which was annoyingly slow. I also rewrote it because I knew I could make improvements. It feels much better now.
There is one small block of code that occurs twice in this macro. Generally I avoid that approach, but in this case I wanted to keep everything in one macro.
It's possible that this code contains a bug, so I may add updated versions here in the future.
Feel free to try it out, and see if it works for you. Please report any problems here.
At the end, your mouse is moved to the location of the word, and a small blue box is displayed for one second so that you can see how accurate the algorithm was. If you want to save that location or immediately click the mouse, that's up to you to do afterwards. If it does not find your word, it returns an error "2", or if it thinks it was close but wasn't sure the result was good enough, it will return a "1".
This macro can be used to see how the following features of KM can be used: Dictionaries, Screen Rectangles, OCR, Bisection algorithms, Subroutines with Return values.
MoveMouseToWord Macro (v1.0)
I've removed v1.0 from this message and replaced it with v1.1, see next post for an explanation and for the new, updated macro.