mdfind to search folders only

I was writing a script to search for folders only and I found “.action”, “.nib”, “.mailstationery” etc files in the results. I used mdls to find something in the results that could help me distinguish them from folders. I can exclude them later using grep or adding kMDFSName to exclude those extensions but I would like to know if I can do it directly. This is because I cannot be sure about how many such extensions I have to exclude. Any ideas?

set ifolder to "path to:System:Library:Automator:Activate Fonts.action:"
set qu to quoted form of (POSIX path of ifolder)
do shell script "mdls " & qu

–>result is

Does everyone hate “mdfind” ? Why is it so hard to find answers to mdfind related queries in this forum?

The syntax is not as approachable as people would like and there is plenty of misunderstanding about query construction that leads people to think it doesn’t work. It’s not perfect but the syntax is incredibly logical. I use it literally every day to do all kinds of weird stuff.

Also, remember this. you’ll only find things Spotlight has indexed. Many people cry “Foul!” over failed queries when (1) they’re looking at unindexed (or unindexable) locations, or (2) their Spotlight index may need to be rebuilt. OT, but just FYI.

Jim

I have got used to the syntax now. It’s not that complicated. I had some trouble because i was not familiar with shell scripting and hence did not use the quotes & ampersands properly.

The issue of rebuiliding the index which you talked about must be the real issue. But one who knows the advantages of using mdfind will go to any extent to fix it.
Till as recently as last month, I stayed away from mdfind thinking Spotlight finds my files quickly, why bother?

Then I saw some of the examples using ical, iTunes and address book here:

http://macscripter.net/viewtopic.php?pid=96813#p96813

and I realized what mdfind can do.

I am so keen to write such queries myself. There are so many different things that can be done with mdfind. I guess, people think of mdfind as just another search utility to find their files and folders. That is probably, why it is not so popular.

As far as I know there is no special key to distinguish folder from package folder, so either you filter the items like

.whose name does not end with ".app"

or in a repeat loop

Hi,
Thanks for the reply.

If you see my first post in this thread, i have mentioned automator actions, nib files and mailstationery files, they won’t be excluded by excluding “.app” from the results of the script below.

set thequery to "action"
set thecmd to "mdfind  'kMDItemDisplayName == " & quote & thequery & "*" & quote & "wc" & " && (kMDItemKind == \"Folder\")'"
do shell script thecmd

I guess, I will keep using mdfind and find out the items i need to exclude(using grep) and keep updating my script with time.

Actually, Spotlight is just the front end to mdfind. Whether you’re constructing Raw Queries in a Find window or using the Spotlight search bar, they are one and the same. Spotlight is just less nerdy (and a bit less powerful in the search bar - though there are neat tricks there too! :D)

BTW, if you’re looking only for folders.
mdfind -onlyin . ‘kMDItemContentType==public.folder’

You should use -onlyin as much as possible since narrowing your searchScope usually returns more useful results. (Note: I am using “.” for the current directory (you can use $PWD as well) or you can push the necessary paths in as needed.)

Cheers!
Jim

I too use mdfind -onlyin … all the time. To me the big advantage is that the paragraphs of the result are a list of all the file paths that met the criteria and I can further boil them down or convert them to posix files, etc.

Hi,
Thanks for the reply.

In my first post in this thread, I have posted the mdls result for “.action” file.
That too has a kMDItemContentType of “public.folder”

To me there are multifold advantages, because the results of mdfind appear in my Quicksilver window. I can rename, delete, move, copy, set comment etc and most importantly, select and run any applescript on mutliple items of the results at once avoiding the need to write separate scripts for each of the actions.

Chris:

You appear to be getting incorrect information in your mdls. What OS are you on? (I am on 10.5.8 so it could be an OS issue if you’re on 10.6.x)

This is my return on the same action.
kMDItemCFBundleIdentifier = “com.apple.Automator.ActivateFonts”
kMDItemContentCreationDate = 2008-05-31 05:59:07 -0400
kMDItemContentModificationDate = 2008-05-31 05:59:07 -0400
kMDItemContentType = “com.apple.automator-action”
kMDItemContentTypeTree = (
“com.apple.automator-action”,
“com.apple.plugin”,
“com.apple.bundle”,
“public.directory”,
“public.item”,
“com.apple.package”
)
kMDItemDisplayName = “Activate Fonts.action”
kMDItemFSContentChangeDate = 2008-05-31 05:59:07 -0400
kMDItemFSCreationDate = 2008-05-31 05:59:07 -0400
kMDItemFSCreatorCode = “”
kMDItemFSFinderFlags = 0
kMDItemFSHasCustomIcon = 0
kMDItemFSInvisible = 0
kMDItemFSIsExtensionHidden = 0
kMDItemFSIsStationery = 0
kMDItemFSLabel = 0
kMDItemFSName = “Activate Fonts.action”
kMDItemFSNodeCount = 1
kMDItemFSOwnerGroupID = 0
kMDItemFSOwnerUserID = 0
kMDItemFSSize = (null)
kMDItemFSTypeCode = “”
kMDItemKind = “Action”
kMDItemLastUsedDate = 2008-05-31 05:59:07 -0400
kMDItemSupportFileType = (
MDSystemFile
)
kMDItemUsedDates = (
2008-05-31 00:00:00 -0400
)

Jim

Thanks Jim. Actually, I am also using 10.5.8

I think, its time to rebuild my index as chrys suggested in this post (when “!=” wasn’t working for me for some of the queries):
http://macscripter.net/viewtopic.php?pid=118669#p118669

wait …

I had an applescript to get location of any selected file.

It returns this for files: “Leopard:Users:chris:Desktop:Picture.png”
It returns this for folders: “Leopard:Users:chris:Desktop:untitled folder:”

It returned me “Leopard:System:Library:Automator:Activate Fonts.action:” this for the automator action.

When I changed it to “Leopard:System:Library:Automator:Activate Fonts.action” (removed the last semicolon), I saw mdls results like you.

But that still doesn’t explain why mdfind should show automator action results with this script:

set thequery to "slide" 
set thecmd to "mdfind  'kMDItemDisplayName == " & quote & thequery & "*" & quote & "wc" & " && (kMDItemContentType == \"public.folder\")'"
do shell script thecmd

Can you please run this above script (if you want to use onlyin then target the same automator folder in /system/library) and see if you find automator actions in the results

Chris2:

I ran the script as given and I did not see any Automator Actions in the return.

Also when I run this.

tell application "Finder"
	set t to (selection as alias)
end tell

. it returns: alias “Macintosh HD:System:Library:Automator:Activate Fonts.action:” (with the trailing colon)

Adding this.

do shell script "mdls " & (quoted form of (POSIX path of t))

. to the script yields the same results as I posted earlier. It lists as an Automator Action, not a folder.

Jim

The problem could be, that the folder System is not indexed by Spotlight, so maybe it returns a generic folder

This works fine for me on Snow Leopard.

Many thanks for all the replies. I appreciate your help.

set t to "Leopard:Users:chris:Desktop:Activate Fonts.action:"
do shell script "mdls " & (quoted form of (POSIX path of t))

–>kMDItemContentType = "com.apple.automator-action"

set t to "Leopard:System:Library:Automator:Activate Fonts.action:"
do shell script "mdls " & (quoted form of (POSIX path of t))

–>kMDItemContentType = "public.folder"

Now the question is: Why does it appear in my mdfind search results if it is not indexed?

Any instructions on how should I rebuild my Spotlight index to understand what is getting indexed and what is not?

for /System/. I recommend to use /usr/bin/find instead