searching for a string in a text file

I am a beginner in AppleScript. I want to automate the build process. I found a sample script on how to replace. And it works fine

set the message_text to "My script number 10" 
set the message_text to replace(message_text, "10", "12")
on replace(this_text, search_string, replacement_string) some code 
end replace

But my message_text (the text in which I am searching for the string is a large file.
How would I set the message_text to the content of a file?
Thank you for responces.

Sounds like you’ll need to get a scriptable text editor involved here. (Tex-Edit Plus is excellent, BBEdit is another great one.) Have the text editor open the text file to get message_text. If this doesn’t answer your question, post and I’ll try to elaborate. :slight_smile:
Also, let us know if you’re on OS 9 or X.
T.J. Mahaffey
tj@macscripter.net

on readFile(fileAlias)
	open for access fileAlias returning fileID
	set fileContents to read fileID as text
	close access fileID
	return fileContents
end readFile

set message_text to readFile(alias "[your path here]")

HTH

Here’s your problem: you’ve got the readFile() and replace() calls within the ‘tell application “Finder”’ block. This means that these calls are being sent to the Finder, which doesn’t recognise them. Two solutions:

  1. Move the if…then block outside the tell Finder block:
tell application "Finder" to (exists file "Mac Build HD:Test")
	if result = false then
		display dialog "Missing"
	else
		set message_text to readFile(alias ("" & "Mac Build HD:Test"))
		replace(message_text, "177", "178")
	end if
  1. Use the ‘my’ keyword so that your calls are directed to the script:
tell application "Finder"
	if (exists file "Mac Build HD:Test") = false then
		display dialog "Missing"
	else
		set message_text to my readFile(alias ("" & "Mac Build HD:Test"))
		my replace(message_text, "177", "178")
	end if
end tell

HTH

Thank you for the advice. I am on OS 9.
Tatiana

Thank you. I will try to do what you suggest and will let you know. I think my mistake was that I opened the file itself instead of an alias, it did not give me an error message, but did not work either.

Thank you for you help.

Tatiana.

Hi,
Oviously I am still missing something in my code, it does not work. The 2 subroutines which I am using are they performed within the Scriptable Text Editor?, If yes, then I have to add lines " tell application…", but when I did it I still had an error.

tell application "Finder"
if (exists file "Mac Build HD:Test") = false then display dialog "Missing" else set message_text to readFile(alias "Mac Build HD:Test") replace(message_text, "177", "178") end if
end tell
on readFile(filealias)
open for access filealias returning fileID
set fileContents to read fileID as text
close access fileID
return fileContents
end readFile
on replace(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace

Thank you again.

Thank you very much for your help.
I used the first solution. Now at least I can see the string being replaced in the Result window of the Scriptable text Editor, however the text file on my HD remains unchanged. I probably need to copy the alias content into the original file. Can you please explain why do I have to use the alias instead of the text file?
Tatiana.