Help with read and write

So, I have some code to write text to a file. This file will keep preferences for another script.

Here’s what I have:

on WriteFile(theData, TargetFile, theAppendBool)
		set the TargetFile to the TargetFile as text
		set the openTargetFile to ¬
			open for access file TargetFile with write permission
		if theAppendBool is false then ¬
			set eof of the openTargetFile to 0
		write theData to the openTargetFile starting at eof
		close access the openTargetFile
		return true
	on error
			close access file TargetFile
		end try
		return false
	end try
end WriteFile

WriteFile( ("Name" & return & "Ralph" & "return" & Nickname & "Ralphus"), (pref_folder & ":" & "prefs.txt", false)

This works just fine. I get a file called prefs.txt with the text I wrote in the specified directory. Now I would like to parse the same file with something like this:

set theLines to paragraphs of (read alias (pref_folder & ":prefs.txt"))
repeat with tLine in theLines
if tLine is equal to "Name" then
display dialog "Found name!"
end if
end repeat

I can include an open statement here, and it opens the file–that is not the issues. This however never enters the true component of the if statement. I have tried just about all forms of casting tLine and doing string comparisons to no avail.

Any help would REALLY be appreciated.

I made a quick text document with the same contents as what you’re writing and this worked for reading it back

set filepath to (path to desktop as Unicode text) & "test.txt"
set fileRef to (open for access file filepath)
set thefile to read fileRef
set para to paragraphs of thefile
repeat with i from 1 to (count of items of para)
	if item i of para is "Name" then
		display dialog "name found"
	end if
end repeat
close access fileRef

Hope that helps.

EDIT: myndcraft posted first - while I was hunting and pecking.

This should work:

set theLines to paragraphs of (read alias (pref_folder & ":prefs.txt"))
repeat with tLine in theLines
	set tLine to tLine as string
	if tLine is equal to "Name" then
		display dialog "Found Name!"
	end if
end repeat

Alright–thanks for the help thus far. I actually tried making the file–instead of having it produced by applescript, and the reading works. It must be a problem with writing. Interesting enough, when I open the files written with TextMate, they appear with spaces (ie Name becomes N a m e). However, they look fine in text edit.

Can someone send a quality write function?

May be mising some functionality you want (which you can add), but this works

set filePath to (path to desktop as Unicode text) & "test.txt"
set LineWrite to ("Name" & return & "Ralph" & return & "Nickname" & return & "Ralphus")
WriteFile(LineWrite, filePath)

on WriteFile(LineWrite, filePath)
	set fileRef to (open for access file filePath with write permission)
	write LineWrite to fileRef
	close access fileRef
end WriteFile

Thanks for the reply, but this still does not work on my machine. Did you confirm that the reader works on this writer’s output?

It just worked on mine, reading too.

For the sake of my own sanity, lol, can you post the exact read & write scripts your using at the moment? The only thing I can think of is you’ve changed the write line or something slightly and thats why the results are coming back wrong.

The original problem here, ralphus, was that in the x in y form of repeat loop, x is actually a reference. While the repeat with x from 1 to (count y) form works fine, you could also simply extract the reference’s contents:

if tLine's contents is "Name" then

Looks like theData’s class might actually be Unicode text - in which case, in your read routine, try replacing:

set theLines to paragraphs of (read alias (pref_folder & ":prefs.txt"))

… with:

set theLines to paragraphs of (read alias (pref_folder & ":prefs.txt") as Unicode text)

However, I can’t see any reason why myndcraft’s write handler doesn’t work for you - since reading the file works fine here, too. (You could slip in a ‘set eof fileRef to 0’ statement after the ‘open for access’ statement - but that shouldn’t make a difference if the file was newly created in the first place…)