Automate finder "name contains" search

Since I upgraded to macOS High Sierra, I found that Spotlight search for filenames doesn’t work as good as it did in previous macOS versions. I submitted a bug report to Apple some months ago, but I don’t expect a reply or fix soon…

So Keyboard Maestro to the rescue! (i hope)

The problem:
In attachment I made a selection of 23 files:
example (12.9 KB)

I made them empty (zero byte) files because only the filename is important here.

Let’s search for files with venti and system in the filename:

This should return all 23 files, but since the upgrade to High Sierra this returns only 11 files. This is a serious problem in our workflow. I use filename search all the time.

As a workarroud, I can search using name contains:

This brings up all 23 files as expected. But it’s very cumbersome compared to simply typing it in the searchbox. So I want to automate this using Keyboard Maestro.

Whenever I type a search query in the search box and hit CTRL+ENTER, it should trigger the KM Macro. I can split the string into different variables, and let KM add a “name contains” row in the finder window for each variable. But that involves a lot of simulated clicks.

Any ideas for a better solution, with less GUI scripting?

One idea is to use the powerful mdfind in terminal, but I’m not sure if and how search results can be displayed in the finder window.

Any help is greatly appreciated!

Have you tried typing this in the Search box:
name:venti name:system

I have found that Spotlight Search in the Finder (even on Sierra) can become confused if you don't enter that search criteria as it expects.

In addition to that, I usually just enter one search term, like name:venti, then arrow down to select the proposed term; then do the same for the second term. A bit more work, but it provides better results.

Here's an animated GIF showing this:


Thanks for the suggestions, but none of these provide the expected results. In fact, I’m quite familiar with the Spotlight syntax and different ways of inputting search queries. Consider myself a Spotlight junkie ;).

I’m not sure about the exact cause and in which macOS version it was introduced, but Spotlight on High Sierra seems to have trouble with some (but not all) files containing dashes. Replacing every dash in the filenames with underscores will provide correct search results using the regular search box input.

Unfortunately, we have used a file naming conventions using dashes for many years. The only way to have reliable search results is, afaik, to use “name contains”. Either in Finder (with a whole lot of clicking) or mdfind (which serves the results in terminal).

What I’m hoping for is a fast, reliable KM macro for serving search results of a “name contains” query in a finder window.

Ah! Wish you had mentioned that in your OP.

I can confirm this behavior in macOS 10.12.6.
I am investigating possible workarounds for this.

Yeah, should have mentioned it. It’s more of a macOS support topic, but my hopes for getting this fixed by Apple aren’t too high. Judging from many long-standing issues I read online, it seems Spotlight isn’t a very big priority for Apple. Which is too bad because it’s such a powerful feature. It’s also the main reason we chose a mac server above a windows server for our workgroup, for blazing fast indexed search on the network drive .

Hopefully KM can offer a good workaround.

Hey @dmduco,

This is all UI scripting, but it works.

Accessing Finder submenus

If you want something more powerful and flexible then I suggest you try out HoudahSpot.

It's a front-end for Spotlight that has many useful features.

Other possibilities are:

A) Create a smart folder on-the-fly.

B) Use Raw Spotlight Query directly in the Finder.

	kMDItemFSName == "*venti*"c && kMDItemFSName == "*system*"c


I reckon my macro above could be adapted to perform (B) with reasonably speed.


Your suggestion using a Raw Spotlight Query is great! Didn’t know this was possible in Finder.

Type in some search terms in the search box and hit CTRL + Return.

The macro then converts this into a Raw Spotlight Query to find all files with filenames containing some, search and terms.

I will also have a look at Houdahspot, but for now this fix is workable for me.

Thanks for the help!

Spotlight search "name contains" using Raw Query.kmmacros (18 KB)