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
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.
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:
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.
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.)
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.
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.
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