I have basic experience with KM on Mac. My existing Macros are very simple, but I'm now seeking to do something more complex...
My goal: Have KM watch for a message on the Mac screen that says "File server is shutting down" and initiate system shut down.
(Why this goal? Because when the electricity goes out the NAS will start its shut down process a few minutes after running on UPS battery. I want the Mac to see this happening, and initiate proper shut down of itself.)
I tried creating creating the macro using ChatGPT. I thought it was going great, until it wasn't
(To be fair, I shouldn't have included the shut down command right away. Should have done testing with a restart command instead.) The problem was that KM didn't wait for user input, it just initiated shut down immediately. I can't share the macro I created or troubleshoot it, because I had to revert my KM configuration to before this happened (using backup).
Considering I may have been doing all wrong with AI to begin with, I turn to you all for input.
That's the great thing about using OCR... it doesn't matter whether it's a notification or not, or where on the screen the text is. With OCR, it's this simple: (and it takes only a fraction of a second to run on my Mac.)
That's what I was trying to ask, in a very poor manner :). For as fast and reliable as OCR is, I will almost always prefer a triggerable event as a means of detecting the state change, if possible.
Thank you for the responses. And thank you, Airy, for the OCR Macro example. That looks just like the one I created previously. And I have created one again using Airy's example. But where I Get hung up, again, is having the action be conditional. I'm realizing that I forgot to mention that I want KM to wait 1 minute for the user to respond to a prompt. (Allowing the user to cancel shut down, or if the user doesn't respond within 1 minute, system shut down will happen.). How can I add this conditional delay?
And if it's not asking too much, is it practical to have a countdown clock for the user input? I understand that this might not be within KM's capability.
Better, as others have suggested, to tag off of the NAS's alert procedure. Can that run scripts (they often can)? So I'd look at an ssh command from the NAS that would run the KM macro, setting the NAS up for "passwordless ssh" into your Mac.
No point going further into that unless it's a route you want to take...
A quick'n'dirty route is to give your alert a 1 second timeout that doesn't cancel the macro, and keep looping through until you either get an answer or run out of time. Demo (which can certainly be improved on):
Here's one way to do that, with two macros. (It can also be done with Javascript in a custom HTML prompt, but this version seems more approachable.) The technique is covered here:
And here's my simplistic demo, which uses a backwards-counting progress bar:
The second macro is needed because the Prompt for Input is blocking: Once onscreen, nothing else can run. So it's called in another macro, asynchronously, from the first. Here's the timer macro:
The other macro just presents the prompt, but also sets a global variable to its own executing instance, as explained in the linked post above. That's what's used to end the macro when the timer expires.
This is bare bones—it needs more work to handle exiting early, etc. For testing, change the step size to -15 or -20 to get a much shorter countdown clock :).
Note there are probably several dozen ways to get this done; this wast just my first thought :). If you didn't want the countdown timer, you could have one macro, where the first action is the prompt, and you set its Action Timeout (in the Gear icon) to one minute. Then you can take action based on it timing out or not.
This will wait for a period of time "between 60 and 70 seconds of inactivity."
Me too, but I like to promote OCR as an alternative. In this case, I promoted OCR because it offered a conceptually simple solution for an ambiguous problem. Moreover, this problem was occurring during a shutdown, so CPU usage should definitely NOT be a concern.
Ok I have put the OCR actions before at the beginning of Rob's Macro that I downloaded and imported. I'm missing something in step 2, because it's not waiting for "file server is shutting down" message before putting up the user prompt.
You don't have anything in the conditions for when the If/Then is true or false. The stuff that should happen when the message appears needs to go in the first box of the if/then.
If you use the Share button in the toolbar in the editor to share the actual macro, it'd be easier for us to help, too.
Sorry UPS tools are more complicated than I'm looking for.
Rob, Yes I know that there needs to be an action... but I'm not sure what action is appropriate (because of the delay and user dialogue complicating things).
All the things that you want to execute if the OCR reads "server is shutting down" go in the execute section of the "If" action -- in this case that's all the actions after the "If" action to the end of the macro, and nothing goes in the otherwise section.
The other thing you should do is look at "Semaphore Locks". As written you could get multiple instances of your macro running during that pause before shutdown (because you wait for a minute but check for the dialog every 15s) -- a semaphore with settings of
OCRing all your screens every 15s for something that'll only be true once every hundreds of thousands -- maybe millions -- of checks is quite a waste of resources. Are sure you can't get some kind of notification from your NAS instead? If it's a Synology you just edit a text file -- IIRC it's /usr/syno/bin/synoups -- and include an ssh command to trigger the shutdown macro on your Mac.