Thursday, July 20, 2017

#1 2009-09-29 01:21:02 am

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

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?

Applescript:

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

"kMDItemContentCreationDate     = 2007-09-24 10:27:33 +0530
kMDItemContentModificationDate = 2007-09-24 10:27:33 +0530
kMDItemContentType             = \"public.folder\"
kMDItemContentTypeTree         = (
    \"public.folder\",
    \"public.directory\",
    \"public.item\"
)
kMDItemDisplayName             = \"Activate Fonts.action\"
kMDItemFSContentChangeDate     = 2007-09-24 10:27:33 +0530
kMDItemFSCreationDate          = 2007-09-24 10:27:33 +0530
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                    = \"Folder\"
kMDItemLastUsedDate            = 2007-09-24 10:27:33 +0530
kMDItemSupportFileType         = (
    MDSystemFile
)
kMDItemUsedDates               = (
    2007-09-24 00:00:00 +0530
)"


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com


Filed under: mdfind

Offline

 

#2 2009-09-30 07:30:26 am

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

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


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#3 2009-09-30 08:28:20 am

BLUEFROG
Member
Registered: 2003-02-17
Posts: 273

Re: mdfind to search folders only

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

Offline

 

#4 2009-09-30 09:47:14 am

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

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:

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.


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#5 2009-09-30 09:56:07 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11453
Website

Re: mdfind to search folders only

As far as I know there is no special key to distinguish folder from package folder, so either you filter the items like
[code]…whose name does not end with ".app"[/code]
or in a repeat loop


regards

Stefan

Offline

 

#6 2009-09-30 10:12:02 am

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

…whose name does not end with ".app"


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.

Applescript:

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.

Last edited by chris2 (2009-09-30 10:45:19 am)


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#7 2009-09-30 01:35:39 pm

BLUEFROG
Member
Registered: 2003-02-17
Posts: 273

Re: mdfind to search folders only

chris2 wrote:

Till as recently as last month, I stayed away from mdfind thinking Spotlight finds my files quickly, why bother?


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! big_smile)

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

Offline

 

#8 2009-09-30 02:03:39 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4658

Re: mdfind to search folders only

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.


iMac running OS X 10.11.6

Offline

 

#9 2009-09-30 08:45:40 pm

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

Hi,
Thanks for the reply.

mdfind -onlyin . 'kMDItemContentType==public.folder'


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


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.

Last edited by chris2 (2009-09-30 08:50:52 pm)


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#10 2009-09-30 09:06:31 pm

BLUEFROG
Member
Registered: 2003-02-17
Posts: 273

Re: mdfind to search folders only

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

Offline

 

#11 2009-09-30 09:20:59 pm

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

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


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#12 2009-09-30 09:31:27 pm

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

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:

Applescript:

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

Last edited by chris2 (2009-09-30 09:42:37 pm)


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#13 2009-10-01 01:09:08 pm

BLUEFROG
Member
Registered: 2003-02-17
Posts: 273

Re: mdfind to search folders only

Chris2:

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

Also when I run this…

Applescript:

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…

Applescript:

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

Last edited by BLUEFROG (2009-10-01 01:12:41 pm)


Filed under: Finder

Offline

 

#14 2009-10-01 01:59:45 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11453
Website

Re: mdfind to search folders only

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


regards

Stefan

Offline

 

#15 2009-10-01 06:47:45 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: mdfind to search folders only

BLUEFROG wrote:

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


This works fine for me on Snow Leopard.

Offline

 

#16 2009-10-01 08:34:30 pm

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: mdfind to search folders only

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

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


Applescript:

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\"

Applescript:

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?


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#17 2009-10-02 01:53:54 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11453
Website

Re: mdfind to search folders only

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


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)