Is There a Way to Restart Keyboard Maestro With a Macro?

Is there a way to restart Keyboard Maestro with a macro?

One macro launched by a USB key trigger stops working from time to time and I can't figure out why. Restarting Keyboard Maestro helps.

I need these steps:

  • Quit Keyboard Maestro (engine and editor)
  • Wait 5 seconds (or wait until Keyboard Maestro process is shut down)
  • Start Keyboard Maestro (with editor closed, if possible)

I'd do it from the menu bar icon.
Is this possible with an AppleScript or a ShellScript?

Hey Boris,

Yes.

You can't do this directly - once Keyboard Maestro is quit then you're dead in the water.

You could save the script as an applet though and then use a Keyboard Maestro macro to activate the applet. That would handoff the task while Keyboard Maestro was quitting and restarting.

I keep other script runner utilities on my system for just such tasks. FastScripts or LaunchBar could do this much more conveniently than an applet.

Regular script - non-applet version:

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/12 14:29
# dMod: 2017/03/12 14:38
# Appl: Keyboard Maestro, Keyboard Maestro Engine, System Events
# Task: Quit the Keyboard Maestro Editor and Engine then restart the Engine.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Keyboard_Maestro_Editor, @Keyboard_Maestro_Engine, @System_Events, @Quit, @Editor, @Engine, @Restart
-------------------------------------------------------------------------------------------

try
   
   tell application "Keyboard Maestro"
      quit
   end tell
   
   tell application "Keyboard Maestro Engine"
      quit
   end tell
   
   set theCntr to 0
   set theDelay to 0.5
   
   tell application "System Events"
      repeat while length of (get processes whose name contains "Keyboard Maestro Engine") > 0
         set theCntr to theCntr + 1
         if theCntr > 20 then
            error "There was a problem quitting Keyboard Maestro!"
         end if
         
         delay theDelay
      end repeat
   end tell
   
   tell application "Keyboard Maestro Engine"
      run
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

-------------------------------------------------------------------------------------------

Applet version:

Quit and Restart Keyboard Maestro.app.zip (57.7 KB)

I'm not sure if GateKeeper will let you run the applet, but you can open it and re-save it using the Script Editor.app.

-Chris

2 Likes

How do you do with LaunchBar?

More importantly, this AS quits and relaunches the engine BUT the engine doesn't work until I manually quit and relaunch it again.

Could be my screwy system or something else?

I don't use LaunchBar. I use Alfred. But I believe it should be very similar.

I use ⌃⌥⌘⇧ + K to trigger it:

image

My script is also from @ccstone. It is to toggle KM Engine status.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/04/20 09:54
# dMod: 2018/04/20 09:55
# Appl: Keyboard Maestro Engine
# Task: Quit and restart the Keyboard Maestro Engine
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Quit, @Restart, @Keyboard_Maestro_Engine, @Engine
----------------------------------------------------------------

tell application id "com.stairways.keyboardmaestro.engine"
   if running then
      quit
   else
      launch
   end if
end tell

Hey Bern,

That's odd...

Try changing this:

tell application "Keyboard Maestro Engine"
    run
end tell

To this:

delay 1

tell application "Keyboard Maestro Engine"
    activate
end tell

If it still doesn't work change the delay to something fairly big like 5 or 10 or 20, and see if that works.

-Chris

Hey Chris,

With 5 I get

After dismissing, the engine resumes once I switch to KM.

Hmm...

I just ran my original script from Post #2 on Mojave with LaunchBar.

It works perfectly.

What are you running the script with? (Still on Big Sur?)

-Chris

Post #2 script, using execute text script with KM on Big Sur.

I downloaded LaunchBar per

I don't know how to use it per this application (or anything else till I play with it a bit ;).

Try it out and see if you like it – but also look at Alfred.

LaunchBar hasn't seen much major development for quite some time now, but Alfred keeps on trucking.

I've used LaunchBar for nearly 20 years, but I'm going to reevaluate after testing Alfred again sometime soon. Alfred does some things LaunchBar can't, and that intrigues me.

But – those features will have to make it worth dropping my daily driver and undertaking a new learning curve – and I'll have to be able to live with the UI.

-Chris

BTW – run the script from the Script Editor or Script Debugger.

That's the place to start.

-Chris

I’ve no attachment to LB. I’m only looking to run this thread’s script. Will Alfred do that?

Generally, I’m doing almost everything with palette triggers and have no interest in keyboarding ironic as that may sound. I just looked at LB over fastscripts thinking there might be some more functionality that I could pull into my KM palette palace. I’d view Alfred the same.

Will test as you suggest. TY!