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

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 %Variable%Path%.

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.

## Wrapping Up

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.