Keyboard Maestro offers many ways to trigger macros, but unfortunately there isn’t a URL handler that lets you trigger macros via a URL. There are a number of keyboardmaestro:// URLs handled by the built-in url scheme, but executing a macro isn’t an option. If you’re interested in triggering macros via bookmarklets and Finder shortcuts, read on…
I’ve written a URL handler that allows you to execute macros and pass variables to them. It’s a simple app that registers a URL handler to process kmlink:// links.
- Download the KMLink-v2.app zip file
- v2 allows you to pass variables; v1 did not
- Extract KMLink.app to your Applications folder
- Open the application. You only have to open it once (which will register the handler), but you need to leave it on your system.
If you’re curious what the application does, I encourage you to (after downloading it and extracting the application) right-click on the application and select “Show Package Contents”. Browse to Contents » Resources » Scripts and open main.scpt in the Applescript Editor.
I’m going to walk you through a basic scenario. We want to visit a URL that will trigger a macro that shows an Alert containing the variable we appended to the URL.
Step 1: Create the macro
- Open Keyboard Maestro and create a new macro (within a macro group that is “Available in all applications” and “Always activated”)
- Set the macro title to KMLink Demo
- Do not set a trigger
- Add a new “Alert” action
- Add the following as the Alert’s comment: %Variable%FirstName%
Step 2: Get the macro’s UID
We use the UID because it’s unchanging and safer than calling a macro via its title.
- With the macro selected, click Edit » Copy As » Copy UID.
Step 3: Try the URL trigger
- Visit the kmlink:// URL in your web browser. Replace the [UID-HERE] part with the UID you got in step 2:
- If the alert popped up, KMLink.app is working correctly. You should see an alert without the variable showing.
- If the alert did not pop up, send a message to @agileadam and I’ll try to help.
- If you realized the variable is missing from the alert, that’s excellent! We didn’t pass it along…
- To add the variable (or any number of variables for that matter), you simply add them to the end of the URL as additional parameters. Please understand that the URL has to be valid (so spaces should be written as %20 for example).
- Here are some examples:
Step 4: Make it easy to use
There are a number of ways to use this new URL trigger. Here are the most common:
Bookmarklets, for our purposes here, are just bookmarks that, when clicked, will trigger a macro.
- Create a new bookmark. Set the URL to the URL you tested above.
- Click the bookmark to run the macro. Done!
I’ve used these to trigger a macro via an icon in my dock. You can use them however you use other items in Finder. Click it to execute your macro.
- The simplest way to do this is to create a bookmarklet (as explained above)
- Drag the bookmarklet into a Finder folder (I usually just drag to my desktop).
- Optionally change the icon of the shortcut. This is explained well at OS X Daily.
Inject links into a webpage
This is more complicated because it requires some basic coding knowledge and (possibly) and understanding of user scripts. I’ve written a full post about this, entitled Keyboard Maestro URL Handler (KMLink) – Link Injection.
After clicking a bookmarklet (or link), my chrome window is no longer active
You may have to start KMLink macros with an “Activate Google Chrome” (or Safari, or whatever you use) action. This makes sure that after opening KMLink it bounces back to your browser to carry out the macro’s actions.