Check if a string contains any of the lines in a text file

Right now i have this hardcoded, but I need it to read from a file.


if theString contains "John" or "Jane" or "William" or "Bob"
        doThis()
else
        doThat()
end if

If possible, I need it to check from a file, and do basically the same thing. The contents of the file would be:

Model: Gen. 1 iMac G5
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

How about this:


set tString to "This text contains the names John, George, and Bob."
set tNames to read alias ((path to desktop folder as text) & "names")
repeat with aName in paragraphs of tNames
	if aName is in tString then display dialog aName & " is in tString"
end repeat

Thanks a lot! It’s so simple, but I just didn’t even think of that. Anyway, to fit it in to the example of the normal if statement in my first post, I did the following:

set stringContainsName to false
set tString to "This text contains the names John, George, and Bob."
set tNames to read alias ((path to desktop folder as text) & "names")
repeat with aName in paragraphs of tNames
	if aName is in tString then set stringContainsName to true
end repeat
if stringContainsName then
	dothis()
else
	dothat()
end if

For your setup codeman93, I’d add in an exit repeat so you don’t keep repeating after you already know that the string contains a name.

repeat with aName in paragraphs of tNames
	if aName is in tString then
		set stringContainsName to true
		exit repeat
	end if
end repeat

Cool! Thanks Bruce. I didn’t know you could do that. :slight_smile:

I had assumed that if the string contained more than one of the listed names, he wanted to know that.