Hazel and KM

Has anyone figured out a way to get Hazel to trigger a hot key that would, in turn, trigger a macro in an app like KM? (Or open to any other suggestions on how folks have had success using the two apps together.)

Quote from Hazel user manual:

“ Hazel rules can use AppleScript or JavaScript in either of two ways: to provide logic for a condition (the “Passes AppleScript” or “Passes JavaScript” attribute), or to perform an action when a rule matches (the “Run AppleScript” or “Run JavaScript” action). If you need to accomplish a task that Hazel’s conditions or actions alone don’t cover, AppleScript or JavaScript may provide a solution.”

Since KM macros can be triggered using AppleScript/JavaScript/shell it would be easy to do as you ask. You could run other apps using the same techniques.

Sorry - I’m away from my Mac so can’t give more concrete examples.

1 Like

See this post for the AppleScript syntax to run a KM macro by its UUID.

2 Likes

Thanks, @tiffle and @noisneil!

What would be an Apple Script to replicate pressing "⌘⌃⌄C" on the keyboard (ignoring the quotation marks)? Would something like the following work?

tell application "System Events"
   key code C using {control down, option down, command down}
end tell

key code 8

(not "C")

1 Like

Thanks, @tiffle. So it would be the following...?

tell application "System Events"
   key code 8 using {control down, option down, command down}
end tell
tell application "System Events"
    key code 8 using {control down, option down, command down}
end tell

would be AppleScript triggering ⌃⌄⌘C

Out of curiosity, what is it you're actually trying to do @alexcr - I mean the "big picture" not just key strokes?

2 Likes

Thanks again, @tiffle! I'm trying to get Hazel to trigger a macro via a hot key.

Firstly – why are you monkeying around with key codes when you can use keystroke to press the actual key?

tell application "System Events"
   keystroke "c" using {control down, option down, command down}
end tell

Secondly – using keystrokes is is a bad idea when it's not the only option. You can run a specific macro from AppleScript by name or by UUID.

tell application "Keyboard Maestro Engine"
   do script "Generic-Test 01"
end tell
tell application "Keyboard Maestro Engine"
   do script "129475DB-3D45-0101-9090-8F2C9B67F66C"
end tell
2 Likes

Ah, good to know! I'm learning on the fly here. :sweat_smile:

Ah, this is very helpful. Where do you find a macro's UUID?

1 Like

Select your macro and right-click or go to the Edit menu of the Keyboard Maestro Editor and look at the options in the Copy as menu.

2 Likes

That’s exactly why I asked the question
 as I said in my first reply to you and as @ccstone has very clearly said, Hazel can use AppleScript/Javascript to directly trigger specific macros - no need to use keystrokes/codes at all!

1 Like