Sort based on matching filename to (dynamic) subfolder name?



I'm a bit of a neophyte at this amazing program, and have zero scripting skill. Though I'm beginning to think I need to get some.

I do a lot of scanning, and most of what I scan gets sorted into one of 20-30 client subfolders. I add a client name or file number to every file as it's saved by the scanner, but I still have the dreary task of sorting them all into their appropriate folders from time to time. I haven't found a better way to speed this up than to add a Hazel rule corresponding to every single new folder name as they're added, but this winds up being really cumbersome.

The grail would be a KM folder watcher that compares each new file to a dynamic subfolder list that it queries, and sorts the new file into the matching subfolder (if it finds a match).

Is this possible? Is it easy, and I'm just a dope that I can't for the life of me figure out how to do it?




Hey Luc,

Yes, it's possible.

Is it easy? Nyet.  :sunglasses:

Please provide a couple of real-world examples of a file name and its corresponding sort-folder.

This will make it easier to advise you.

BTW -- If you haven't read this it's worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?



Thank you Chris!

Version 8.2.4
macOS 10.14.2

I maintain 20-30 open client files.

Each open file has its own folder, named with a unique convention, like ~/OPEN/CLIE0001, ~/OPEN/ANTT1234, and so on. Additionally I maintain a dozen or so other folders that regularly take input.

Once a day (if I’m being good):

  1. I scan a couple dozen physical docs into a dedicated folder, call it ~/DOCUMENTS/SCAN. Each of these is associated with one of the open client files.

  2. At some point in the scanning workflow I apply the unique convention to each file: ANTT1234_20181128.pdf. The only reason I do this is to facilitate sorting.

  3. Then I sort them all into their various folders, i.e. DOCUMENTS/SCAN/ANTT1234.20181128.PDF → ~/OPEN/ANTT1234/ANTT1234_20181128.pdf

This process, besides being tedious, means I wind up with redundant info in the filenames (no need to have the folder and each file in the folder named the same way).

I would like to automate as much of this process as possible. I could have a Hazel rule pertaining to each of these folders, but I’d have to change them with every new open file and it would be a headache.

I have been experimenting with KM for possible solutions—maybe a folder watcher that, upon detecting a new file, queries the ~/OPEN folder and populates the subfolders into a palette, then moves the file and applies an appropriate filename upon selection.

But I wouldn’t want to create this palette manually, folder by folder, and have to change it with every client file added or closed. For the same reason, if it’s very hard to knock something like this together it’s probably not worth doing. I’m probably better off just adding a bunch of Hazel rules to my file-opening workflow.

Thanks again!!


Hey Luc,

As long as your scanned files and their destination folders have a shared unique prefix, it will be fairly easy to do the sorting.

The best way would be to activate the macro manually (or periodically) -- probably.

Folder-watcher triggers are great, but they have their pitfalls.

Let's get a manually activated macro working and then you can fiddle with a folder-watcher trigger if you want to.

Essentially the workflow is:

A) Read the file names of the ~/DOCUMENTS/SCAN folder.

B) For each file found:
       - Extract the unique prefix.
       - Scan the ~/OPEN folder for that prefix.
       - Move the file.

This can be done:

In any case the task is very doable and not overly difficult.



Thank you! Will report back.


Hey Luc (@antexit),

I had a little extra time today and wrote a demo macro using AppleScript, since that was the easiest and least time consuming method (for me).

I didn't feel like writing up instructions, so I made a quick demo video (on YouTube see below).

The macro may require some slight adjustments to work with your coding system, and I'm happy to help if that's necessary.


Move Coded Client Files From Collation Folder to Coded Client Folders v1.00.kmmacros (8.5 KB)

Demo Video on YouTube:


Holy cow, this is incredible. I was spending too much time with KM and took a break from lurking on this forum. Thanks so much, Chris, this is unreal.

Will report back.