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.
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?
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.
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...
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%".
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
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.