Won’t it be nice to execute Keyboard Maestro macros remotely? Imagine what you can do with this feature. Forget to lock your Mac? Put it to sleep with single tap. Need to run command in Terminal? Send the command from your iPhone. If you can build the macros, you can run it from everywhere.

This guide is inspired from the post written by David Sparks who shared his method to put Mac to sleep with Drafts and Rick Stawarz who simplified the process with Launch Center Pro. The basic concept for this setup is to have Hazel monitor incoming new files in specified folder which in return, if the new file matches the rule conditions, will run an AppleScript that puts Mac to Sleep Mode.

Just like Rick Stawarz, I’m also not satisfied with only putting Mac into sleep mode. Sometimes I only want to turn off the display which can’t be done in AppleScript. I also want to receive notification when an action is completed.

The most important feature I’m looking for is simplicity to create more automated actions on Mac. AppleScript is powerful, but it’s difficult to organize them.

Since Keyboard Maestro also supports AppleScript, I put together the list of apps and services you can use to run macros remotely and receive notification when a macro is triggered.

Configuring Notification

We’re going to use email channel as the trigger that will send the notification to Launch Center Pro. I considered other channels as triggers, but it seems to me that email is the only channel that offers flexibility in customizing notification message.

2. Create a new remote macro group. I name mine Maestro Control. It’s recommended to put macros with different triggers into separate macro groups. This structure allows us to extend existing macros easily — in our case, it’ll be sending email to IFTTT email address actions.

3. Create the macro you want to run remotely. The one we all can benefit from is the Put Computer to Sleep macro. Try to see if the macro is working by clicking the Try button under the actions sheets. We can continue to next step when the macro is working as expected.

4. Create email actions that send email with the message of your notification as subject to your private IFTTT email address. I always name the subject with this pattern Keyboard Maestro — <Message> to differentiate Keyboard Maestro notifications from others.

You’ll notice that I put a 20 seconds pause before quitting Mail. This is because we want to make sure the email is sent to IFTTT properly. Sometimes you must send the notification first if the macro will pause the system process — like this macro that puts Mac into sleep mode.

Now we have a perfect working macro that’ll send you notification when it runs. IFTTT for iPhone keeps the log of triggered recipes. You can use it to find out what actions you’ve triggered throughout the day.

Configuring Remote Triggers

Remote triggers are the way you send message to your Mac. It doesn’t have to be Drafts and Launch Center Pro. You can even just create a plain text document in the folder of your choice to trigger macros remotely. Since we prefer a simplified workflow, we’re going to use Drafts for uploading file to Dropbox, and Launch Center Pro for single tap trigger.

1. Create a new Dropbox action in Drafts. Follow the setting below to create a new plain text document with this filename pattern 2014-08-20-17-13-12 KM Sleep Screen.

2. Configure action to delete the notes after success by default. You can find the setting for After Success under Manage Actions. This will remove the sent notes from history and archive.

3. Use this spreadsheet to generate x-callback-url that triggers action in Drafts. The formula in x-callback-url column replaces the white space with %20. You can also encode other characters with URL Decoder/Encoder.

4. Create a new action in Launch Center Pro. Avoid using Action Composer. Instead, paste the generated x-callback-url in the URL field. Name it with the macro name (without KM) like Put Computer to Sleep and choose a suitable icon for it. Try to run the action. It should open Drafts, create a new plain text document in Dropbox, and return to Launch Center Pro.

The Drafts action we created earlier, if you noticed, skip the first line of text, and only upload the body of content to Dropbox. This means we can write anything on the second line and have AppleScript set its to system clipboard. We’re going to find out how to do it in AppleScript later. For now, let‘s focus how to pass sentence to second line in Launch Center Pro.

You’ll find a line break encoded character %0A in the generated x-callback-url. Put [clipboard] after %0A like this following example:

Feel free to experiment with other type of prompt in Launch Center Pro action composer. The others are [prompt], [prompt-num], [contact], and [dropbox]. You don’t have to memorize these tags since you’ll be able to see them when URL field is active.

5. Check the new file in Dropbox. Make sure new file exists in the folder you’ve selected in Dropbox — in our case the folder will be located at ~/Dropbox/Apps/Drafts/. Verify the filename and its content.

Setting Up Hazel to Monitor Triggers

Now we have remote triggers and notifications running properly. The final step will have Hazel links everything together. Hazel will tell Keyboard Maestro to run macros based on incoming files in Dropbox.

1. Tell Hazel to monitor Drafts folder. If you’re new to Hazel, simply drag the folder into the Folders section.

2. Create a new rule for each macro. The rule above runs macro by detecting the filename that you’ve defined in the spreadsheet that generates x-callback-url. Once Hazel detects a matching file, it’ll move it to Trash and runs the content of AppleScript.

3. Run Keyboard Maestro macros with AppleScript. There are three ways to run Keyboard Maestro macros in AppleScript. The simplest one is to run macro by name.

4. Pass file content to clipboard. You can also set the system clipboard with the content of your Dropbox plain text document. Add these two lines of code that set the system clipboard to the file content.

Create a new Hazel rule in Drafts folder that detects the filename of the plain text document. Use the UID in the AppleScript.

Create a new Launch Center Pro action and use the generated x-callback-url in URL field.

It’s a rather complex setup requiring a lot of apps and services. But the core of the services is Dropbox and Hazel. Drafts and Launch Center Pro merely simplify the workflow so you don’t have to type command whenever you want to run a macro remotely. If you find this guide useful, please share it with your friends or followers.

Related Posts:

Get my book for FREE! I’m writing a book about Keyboard Maestro where you can learn to automate common tasks with Keyboard Maestro macro examples. Subscribe to receive the book when it launches. More details »