Search for text in multiple files

I’d like to be able to search multiple files of varying file types in scattered folders for a specific string. I have searched through dozens of examples of scripts on this site, but I haven’t been able to find a script that works for me. Any help would be appreciated. Thanks.

Can you just loop through the files, read their contents, and search for the desired string there?

I’m sure more advanced solutions are possible.

There are many options, but you first have to decide on a certain type of file and see exactly what the AppleScript code looks like, we have Files: TEXT, RTF, PDF, PAGE, NUMBER and many more.

On a Mac, Spotlight already does that in a Finder window. Why use Applescript?

After you find the files, you can process them in some way. Also, by default, spotlight finds all the files that match, whereas —per the opening post— you may wish to restrict the files to certain directories. There isn’t much information to go on but here is a hypothetical situation.

You want to restrict the search to four folders (and their subfolders) but they are scattered, for example, as below. You could keep the path list in a text file.

/Users/username/Downloads/cli
/Users/username/Documents/noso-yr2024/mo2024-02
/Users/username/Documents/nonsorted-years/noso-yr2023/mo2023-02
/Users/username/Documents/nonsorted-years/noso-yr2022/mo2022-02/size script

You could read the file, like so…

use scripting additions
set pathsFile to alias "MacHD:Users:username:tofind:pathlist" as text
--> "MacHD:Users:username:tofind:pathlist"

set eaPath to paragraphs of (read file pathsFile)
--> {"/Users/username/Downloads/cli", "/Users/username/Documents/noso-yr2024/mo2024-02", "/Users/username/Documents/nonsorted-years/noso-yr2023/mo2023-02", "/Users/username/Documents/nonsorted-years/noso-yr2022/mo2022-02/size script"}

Then create a string of quoted paths to use in the shell:

set pathList to {}
repeat with x in eaPath
	set end of pathList to quoted form of x
end repeat
pathList -- list of quoted paths for find
set text item delimiters to space
set paths to pathList as text
--> "'/Users/username/Downloads/cli' '/Users/username/Documents/noso-yr2024/mo2024-02' '/Users/username/Documents/nonsorted-years/noso-yr2023/mo2023-02' '/Users/username/Documents/nonsorted-years/noso-yr2022/mo2022-02/size script'"

You can now run a combination of find and grep on this set of directories

set cmd2 to "find " & paths & " -name '*txt' -print0 | xargs -0 fgrep -l 'size' "
set finalParas to do shell script cmd2 without altering line endings
set text item delimiters to linefeed
set phase2 to reverse of (rest of (reverse of (text items of finalParas)))
--> list of posix file references, extension 'txt', containing string `size`

Now you have a list of matching files. You can open them in a specific app or read/write them directly.

NB My drive isn’t indexed so I can’t use mdls. With mdls, you can find text within a wider range of document types (eg .numbers) and open them in their normal application and process them accordingly.

By using applescript, you can automate much (or all) of the task.

Thanks very much - that’s very helpful!