Applescript to run macro

I want to write a simple AppleScript to run one KM macro.

I used the syntax:

tell application "Keyboard Maestro Engine"
do script "Switch to Compact View"
end tell

But when I ran it, I got an AppleScript error saying “do script found no macros with a matching name”.

Since that IS the name of the macro, I tried again with that macro’s UID, but got the same error message. What am I doing wrong?

1 Like

Okay - just discovered it will work if I put the macros in the Global Macro Group.

So what is the AppleScript syntax I should use if I want to keep the macros in a specific group?

It has nothing to do with AppleScript. You just have to put the macro in a group enabled when you want to run it. For example if you wanted to run it when Safari is in front, then it would have to be in a group enabled when Safari is in front, or in a global group.

1 Like

Got it - thanks much.

Couldn’t I make some simple Applescript application that does basically nothing. Run it, and use the “Triggered by ABC Application” to kick off the KM macro?

You could, but there are better ways: if you click on “or by script” in the triggers area of a macro and select “Or by AppleScript”, KM will display the command to run from AppleScript to trigger the macro. Using this method you can trigger a macro by clicking an icon in the Dock, for example, but you could also combine it with Folder Actions to trigger a macro when the contents of a folder changes, etc.

1 Like

I gotta say, the “dump app” solution seems really slick.

I just made an app called VPN400 to kick off a toggle method, and it does basically nothing.
Now, for any KM I want, I can just

(1) duplicate the dumb app,
(2) rename it to the new app for kicking off the desired KM script, and
(3) within the desired KM macro, use the “Triggered by” function so that the dumb application kicks off the macro.

Obviously, this is a bit of a disjoint approach, without a lot of smarts or flexibility, but it seems to get the job done for simply kicking off a script.