I would like write an applescript to evaluate whether a particular file contains a specified string (perhaps as part of an if… then statement). I want to invoke a shell command using mdfind to do this and I am aware of how to restrict mdfind to find files containing a specified string only in specified directories:
mdfind -onlyin /Users/viktor/Desktop/ nintendo
This command will find all files containing the string "nintendo". Is it possible to somehow query a file rather than a directory? And return true / false or the file itself to indicate that the string is in the text content of the file? Putting a file name after "-onlyin" does not work of course…
Best Answer
You could also just use grep:
-q = quiet, -F = fixed strings (disable regex), -i = case-insensitive.
This would search recursively (-r) and list the names of matching files (-l):
You can also restrict mdfind to specific metadata attributes, but it doesn't really help in this case: