Restarting the KM Engine with a single key

Technically, it's impossible to create a macro to restart the KM Engine because an app cannot directly restart itself. (Barring some sneaky level of indirection either by the programmer or the KM developer.) However I've always needed to be able to do this. So I created an Automator macro a few days go that does it, and a macOS shortcut (not a KM shortcut) that triggers this Automator macro. As a result, it works. Now I can press CTRL-F18 and it will restart the KM Engine. Here's what the contents of the Automator program looks like. It's simply a "Run Shell Script" action with the following text:

zsh -c "pkill -9 'Keyboard Maestro Engine' ; open -g -j '/Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/Keyboard Maestro Engine' --hide; say Restarting"

In the KM 11 release notes it says, "Fixed a memory leak when finding images." so maybe I actually won't need to restart the Engine anymore.

My solution does leave a small Terminal window open, but that's a cosmetic issue and it doesn't come to the front of the windows anyway, so it's a very minor issue. I couldn't figure out how to make the window invisible.

I actually have another way to solve this issue, using the KM Engine directly, but I prefer this method since there is no delay, and it's cleaner.

I run this AppleScript from KM and it works just fine:

tell application "Keyboard Maestro Engine" to reload

Your argument seemed convincing at first, but I have an issue with it. If you are running Activity Monitor, and use your method to restart the Engine, the KM Engine which I have clicked on to highlight does NOT disappear and reappear. But using my method, it does disappear and reappear. And you might wonder why that's important, and I would reply that using your method, any large amount of memory is not freed up, while using my method, any large memory allocation caused by memory leaks is freed up. I did those tests in an earlier version of KM and macOS, and so I'm not certain that the results would be the same now, since KM 11 claims to have fixed a memory leak caused by image searches.

Also, your argument doesn't really mention that your solution, like mine, creates an external process that tries to kill the Engine. It isn't the Engine itself that restarts itself. So on that point, my claim ("an app cannot directly restart itself") hasn't been disproven.

You're right. Your method is a restart; the one I just suggested is a reload/refresh.

This one is a proper restart, and can be run from within Keyboard Maestro:

tell application "Keyboard Maestro Engine" to quit

delay 2

tell application "Keyboard Maestro Engine" to activate

3 Likes

Thanks. To be honest, I was worried I might be wrong. Your new method is better than my method.

1 Like

Here’s one I developed last year and have been using successfully ever since.

AppleScript to relaunch KM Engine and Editor (click to expand/collapse)
----------------------------------------------------------
# Author   : Chris Thomerson (@cdthomer)
#
# Version  : 3.0.1 (Added try to quit application commands)
# History  : 3.0.0 (Improved app status checks and added notification of running)
#          : 2.0.0 (Added quitting Editor as well)
#          : 1.0.0 (Initial script)
#
# Created  : Thursday, April 7, 2022
# Modified : Monday, February 13, 2023
# macOS    : 12.6.3 (Monterey) - 13.6 (Ventura)
# Tags     : Keyboard Maestro Engine, Keyboard Maestro Editor
#
# PURPOSE
# Quit the Keyboard Maestro Engine and then relaunch it. This 
# is useful for when the Engine freezes or otherwise starts 
# behaving erratically.
#
# DISCLAIMER
# Permission to use, copy, modify, and/or distribute this
# software for any purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
----------------------------------------------------------

# setup variables for easier coding
set appName1 to "Keyboard Maestro Engine"
set appName2 to "Keyboard Maestro"

# try quitting the Engine
try
	tell application appName1
		quit
	end tell
end try

# try quitting the Editor
try
	tell application appName2
		quit
	end tell
end try

# notify of Engine and Editor quitting
display notification "Quitting " & appName1 & " & " & appName2

# wait for the Engine to fully quit
repeat while application appName1 is running
	delay 0.1
end repeat

# wait for the Editor to fully quit
repeat while application appName2 is running
	delay 0.1
end repeat

# notify of Engine and Editor relaunching
display notification "Quit " & appName1 & " & " & appName2 & "
Now relaunching"

# setup variables for checks
set app1Running to "launching"
set app2Running to "launching"

# launch Engine
tell application appName1
	launch
end tell

# launch Editor
tell application appName2
	launch
end tell

# wait for Engine to launch
repeat until app1Running is "running"
	if application appName1 is not running then
		set app1Running to "still launching"
	else
		set app1Running to "running"
	end if
end repeat

# wait for Editor to launch
repeat until app2Running is "running"
	if application appName1 is not running then
		set app2Running to "still launching"
	else
		set app2Running to "running"
	end if
end repeat

# notify of Engine and Editor having relaunched
display notification "Launched " & appName1 & " & " & appName2
5 Likes

Nice!