Hi everyone.
I have long-running macros that sit there chugging away for hours while I do other things and am not looking at the screen.
Sometimes I come back and see that the macro failed / aborted quite a long time ago. A simple option to play a sound (or if you really want to get fancy, send an SMS or other notification) would alert me that I need to take a look and would also allow me to venture into other tasks, freeing up my attention.
How to configure KM to play a sound when a macro fails / aborts?
Sorry if this is a stupid question because it seems so obvious a feature. But I've looked everywhere and cannot find it. Cheers all. Rook.
I think I might have a slick solution to this problem, but you’ll have to wait till I get back from vacation. One question I have for you is how quickly do you need the message to be sent. Is five minutes after the error OK? Or do you need it to be under 10 seconds after the error message appears?
The send SMS action in keyboard maestro is not very reliable in my opinion. But it doesn’t sound like you need 100% reliability here.
Hi!
Here's my take on a way to receive a notification if a particular macro is cancelled before completion. It is now set up to play one sound if it is canceled, and another sound if it completes without cancelling, but other kinds of notifications can be placed in place of these auditive notifications.
This macro works by waiting for the caller macro (any macro you have placed the Execute Macro action at the beginning of), and then checks the KM engine log to see if the caller macro is entered as having been "cancelled" within the last second. There might be some nuances of how a macro can fail/abort that I am not thinking of and that this macro does not account for but hopefully something like this will work for you.
You might have to change the directory in the Shell script condition to suit your Keyboard Maestro Engine log. The simplest way to do this is to click Help > Open Logs Folder, then right clicking on the Engine.log
file, and while holding down option selecting Copy "Engine.log" as Pathname, then pasting this path into the shell script, between the double quotes, in place of the $HOME/Library/Logs/Keyboard Maestro/Engine.log
that is entered there now.
For demonstrational purposes I made a macro that fails after 3 seconds, but if you change the 0
to a 1
in the For Each it will run to completion without getting cancelled.
Notify if caller is no longer running CALLER.kmmacros (18 KB)
Notify if caller is no longer running SUBMACRO.kmmacros (20 KB)
EDIT: Made a couple of small change to the submacro
All though I like my submacro approach above I just had a literal shower epiphany that the Try/Catch action is very well suited for this kind of a task. Just place your whole macro within the 'Try' section (top field) and whatever kind of notification you want in the 'Catch' section (bottom field) and you're done!
I've demonstrated it bellow using the same action that fails after 3 seconds from my demonstration above. I've also added to the demonstration a green group as an option if you'd also want another notification for when the macro (the red actions in my demonstration) does not fail.
Notify if macro fails USING TRY-CATCH.kmmacros (19 KB)
EDIT: Made a couple of small change to the macro
Taking it a step further -- use a wrapper macro with "Execute a Macro" calling your "proper" macro in the "Try" block, put your error handling in the "Catch":
Fails 1 in 3.kmmacros (3.2 KB)
Notify on Failure.kmmacros (2.5 KB)
I can give you a really simple option. It's so simple, it doesn't even use the KM Engine itself! Just open a terminal window and execute this command:
tail -f -n 0 "/Users/myname/Library/Logs/Keyboard Maestro/Engine.log" | grep --line-buffered "failed:" | tr " " "\007"
You have to use your own username where it says "myname" above. What this does is CONTINUALLY examine the trailing end of the KM engine log file, look for errors (by selecting lines containing "failed:", and print "about 5" beeps whenever an error is observed. The reason I say "about 5" is that the number of beeps is equal to the number of spaces in the error message. This seems like a strange way to do things, but I find that one beep really isn't enough, so it works pretty well this way. There are ways to use custom audio files, but that complicates the solution.
As for sending SMS, this method can also be modified to send SMS messages, (once again without even using the KM Engine) but this requires knowledge of the country that you are located it, because some SMS web portals seem to block requests based on your country of origin. I haven't done this myself yet, but it should be a good option because KM's SMS action isn't 100% reliable for some reason, as it says in its own documentation. (Due to the fact that Apple's own Message app, at least in Big Sur, does not support AppleScript! Shame on Apple!)
Another factor which you haven't specified that would help here is whether there is an iPhone near your Mac that is logged into the same Apple ID as your Mac is when errors occur. If so, I think this is much easier, but more likely your response will be "I take my phone with me when I'm away from my computer, so we can't rely on a phone being nearby."