www 101

All you need to know about the internet

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Working With Files in Keyboard Maestro: Part 1

So far in my tutorials on Keyboard Maestro, I’ve covered a huge number of the app’s features. I’ve looked at everything from creating simple keyboard shortcuts to macros that automatically Tweet whatever you’re listening to on Spotify. One thing I haven’t covered yet, however, is how to use Keyboard Maestro to work with files in the Mac’s file system.

It’s time to fix that.

Over the next few tutorials I’ll show how you can work with files in Keyboard Maestro

In this tutorial, I’m starting with the very basics by showing you how to create a macro that renames a group of files so that they all have the same base name followed by a sequence number, e.g. Tuts-1.jpg, Tuts-2.jpg, Tuts-3.jpg and so forth. 

It’s a relatively simple macro but it nicely demonstrates the basics of working with files.

Prerequisites

For this initial lesson I’ll keep things fairly simple. You’ll be able to get the macro to work by following my exact steps. To get the most from this tutorial though, it will help if you’re familiar with the basics of Keyboard Maestro. The best place to start is my five part introductory series:

You should also consider looking at my more recent series which covers:

You’ll also need a copy of Keyboard Maestro. It costs $36 for a full licence (which I obviously think is well worth it) and there’s a free trial so you can get a feel for the app before buying it.

Building the Macro

Setting It Up

Open Keyboard Maestro and create a new macro. Give it a name like Rename Files and a Hot Key trigger. 

I’ve gone with Command-Option-Shift-R for the trigger because it’s not used by Finder already.

namingmacro
Naming the macro.

This macro needs to do a few things before it loads in any files. 

First, I need some way to tell it what to name each file. 

Second, it needs a counter so that it can add the sequence number to the end of each filename. That counter also needs to reset to 1 every time the action is run. 

Add a New Action and select Prompt for User Input from the Variables Category. Give it a title and prompt. Click the Green Plus icon to add a new variable. Call it BaseFilename. Leave the Default Value blank. 

user prompt
Getting the new name from the user.

Next add a Set Variable to Calculation action. You’ll also find it in the Variables Category. Give it the variable name FileNameCounter and have it set to 1

This both gives us the counter variable and resets it every time the macro is run.

filenamecounter
The filename counter.

Working With the Files

Once things are set up the files can be pulled in. Add a For Each Path in Finder Selection action. For the macro to run, you’ll need to select a group of files in Finder before pressing the Hot Key trigger. If you don’t select any files, it will just crash.

A For Each Path in Finder Selection action is a kind of Control Flow action. It will cycle through each file that you pass to it and perform the same series of actions on each one. I need it to:

  • Save the file’s extension to a variable so it can be used later
  • Rename the file to the base name plus the current counter value plus the extension
  • Add one to the counter so the next file gets the next number in the sequence

Add a Get File Attribute action from the File Category to the loop in the For Each Item in a Collection action and set it to Get Extension to Variable FileExtension

For From File, copy and paste in %Variable%Path%. This tells Keyboard Maestro to store the extension of the file you’ve imported as a variable.

for each action
A For Each Path action is what cycles through each file.

Next add a Move or Rename a File action directly beneath the Get Extension action. Set it to Move %Variable%Path% to %Variable%BaseFilename%-%Variable%FileNameCounter%.%Variable%FileExtension%

Pay attention to the punctuation; there is a hyphen between the call for the BaseFilename variable and the call for the counter and there is a full-stop (period) between the counter and the extension.

renaming files
This action actually renames the files.

The final thing to do is have the FileNameCounter variable cycle by one. Add a Set Variable to Calculation action and have it Set Variable FileNameCounter to FileNameCounter+1.

increment
Without incrementing the counter, each file would end up with -1 as the suffix.

Now all that remains is to test it. 

Select a group of files in Finder and press the Hot Key trigger. Enter something like Test for the name and they should all get renamed to a sequence.

results
The results of running the macro. I’ve renamed a group of IMG_XXX files to Test-1, Test-2, etc. in just a few seconds.

Going Further

This is obviously a very simple macro but it covers the basics of working with files in Keyboard Maestro. In the next few tutorials I’ll start to dig deeper into how you can manipulate files with your macros. 

For the time being, however, you now know enough to load files into Keyboard Maestro, rename them and record file attributes. Combined with the information from the other tutorials, you should be starting to get a feel for what you might be able to accomplish.