Smart folder filter "does not contain"
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.
Solution 1:
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
Solution 2:
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