How to modify a "search for file within a folder " dynamic macro by the late JMTx to include subfolders

The late @ JMichaelTX wrote an excellent macro to select a file within a folder, with incremental search and autocomplete.

Is there a way to edit the macro to include subfolders ?

thanks in advance for your time and help

MACRO: Choose File From Folder List With Incremental-Search and AutoComplete - Macro Library - Keyboard Maestro Discourse

You could replace the AppleScript Action which generates the list of files with some native Keyboard Maestro Actions that generates the list and includes files in subfolders.

Where it gets a bit tricky is how to keep the list to a list of filenames only (i.e. to avoid having to show the full path in the list but at the same time show part of the path to files in subfolders). Anyway, here is the original Macro modified with a few changes. It shows all files in the main folder as just filenames and for files in subfolders it shows the subfolder name and file.

Modified to include files in subfolders - Choose File From All Files in Target Folder using KM Prompt With List [Example].kmmacros (30.9 KB)

Click to Show Image of Macro

1 Like

the macro is great !. Thank you very much @Zabobon

I carried out some tests and found that:

  • the search function works irrespectively of the order of words in the filename. For example, the macro will find a file called green chicken.txt even if I type in chicken green (shown below). Many file search apps do not allow this. They can't find the file if the words of not in the right order.

  • one small drawback is the fact that paths are truncated when there are multiple levels of nested folders, which is understandable. I created 5 nested levels of dummy folders with a file in the most nested folder. It found the file but the macro displayed only (see below)

    • the 1st and the last level folder in the path
    • the filename at the end.
      The is not a major impediment. If I understand correctly it is not possible to increase the width of the KM file search window

After working with the macro, your addition of subfolders is fantastic and including the path of subfolders is a plus to localize the file.

To open the file, I simply have to click on it.
Ideally, I would also have the option of revealing the file in Finder (I use Forklift as an alternative), namely the option to click on the file while pressing a modifier key to reveal it instead of opening it. I know that this is feasible. I remember coming across macros to do so, but can't track them down. Would you have an idea?.

thanks again very much

(see below)
1- first level parent folder
2 mishmash of intermediate folder names
3- last words of most nested level 5 folder
4- filename

To give yourself an alternative action you can replace the current last Action in the Macro (which at the moment opens the file) with a test to see if a modifiier key is being held down (for example the Option key) and then do something different. Keyboard Maestro has a Reveal Action which will show the item in the Finder. If you wanted to show in Forklift you would put whatever method you have to do that instead.


works perfectly. Absolutely Great !! thanks very much.

1 Like

Hi, I would like to add at the beginning the option to 1- open folder in Finder OR 2- continue on to your macro listing files. Which combinations of actions would be the most efficient / intelligent to do so ?
I would envision an If Then Else action as in If the Key R (for reveal) is down open folder in Finder, else continue to your macro, but the macro has to wait for the user input. I am a bit (very) confused, and there may be much better ways than the IF Then Else.
thanks very much !

I put this at the start of the macro and it seems to work. (trigger macro with or without the ⌘ modifier)
otherwise contains nothing - just continue the macro

1 Like

Yes, it all depends how you want it to work and how the macro is triggered in the first place. There is always a way!

If you were triggering the Macro with a hotkey you could also do it something like this:

1 Like

Thank you for solution .

WOW !! how smart. Very elegant. I will certainly do it that way. I'm glad I asked you. thanks very much

1 Like

But if you were triggering the macro by clicking on it in a Palette, your method would be the best. I use that for some Macros I have that open specific folders. Clicking on the folder name in the Palette opens the folder, but holding down a modifier key while clicking on the same macro in the palette opens an Apple Note about it.

1 Like

Never thought of that. Extremely interesting considering the large number of Bear notes I have and which I would often like to consult. I will start today trying to use it.

Thanks to your help, I am quickly reducing the number of macros by concentrating multiple alternative functions in one macro.

I had another idea, but I hesitate, because you will have me banned from the forum for my relentless questions. So here is my question because I think that it may interest forum members as the swiss knife of folder macros. Please feel free to say you have had enough. I am extremely grateful for everything you have done until now.

In it's present state, I (I mean YOU) achieved algorithm 1.

What if I want to go to algorithm 2 which includes asking the user if subfolders should be included or not in the search.
1- is "recursively" the key to searching subfolders ?
2- if yes, I would have 2 options one using recursively and the other "alphabetically"
In terms of speed, ie in order not to slow down the workflow how should I implement the choice between with and without subfolders ?

thanks again


Yes, exactly.

Technically there is no problem including multiple options but the more options you build in the harder it becomes to keep the Macro simple to use. Once there are many alternative options I would use a Keyboard Maestro Prompt at some point in the flow.

If "OK" is the most used behaviour then hitting the return key will send the Macro that way and clicking on "More options" can be used in the less common cases. It's good because there is nothing to remember apart from the single hotkey to launch the Macro.

The great thing about Keyboard Maestro is that you can build a first draft of your Macro and then see how it works in everyday use.

One suggestion is to make your Macro work as a subroutine (you might already be doing this). That way for each new "folder macro" you would just need to have a single Action to send your folder path to the subroutine Macro. And the complex main macro being a subroutine would make editing it much simpler (rather than having to change lots of Macros).

So, each of your Folder macros would look something like this (with just a single Action):

And your subroutine Macro would be set up like this:

1 Like

I used prompt for user input in the past and abandoned it because I found that it slowed down the workflow too much.

Based on your excellent idea, I am thinking of expanding to 3 different hotkeys to trigger the macro instead of 2.

trigger 1 (D) to search folder + subfolders
trigger 2 (⌘D) to simply reveal the folder in finder

I now want to add option 3 ⌃D which searches the folder without the subfolders.

By changing recursive to alphabetical I can now convert the macro to search without subfolders.

My problem is now that at the beginning of the macro I have to 3 possible routes instead of 2.

I thought I would use an action called "case" but the only one I could find does not seem appropriate. Should I use nested if them else or would you have another suggestion?

thank you

OK, I think that I managed to make switch case work. thanks again
A draft just for testing below.

1 Like

Yes, with Switch case you can have as many alternatives as you want. It is a very useful Action.

As you are using D, ⌘D, ⌃D that is using up quite a lot of hotkeys for one folder. Presumably your macro will only be active at certain special times so you don't lose the normal use of those keys?

yes, all within a dedicated "folder "palette. Thanks to you, I have understood how to use multiple triggers. I need to better find a way to remember the list of tokens. I did not know about %Trigger%. There are so many. thanks again.