1

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

1

You could also just use grep:

grep -qFi nintendo file.txt && echo found

-q = quiet, -F = fixed strings (disable regex), -i = case-insensitive.

This would search recursively (-r) and list the names of matching files (-l):

grep -rlFi nintendo ~/Notes

You can also restrict mdfind to specific metadata attributes, but it doesn't really help in this case:

mdfind 'kMDItemTextContent="*nintendo*"i' -onlyin ~/Notes/
1

Try:

set fileText to read POSIX file "/Users/John/Desktop/test.txt" as «class utf8»
fileText contains "nintendo"

You must log in to answer this question.

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