Searching the *Contents* of Files From a KM Search Prompt?

I have a folder that has a number of RTFs that are automatically created while undertaking a task and I often have to use the search field in the Finder window for the folder they are in.

Ideally, I'd like to have a hotkey trigger a search, without having to have that Finder window open. Is there a search option that can find content inside an RTF? I'm not interested in matches of the filenames, only what is in the files.

Is there a facility to do this in KM?

Hey Lloyd,

No, Keyboard Maestro cannot do this natively.

You can using the command line version of Spotlight from a shell action though.

mdfind -onlyin ~/test_directory/ 'kMDItemTextContent == "search text"c'

Personally I'd probably use HoudahSpot with a template to do this, but I already own a copy.


I had a go at that last night, but it all fell apart when I tried passing in KM variables for directory and search text. Have you got an example of that?

Pretty please?

Yes. Keyboard Maestro can read the contents of an RTF file and then do a search of that contents to see if it can find a match. Here is a proof of concept as a starting point - you would need to change the path in the first Magenta Action to the actual path of the folder containing your RTF files. This is just an Example Macro and simply displays a list of the files the searched for text is in - how you make use of the results of the Find would be up to you.

(I tried this Example Macro out on a folder of over a hundred RTF files and it is pretty much instant in its search of their contents.)

EDIT: Updated after @Nige_S's comments in posts below.

EXAMPLE Search a Folder of RTF Files for Contents v2.01.kmmacros (23.5 KB)

Click to Show Image of Macro

1 Like

Bad news I'm afraid -- this breaks if a file has a formatting change in the searched-for text. So searching for "some text" won't find "some text".

I started from the same place as you, found the problem, then started looking at mdfind instead -- there's probably a way to convert the rich text in the variable to searchable plain text but I never got that far...

1 Like

Ah yes. But I think I've fixed that and have updated my Macro.

The interesting thing is that if the contents of the RTF are read directly to a variable (which is plain text) all those RTF codes are retained as text codes. But if it is first read to the Clipboard, the Clipboard has the different versions stored (including one without codes) and then using Keyboard Maestro's Remove Styles Filter seems to select the version that is just plain text without any codes. After that, a search for "some text" will find "some text". I got this by finding this post by @Tom


Ah -- that's the missing link. I did a quick test "Filter"ing the variable directly before moving on to mdfind.

Nice one!

For completeness -- you don't need the variable at all. You can filter the clipboard to "source" and then use it directly with an "If... %SystemClipboard% contains %Variable%LOCAL__Search%".

1 Like

Yes, you're right. I've just updated the example Macro again to work that way.

1 Like

Hey @Nige_S,

Yeah, that doesn't work as expected. It's been forever since I've done one of these, and I've forgotten the pitfalls – so it was a pesky chore.

I eventually resorted to building the command string in a KM variable and then using eval to run it.

Note - this may be relevant for some folks:


Search for RTF File Contents v1.00.kmmacros (9.7 KB)
Keyboard Maestro Export

1 Like


Those playing along at home will have spotted the * in the search text and Chris's comment -- you can also use ? for any single character, ?? for any two, etc. You can also use them at the beginning of the string, so:

prejudice matches "prejudice" but not "prejudiced" or "unprejudiced"
prejudice* matches "prejudice" and "prejudiced" but not "unprejudiced"
prejudice? matches "prejudiced" but not "prejudice" or "unprejudiced"
*prejudice? matches "unprejudiced" and "prejudiced" but not "prejudice"
*prejudice* matches all three

1 Like

Wow, I had not checked replies on this until today - lots of activity! Thanks all, I see you've provided a Macro so I will download and give that a go tomorrow.