search for string in text edit

Hi there is a thing I would like to do, I want to search for a string in a text edit file (.txt) without opening it and display a message if the string can’t be found. Is it something I can do?

example in the file, the strings look like that

DESIGN PGM PRESS
DGN 25635 PGM 25149 996 UV
DGN 64636 PGM 69458 XL 105

there is a between each item

Thanks for any advises

You may try:

set theFile to choose file of type {"public.plain-text"}

set fileContents to read theFile as «class utf8»


set theKeys to {"DESIGN	PGM	PRESS", "DGN 25635	PGM 25149	996 UV", "DGN 64636	PGM 69458	XL 105"}

set missing to {}
repeat with akey in theKeys
	if fileContents does not contain akey then set end of missing to (akey as text) # Don't remove the late coercion
end repeat

set cnt to count missing
if cnt > 0 then
	set msg to my recolle(missing, linefeed)
	if cnt > 1 then
		set msg to "the keys" & linefeed & msg & linefeed & "are missing"
	else
		set msg to "the key" & linefeed & msg & linefeed & "is missing"
	end if
	display dialog msg
end if

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 28 octobre 2019 12:21:19

Hello Yvan, thanks for replying, I’ll give more explanation on what the script should do.

  1. The user enter a DESIGN NUMBER in a dialog box, then a PGM NUMBER in another dialog box and then from a list, select the press for wich he’s looking.

From that…

  1. It looks in the text file if what the user entered, already exist. If not the user has something to do, if yes then he doesn’t.

Cause actually I don’t understand what the code you wrote should do :expressionless:

Merci!

I think I get it, no need to go any further for now… :slight_smile: