How to use "macOS Shortcuts" to make the KM Engine restart

I love KM. But I do have to stop and/or start the engine (up to) dozens of times per day, so I started to wonder if there was an easier way to do it. Right now I stop the engine by clicking on the KM Engine menu in the menu bar, and then if I want to start the engine I have to launch KM Editor (or, if it's already running, switch to it) then I have to click on its "File/Launch Engine" menu item. That's a lot of steps.

In theory the KM Engine can be used to stop itself, but it (obviously) can't start itself. So I was wondering what part of macOS can be used to do both of those things. naturally, the macOS app called Shortcuts was probably the solution. After 15 minutes, I had a simple shortcut running that does what I want.

I actually have two shortcuts... one that reboots the Engine, and one that toggles it. For today, I'll simply post the one that reboots the engine. Here is a screenshot of the shortcut, which you can use to create your own "shortcut."

The "sleep 1" command in the above action seems to prevent a timing issue that occasionally resulted in a shell script error message.

Notice the checkbox for "Pin in Menu Bar". This allows you to access this macro from the Shortcuts icon in the Menu bar, which is a convenient way to trigger this macro.

To make it even easier to use, drag the Shortcuts icon in the Menu Bar next to the KM Engine in the Menu Bar (to do that, use COMMAND-LeftMouseClick to drag the icons to different places in the Menu Bar.)

It seems that macOS has a limit of triggering the same shortcut once every six seconds. So if you try to trigger this shortcut twice in a row, you will have to wait at least 5 seconds for a message to disappear before the menu reappears to let you run it again. That's not a problem, since nobody will need to reboot the engine twice in 6 seconds.

Even better, the Shortcuts app allows you to assign a single key to this shortcut. So for example, I can assign the key "Command-Home" to the shortcut, and now pressing that key will reboot the Engine.

Just click on the button "Add Shortcut Key" and press Command-Home. Then it looks like this:

Screenshot 2024-03-13 at 13.28.17

Now I can reboot the KM Engine with a single key. Yay!

(Note: Bear in mind that the Shortcuts app's shortcut keys seem to have a lower priority than the active app's shortcut keys, so if the app you are using already uses the key you assigned to the Shortcut, your app's definition will take precedence. So use a shortcut key that your apps don't use. Command-Home is probably not the best shortcut to choose, since many apps have this key pre-defined. I still don't understand this topic fully.)

In fact, you can even have the KM Engine send a key to macOS to call this Shortcut, but that's an unnecessary level of redirection, so I won't explain it here (even though it works, and it's cool to realize that using this method the KM Engine can restart itself.)

If people want to see my separate Shortcut which toggles the engine rather than restarts it, I can post that also, probably as a reply within this thread.

P.S. I used a "robot head" as my icon for my shortcut. To me that seems to capture the essence of what KM is. There are hundreds of icons to choose from, but I couldn't see anything better. Therefore, I recommend that anyone who creates Shortcuts that pertain to Keyboard Maestro should use the robot head icon.

I don't have the Shortcuts app on my Mac, but one of the methods in this thread is likely viable.

Unfortunately, the "reload" in that post is not the same thing as stopping and restarting the engine's process. It sounds the same, because it uses the word "reload," but it's not the same thing as stopping and restarting the KM Engine. So it's not an equivalent solution. It wouldn't solve the problems that are making me do all this.

This AppleScript code quits and relaunches the KM Engine in one move. Of course, for individual needs it may be necessary to make this two separate scripts.

tell application "Keyboard Maestro Engine"
	quit
	
	delay 1
	
	tell application "Keyboard Maestro Engine"
		activate
	end tell	
end tell

You should quit the engine using AppleScript, not kill -9 (which basically just stops it instantly with no ability to cleanly exit). kill -9 will almost certainly lead to corruptions of things like the variables database if you do it enough and get unlucky.

Starting the engine is best done by specifying the full path to the Keyboard Maestro Engine to ensure the correct version is run if you have an old version of Keyboard Maestro on your Mac (which you should generally avoid) by using the open shell command.

A better question though is why do you have to quit & relaunch the Keyboard Maestro Engine multiple times a day. That is decidedly not normal.

I accept your advice. I regret proposing an idea that might be harmful. However then I will modify my command to issue an AppleScript command from the command line. I think it's possible to do that.

Okay. Hmm, neither my idea nor NaOH's idea used a full path.

The Engine's memory allotment increases dramatically over time. Just now I restarted the engine and it dropped from 447 MB down to 68 MB of RAM. It used to consume cumulatively about 1 GB more RAM every hour, so after 8 hours it was using 8 GB of RAM, (grinding my 8GB Mac Mini to a halt) but now it's probably about 100 MB more RAM every hour (perhaps that's because I have an M3 with triple the RAM of my old M1 machine.) In order to keep the RAM usage minimal, I have a KM macro that reminds me every hour (if my machine has been idle for a few minutes) to restart the Engine. This is why I created my macro today.