Trigger Any Specific KM Macro Instantly Via LaunchBar Action

My mind is blown, y'all.

A minor "holy grail" for me has been: how can I quickly and easily trigger a KM macro in Launchbar. Yes, I know someone has made an action where you first invoke KM in LB, then hit space and hunt for the macro you want. I also know about the Trigger Macro By Name macro. But I wanted to trigger a macro instantly from LB.

I knew that one could, theoretically, create LB actions that ran scripts. But I found the LB action editor extremely confusing. Well, through trial and error, I figured out how to make a simple LB action that runs a script. Then, I read up on KM's scriptability. It's a beautiful thing.

I am no coder, and I am not proficient in any scripting syntaxes, but for what I want to do, this sh*t ain't rocket science, it turns out. :wink:

Here are, for me, the key Applescripts:

Tell KM to execute a macro:

tell application "Keyboard Maestro Engine"
  do script "[Name of Your Macro]"
end tell

You can also identify a macro by UID:

tell application "Keyboard Maestro Engine"
  do script "D0C150C7-8A0C-4837-918A-427E2BCFB6B9"
end tell

Again, this is all on the KM scripting guide page (including instructions for obtaining macro UIDs).

It also occurred to me that you can tell the computer to execute keystrokes and key combinations via Applescript, which would also allow you to trigger a KM macro or conflict palette. So, for example:

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

Now, plug one of those simple scripts into a custom LB action, and you're good to go.

The method for creating a LB action is simple:

First, invoke the Action Editor in LB:

Click the + and select "New Action":


Enter the title of your action and hit return. This will populate the Action Name and the File Name.

Click on the "Scripts" tab, and choose AppleScript for the Default Script.


Click "New Script", enter a name for the script with suffix `.applescript`.


Hit `return` and you will see the new script.

Click "Edit" and the script editor will open. At that point, you can enter one of the simple scripts I describe above, e.g.,

tell application "Keyboard Maestro Engine"
  do script "[Name of Your Macro or UUID]"
end tell



Compile the script and close the editor. Enter the name of the script in the box underneath "Default Script" and hit return. (I typically also check "Run in background" although I'm not sure it's needed). You will see that the new script that had appeared near the bottom of the editor has disappeared.

If you need to edit the script again, simply click "Edit", do your editing, compile, and close the script editor.

You can drag and drop an icon image into the "Resources" area if you want.

This should create a working action which triggers your KM macro. You can now use it like any other LB action.

For my part, I don't need to access all of my macros via LB, but there are several that I find very convenient to invoke via LB. In the below examples, I'm using LB to invoke palettes of actions for the clipboard, for markdown text transformation, and for document templates:

Here is a sequence triggering text wrap and unwrap functions:




The possibilities are endless!

Cheers!

Brian

5 Likes

Thanks for sharing this tutorial on how to create a LB Action to trigger a KM Macro.

I'm probably missing something, but I honestly don't understand the advantage of using LB vs a KM Palette for this purpose. (BTW, I am a very avid LB fan, using it many times a day).

In all of the above cases the number of keystrokes is the same:

  1. Trigger the tool (LB or KM Palette)
  2. Type the hot key for the macro of interest

For high-use macros, like your "palettes of actions for the clipboard, for markdown text transformation, and for document templates", I usually use a hot-key. The advantage of using KM directly is that it knows which macros are active for a specific app, and so I can, for example, use the same hot-key when I am in different apps, to trigger app-specific macros. Templates is a good example. ⌃T might be a good choice for templates in all apps.

So, I guess what I am trying to understand is what benefit do you get by using LB that makes it worth all the effort to build a LB Action for each KM Macro? If it is just your preference, that's fine.

BTW, I also use @DanThomas' MACRO: Execute Macro by Name (Spotlight) to quickly and easily select a macro from a common set.

For example, I trigger Dan's macro (hot key ⌃⌥⌘M), then type "@CB" to get a list of my ClipBoard macros, then just a letter or two to select the one I want. The "@CB" is what is referred to as a "text tag".

All of your points are valid. I think it may come down to personal preference and how your muscle memory is distributed. I have probably a dozen hotkeys for different palettes. I have fewer hotkeys for individual actions. Sometimes it's hard to remember the hotkeys. (I actually have a hotkey that brings up a cheatsheet for hotkeys.). I find that invoking the "action by name" dialog to require just a bit more time than invoking LB, because I need a second to remind myself of the hotkey for that and to adjust my fingers accordingly. Also, LB allows me to assign a more intuitive abbreviation to different actions, as opposed to having to recall the actual name of the action if I'm using the "action by name" macro. So for example, to invoke "Clipboard Options" I just invoke LB and hit "CCO".

I haven't assigned all KB hotkeys to LB, just some that I use very frequently, including some individual actions for which I don't have a KM hotkey set (it may be part of a conflict palette). So, bottom line, I use a mixture of LB and KM to invoke KM actions. There are just times when I want to get to a KM action quickly through LB because I invoke LB so often that I don't want to break out of that habit to get to KM. For those KM hotkeys that I invoke regularly, the muscle memory is set, and I just go ahead and invoke via those hotkeys. I guess I find that using LB for some actions helps me have access to the full suite of KM actions without necessarily having to remember all the hotkeys all the time. I'm damn near 50 and just find it more difficult to do that these days!

Finally, I haven't had any issue with LB triggering actions or palettes that aren't appropriate to the frontmost app. I think most of the palettes or actions I invoke via LB are global actions.

1 Like

OK, that's great. I just wanted to make sure I was not missing some intrinsic LB feature, since I'm still on a steep learning curve with all of its features.

1 Like

One nice thing is that if you use the KM action UUID in the Applescript for the LB action, you can edit or change the name of the corresponding KM action without having to alter the script.

Thanks for this, really nice.

My pleasure. Enjoy!

Thank you for sharing this. I have a KM macro that takes a phone number off my clipboard, and sends it over to a VOIP app (Mitel), pasting it into the "dial" field and hitting Enter to dial the number. I have to brute force this in KM by using found images, but it works.

I tried creating a custom LB action to invoke that macro, and then tie it to the default phone number action in the LaunchBar settings. I get the attached error, though: "The script does not implement a handler named "handle_string". I did find a post in the LB forums, which seems to predate this post, but I have no idea how to use this, such as where the plist file is.

If anyone has any suggestions, I would be very appreciative. Thanks!

Hey Evan,

I'd use clicks relative to the front window instead of found images. It should be quite a lot faster.

I don't see how that could possibly work, since the LaunchBar action is designed to work with system software and not your app.

Ask Mitel support if their app supports a URL-scheme for scripted access.

-Chris

Thanks, Christopher. I'll see if Mitel supports a URL scheme, but I'm not optimisitc. Since the initial post in this thread lets you fire KM macros from LaunchBar, and I have a macro that works to dial the number, I was hoping I could just use LaunchBar to kick off that macro and dial the number. But certainly a URL scheme would be ideal.