How to create an Apple Script to search

jasetech

Registered
Hi all,

I am looking to create an Apple Script to search through the console log for a certain string of text, but am not having much success.

Specifically, I would want the script to search through the system log, and if it finds a string of text it would use the say command to say that it found the string. Conversely, if it does not find the search string it would use the say command to say it didn't.

Any ideas?
 
Here's one way to do it:
Code:
set x to 0
try
	set x to (do shell script "grep -c 'TEXT TO FIND' /var/log/system.log")
end try
if x > 0 then
	say "The string was found."
else
	say "No matches were found."
end if

I use a shell script within an AppleScript to search for the text in the file. There's surely a way to do this in "pure" AppleScript, but using grep to search files seems natural to me. I use the "try" statement there because grep returns an error if zero matches are found (or maybe that's a bug in AppleScript). Once run, X will contain the number of occurrences of the string found in the file.
 
Back
Top