Page 1 of 1
Reading from text file
Posted: Thu Jun 25, 2009 12:00 am
by saratogacoach
Hi,
I have been trying to read the contents of a text file into a variable and cannot get the script in a button to work. I wrote the word "hello" into the first line of this text file, saving it in the default folder where the stack is located.
Code: Select all
on mouseUp
open file "holder.txt" for read
read from file "holder" at -1 until EOF
put it into xTemp
answer xTemp
end mouseUp
My answer box is empty. I've tried other variations like read from file "holder.txt" and even put URL into xTemp, but with no success.
I must be doing something wrong.
Any help would be appreciated.
Kind Regards,
Posted: Thu Jun 25, 2009 12:19 am
by BvG
the main problem with opening files is the path, so i suggest to test wetter your file exists:
note that rev generally doesn't even know what a file extension is, so leaving it out will certainly not work.
the second problem with working with files is to use the open/read/close file commands. instead, use the url ones:
Code: Select all
on mouseUp
answer file "a text file"
if it = "" or the result = "cancel" then
beep
exit mouseUp
end if
put it into thePath
if there is no file thePath then
beep
exit mouseUp
end if
put url ("file:" & thePath) into theFile
answer theFile
end if
I'd also suggest using "put theFile" instead of "answer theFile" but that's certainly a matter of taste...
Posted: Thu Jun 25, 2009 1:24 am
by saratogacoach
Hi Björnke,
Thanks for your suggestions.
What finally worked was using the URL and putting the variable into the file.
I thought I had tried that, only turning to a more convoluted approach involving opening and reading the file, when that didn't seem to work.
(I probably was not scripting the URL option correctly.)
Thanks for your help.
Kind Regards,
Posted: Thu Jun 25, 2009 1:08 pm
by Klaus
Hi Coach,
your first script was OK, but you started with the offset "... at -1" which is the end of the file, so you got nothing

AND you did not add the suffix in the line: "read from file...", know what I mean?
...
open file "holder.txt"...
read from file "holder"...
..
So you were referring to two different files.
Best
Klaus
Posted: Thu Jun 25, 2009 1:27 pm
by saratogacoach
Thanks, Klaus.
Yes, I understand this better now.
(Learning this slowly but surely.

)
Kind Regards,