In the previous tutorial I introduced some of the basic ways Keyboard Maestro could work with files by creating a macro that renamed a group of files into a sequence.
In this tutorial, I’ll take things further and create a macro that watches a folder. Whenever an image is added to it, it’s going to take it, add a tag, move it to another folder, and then pass it to ImageOptim, an app that optimises images for uploading to the internet.
Like the previous tutorial, which you need to have read before continuing, I’ll keep things pretty basic. If you follow along exactly you’ll be able to get the macro to work.
You will, however, get more out of this tutorial if you’re familiar with the basics of Keyboard Maestro. I’ve written a five part introductory series:
- Keyboard Maestro I: Introduction
- Keyboard Maestro II: Launching Apps Intelligently
- Keyboard Maestro III: Situational Triggers
- Keyboard Maestro IV: Control Flow
- Keyboard Maestro V: Variables
I also have some more recent tutorials exploring specific uses for Keyboard Maestro which will help you see how it can fit into your workflow:
- Using Keyboard Maestro to Create Custom Keyboard Shortcuts
- Using Palettes to Improve Keyboard Shortcuts in Keyboard Maestro
- Working With Text in Keyboard Maestro: Part 1
- Working With Text in Keyboard Maestro: Part 2
You obviously need a copy of Keyboard Maestro. It’s $36 for a full licence but there’s a free trial so you don’t need to pay to follow this tutorial. If you’re going to make my macro exactly, you also need a free copy of ImageOptim.
Building the Macro
Setting It Up
Before starting, I'll break down exactly how this macro works.
Keyboard Maestro watches an Input folder. Whenever a file, or group of files, is added to the folder, it triggers and runs the macro on them. The macro gives each file a blue tag, moves each file to an Output folder and opens each file in ImageOptim.
The macro works on images, but it will run up to the last step on any kind of file.
The first step is to set up the folder structure in Finder. I’ve two folders, one called Input the other called Output, inside a folder called KM Test for this lesson but the folders can be anywhere.
Open Keyboard Maestro and create a new macro. Call it something descriptive like Watch Folder for Web Optimisation. Click New Trigger and select Folder Trigger.
By default, the folder trigger targets the Downloads folder. This isn’t what I want. Click the small purple folder icon and then select the Input folder. Ensure the trigger is set to Adds An Item and Ignore Partial Files and it’s ready to go.
Pulling in the Files
Now I’ve got a macro that will trigger when anything is added to the Input folder but it doesn’t do anything yet or have access to the files in that folder.
The next major step is to pull in all the files in the Input folder.
Add a For Each Path in Folder from the File category. Click the purple folder icon and select the Input folder again.
For this macro, it doesn’t matter what order the files are operated on so I’m going to leave it Alphabetically. If it does matter for you, explore some of the other options.
Adding a Tag
Now that the macro triggers when I file is added to the Input folder and then pulls it in, it’s time to actually start doing something to it. The first thing I want to do is add a blue tag to it.
This is one of macOS’s built in tags and it will just let me know that any image tagged with it is optimised for the web.
Add a Set File Attribute from the File category under Execute the Following Actions. Set Set to Tags (add), Of File to
%Variable%Path%, and to to Blue.
Now when any file is added to the Input folder it automatically gets a blue tag.
Moving the Files
The next big step is to move the files. Before I do that, however, I want to save the filename to a variable.
At the moment, I only have it’s current file path saved. When I move it, its file path will change so the current variable won’t work any more. I used the same method in the last tutorial to get the extension of the file.
Add a Get File Attribute action and set it to Get the File Name and save it to a Variable called FileName from the File
Now that’s saved, I can move it. Add a Move or Rename File action. Set it to Move
%Variable%Path% and then use the folder selector to select the Output folder.
Any file added to the Input folder will end up in the Output folder with a blue tag. Now is a good time to test the macro out and make sure everything is working okay.
Opening the Moved Files in ImageOptim
The final step is to send the newly moved files to ImageOptim where they’ll automatically be optimised for uploading to the internet. I’ve covered ImageOptim before if you are interested in more on how it works.
Add an Open a File, Folder or Application action. For With select ImageOptim. The problem lies in telling Keyboard Maestro what file to Open. Since the macro has moved the file, the Path variable doesn’t work.
The trick is to combine the folder path, which we set, and the filename, which I've saved as a variable. Copy and paste the folder path from the previous step and then add
/%Variable%FileName% at the end (make sure to include the slash). That way Keyboard Maestro knows where to find the file.
And that’s the macro finished. Whenever you add a file to the Input folder it gets moved to Output, a blue tag applied and optimised for the web by ImageOptim.
While the macro I demonstrated in this tutorial isn’t incredibly useful (you can just open files in ImageOptim and get much the same effect) it shows a lot of useful techniques for building more powerful macros with the Folder Trigger.
Now you know how to have Keyboard Maestro monitor a folder, pull any new files in, and operate on them both before and after moving them to a new location.
Here’s the finished macro.
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post