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-3.jpg and so forth.
It’s a relatively simple macro but it nicely demonstrates the basics of working with files.
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:
- Keyboard Maestro I: Introduction
- Keyboard Maestro II: Launching Apps Intelligently
- Keyboard Maestro III: Situational Triggers
- Keyboard Maestro IV: Control Flow
- Keyboard Maestro V: Variables
You should also consider looking at my more recent series which covers:
- 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’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.
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.
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.
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.
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.
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.
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.
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.
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post