New to Keyboard Maestro... would appreciate some help towards some finder file manipulation

Hi all. Having some trouble getting into the syntax of KM and would love some help thinking about how to approach this problem. If someone could point me towards some good tutorials for this topic, I'd appreciate it- I don't need code written, just something to read up on to start learning and working!

I would like to write a macro to condense a folder full of sequential files into folders. They are stills from a video that export with the frame number appended to the file name. So think...


in one folder. I would like to create three folders (videoA, videoB, and videoC) and move all files with the common name (save for the last x characters) into that folder.

I think I understand how to call up a folder in the macro sequence and how to write a file name to a variable, but I guess my questions are:

  1. How do I read the filename variable so that a file is moved to a given folder if the first x characters are the same?

  2. How do I sequentially read through the entire folder and break the sequence upon completion?

  3. Broadly, what is the syntax for retrieving a folder or file in one action and then use that as a condition, argument, or variable in another action?

I hope that that makes sense! Very new to this and would love to get my feet under me a bit, but finding it hard to know how and where to start tinkering :slight_smile:

Thanks in advance!

TLDR; Skip Down to the "Getting Started" section.

Once you learn a bit about KM, you will find tasks like this one fairly easy. But, like with all languages, you have to first learn the terms and definitions and syntax of KM. I think you'll find KM easier to learn than most. It is designed for non-programmers, but if you happen to be a programmer, that is to your advantage -- mostly from understanding how to construct the logic of any program, a KM Macro in this case.

So, let me give you some rough pseudo code, to help get you started.
this is not perfect, and I may have left out some small statements (KM Actions).

Do a For Each action with a Folder Contents collection

  • This is like a repeat loop, with each iteration providing the filePath of the next file in that folder.
  • The following are statements (KM Actions) in this "For Each" Action.
  1. The "For Each" will have a loop variable, we'll name "filePath", which is the full POSIX file path to the next file in that folder.
  2. Set a Variable (fileRoot) to the root of the file name of filePath
  3. Use a formula or a Switch or Case action to set a Variable "DestinationFolder" to the destination path for this file
    • For example, if the paths to all of your destination folders look like this:
      ~/Documents/Some Folder/<fileRoot>
    • THEN a simple Set Variable to Text action will work fine. Something like this:
      Set Variable "DestinationFolder to ~/Documents/Some Folder/%Variable%fileRoot%
  4. Now use the Move File Action, something like this:
    • MOVE: %Variable%filePath%
    • TO: %Variable%DestinationFolder%

That's it!

Please feel free to ask any questions about this.

However, none of that may make any sense until you learn the basics of KM.
So, I'd suggest that you start here:


Nickrs, welcome to Keyboard Maestro (KM) and its Forum.
KM is one of the best Mac automation tools available, its Forum is one of the best and friendliest forums on the Internet. Whenever you reach a tough stumbling block trying to use KM, please feel free to post your question/problem here for help.

You will also find this helpful:
Tip: How Do I Get The Best Answer in the Shortest Time?

Getting Started with Keyboard Maestro

For more help, see Getting Started with Keyboard Maestro and the Forum .



As far as learning KM, Iā€™d suggest MacSparky.

1 Like

I think the sort of thing I would do would be:

Make sure you have a full backup, and ideally a duplicate of the working folder as well, since any errors in any of this could be disastrous.