13

Smart Folder Exclude Folder

Ultimately I want the result of this find command to be a smart folder.

The criteria aren't that complicated:

  • name should be "README.md"
  • type should be file
  • path should not contain "node_modules"

find /Users/me/Documents -type f -name README.md -not -path "*/node_modules/*"

The problem is that the smart folders criteria operator list seems to be lacking a does not contain option.

The available options are:

  • matches
  • contains
  • begins with
  • ends with
  • is
  • is not

Is it possible to accomplish this and if so then how?

Edit 1

I found that by holding the option key I am able to add a negation clause to the smart folder search criteria but I cannot seem to successfully exclude the node_modules folder. It is unclear which criteria to use but none of the ones I have tried seem to work:

  • Document Container
  • Containing Folder Names
  • Folder Name

I have tried combining these with the following operators:

  • contains
  • matches

and with the following terms:

  • node_modules
  • node_modules

in case it supports wildcard searches.

I have tried all combinations of the above filters, operators and terms.

The documentation is so poor on the subject.

enter image description here

  • 2
    Have you found a solution? – gota Nov 27 '15 at 10:41
2

It looks like kMDItemPath can't do what you need:

no-results-in-spotlight-in-searches-against-kmditempath

Some potential alternatives are discussed here:

how-to-locate-a-file-in-spotlight-using-folder-and-file-name

  • 1
    Thanks but I already have a command line function that retrieves the files I am interested in and I am making a Smart Folder, not doing a spotlight search. I have come across the limitation of mkdItemPath. – km6zla Jun 4 '14 at 19:27
  • Spotlight Search and Smart Folders use the same internal mechanism. kMDItemPath won't work in either of these user interfaces. – GraniteRobert Jun 5 '14 at 3:19
1

There is a workaround, but it's not very pretty. It will, however, serve your purposes if you just want to access your READMEs in one folder (using the criteria you've specified), and have some notion of where they come from.

The idea is to use your shell script to find the right files, and then collect aliases to each file in one directory. We then rename the aliases to tell us what parent directory the original file belongs to.

The Applescript to do this is below. It looks ugly here, but try pasting it into Script Editor and compile it, and you should be able to see the logic.

--- Set up the name of the folder to contain search results
set myFolder to POSIX file "/Users/me/SmartFolder"

--- Clear the folder of contents. Then we will see only the results of an updated search
tell application "Finder"
    display dialog "WARNING. This will delete all files below " & (POSIX path of myFolder) & " . Are you sure you want to continue?"
    delete (every item of folder myFolder)
    --- alternatively, if you want to keep your script in the same folder, replace the line above with
    --- delete ((every item of folder myFolder) whose name does not end with ".scpt")
end tell

--- The shell command that is doing all the searching
set FileList to do shell script "find /Users/me/Documents -type f -name README.md -not -path '*/node_modules/*'"

--- The rest of the script takes each file and makes an alias in our folder containing search results. The aliases are renamed according to "ParentDirectory_filename"
set FileList to paragraphs of FileList
repeat with CurrentFile in FileList
    set ASCurrentFile to POSIX file CurrentFile
    set PathList to words of (ASCurrentFile as string)
    --- Make the new name include the Parent Directory and file name
    set NewName to (item -2 of PathList) & "_" & (item -1 of PathList)
    tell application "Finder"
        make new alias file at myFolder to ASCurrentFile
        --- We need to save the name/location of the new alias file before we can try to rename it
        set NewAlias to result
        set i to 1
        repeat
            try
                --- Try to rename the alias. Won't work if there's already an alias with the new name
                set name of NewAlias to NewName
                exit repeat
            on error
                --- Append a number to the alias. Increase the number for more duplicates
                if i is not equal to 1 then
                    set NewName to text 1 thru -3 of NewName
                end if
                set NewName to NewName & " " & i
                set i to (i + 1)
            end try
        end repeat
    end tell
end repeat
  • I don't know if this works. +1 for even coming up with it. I'm not willing to go this far to make a readme folder. – km6zla Mar 29 '17 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .