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.
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