Write to text file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Write to text file
I created a stack. There are five option menus and one slider on the stack. I would like to write the values of all the option menus and the slider to a text file, one line per item. I used write label of btn "btnName" to file "fileName" & return to write the value of the option menus, and write thumbPos of scrollbar "sliderName" to file "fileName" & return to write the value of the slider.
After running the scripts, I looked at the file. It seems ok except that there is one value with double lines. I tried to delete that option menu and make it again, but there is no different. I don't know what is wrong with my stack. I am using Mac OS 9.
Any idea?
Proza
After running the scripts, I looked at the file. It seems ok except that there is one value with double lines. I tried to delete that option menu and make it again, but there is no different. I don't know what is wrong with my stack. I am using Mac OS 9.
Any idea?
Proza
It's most likely something in your stack. Most notably, the following line won't work:
you probably want something like this:
Also note that labels of buttons can have several lines, which might throw your approach off.
Code: Select all
write label of btn "btnName" to file "fileName" & return
Code: Select all
open file "fileName" for append
write the label of button "btnName" & return to file "fileName"
close file "fileName"
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
I am sorry that when I typed the above message, there was some typo errors. The & return is before the to file.
Today, I made a brand new stack to test again. I made three option menus named "btn1", "btn2" and "btn3". The choices of btn1 are 2,3,4,5. The choices of btn2 are 1,2,3,4,5. The choices of btn3 are +,+/-.
I only changed the name, the menu items, the font, the font size, and the size of the option menus.
I also made a push button for testing:
on mouseUp
put specialFolderPath( "Preferences") & "/test" into theFile
open file theFile for write
write label of btn "btn1" & return to file theFile
write label of btn "btn2" & return to file theFile
write label of btn "btn3" & return to file theFile
close file theFile
open file theFile for read
read from file theFile for 1 line
set label of btn "btn1" to it
read from file theFile for 1 line
set label of btn "btn2" to it
read from file theFile for 1 line
set label of btn "btn3" to it
close file theFile
end mouseUp
I picked 2 from btn1, 3 from btn2, and + from btn3. When I clicked at the push button the first time, nothing happened. That's mean the file was created without problem. When I tried the second time, option menu btn2 changed to nothing, and option menu btn3 changed to 3. This means that the value of option menu btn1 was written in double lines.
Today, I made a brand new stack to test again. I made three option menus named "btn1", "btn2" and "btn3". The choices of btn1 are 2,3,4,5. The choices of btn2 are 1,2,3,4,5. The choices of btn3 are +,+/-.
I only changed the name, the menu items, the font, the font size, and the size of the option menus.
I also made a push button for testing:
on mouseUp
put specialFolderPath( "Preferences") & "/test" into theFile
open file theFile for write
write label of btn "btn1" & return to file theFile
write label of btn "btn2" & return to file theFile
write label of btn "btn3" & return to file theFile
close file theFile
open file theFile for read
read from file theFile for 1 line
set label of btn "btn1" to it
read from file theFile for 1 line
set label of btn "btn2" to it
read from file theFile for 1 line
set label of btn "btn3" to it
close file theFile
end mouseUp
I picked 2 from btn1, 3 from btn2, and + from btn3. When I clicked at the push button the first time, nothing happened. That's mean the file was created without problem. When I tried the second time, option menu btn2 changed to nothing, and option menu btn3 changed to 3. This means that the value of option menu btn1 was written in double lines.
Hi proza,
please let me first give the advise to also read related threads here!
"newbie question; write to file, read from file?" already has much info about this topic!
I do not know what exactly is going wrong in your script but I suggest that you use another syntax to read and write things to file(s).
Using the "url" syntax**** is much more readable and much less to type, because with that syntay you do not have to "open" the file before reading/writing and "close" the files after you are finished. Rev will do that for you
***Check the docs for "URL"!
The basic syntax is for writing:
...
put "text or whatever goes here" into url("file:" & "path/to/file/here.txt")
..
for reading:
...
put url("file:" & "path/to/file/here.txt") into fld "your field here"
## or any variable...
...
As you see, this can be done with ONE line of code!
Another good idea is to collect ALL your data first (in a variable) and then write it to a file in ONE piece.
Your example would then look like this:
...
put specialFolderPath("Preferences") & "/test" into theFile
put the label of btn "btn1" into collected_data
put CR & the label of btn "btn2" after collected_data
put CR & the label of btn "btn3" after collected_data
### add more data to the variable if necessary...
put collected_data into url("file:" & thefile)
...
That's it.
Same for reading that file in:
...
put specialFolderPath("Preferences") & "/test" into theFile
put url("file:" & thefile) into saved_data
set the label of btn "btn1" to line 1 of saved_data
set the label of btn "btn2" to line 2 of saved_data
## etc... you get the picture
..
This is more readable and will reduce read and write actions to the harddisk.
If you have a LOT of data, this may also be a bit faster than accessing the data on disk every time you need a tiny bit of it.
You can also put something BEFORE or AFTER the file, just like you would put something BEFORE or AFTER a field or variable.
So again, no need to "open file xyz for append"
...
put "This should go to line 1 of the file on disk" & CR BEFORE url("file:" & thefile)
...
put CR & "This will be the last line of the file on disk." AFTER url("file:" & thefile)
...
I hope this will work for you.
Try to get accustomed to this syntax, I really love the simplicity of it
Best
Klaus
please let me first give the advise to also read related threads here!
"newbie question; write to file, read from file?" already has much info about this topic!
I do not know what exactly is going wrong in your script but I suggest that you use another syntax to read and write things to file(s).
Using the "url" syntax**** is much more readable and much less to type, because with that syntay you do not have to "open" the file before reading/writing and "close" the files after you are finished. Rev will do that for you

***Check the docs for "URL"!
The basic syntax is for writing:
...
put "text or whatever goes here" into url("file:" & "path/to/file/here.txt")
..
for reading:
...
put url("file:" & "path/to/file/here.txt") into fld "your field here"
## or any variable...
...
As you see, this can be done with ONE line of code!
Another good idea is to collect ALL your data first (in a variable) and then write it to a file in ONE piece.
Your example would then look like this:
...
put specialFolderPath("Preferences") & "/test" into theFile
put the label of btn "btn1" into collected_data
put CR & the label of btn "btn2" after collected_data
put CR & the label of btn "btn3" after collected_data
### add more data to the variable if necessary...
put collected_data into url("file:" & thefile)
...
That's it.
Same for reading that file in:
...
put specialFolderPath("Preferences") & "/test" into theFile
put url("file:" & thefile) into saved_data
set the label of btn "btn1" to line 1 of saved_data
set the label of btn "btn2" to line 2 of saved_data
## etc... you get the picture
..
This is more readable and will reduce read and write actions to the harddisk.
If you have a LOT of data, this may also be a bit faster than accessing the data on disk every time you need a tiny bit of it.
You can also put something BEFORE or AFTER the file, just like you would put something BEFORE or AFTER a field or variable.
So again, no need to "open file xyz for append"
...
put "This should go to line 1 of the file on disk" & CR BEFORE url("file:" & thefile)
...
put CR & "This will be the last line of the file on disk." AFTER url("file:" & thefile)
...
I hope this will work for you.
Try to get accustomed to this syntax, I really love the simplicity of it

Best
Klaus
You really should use the much simpler url forms for this, as Klaus suggested.
However, It won't solve your problem, which is additional returns. If you use "read for X lines", the return at the end will be included. So the first time you use it, everything works, but the buttons will be set to have two line labels (Which I told you already above can be a problem with your approach). Then, when you run it the second time, you put the two lines into your file, and you see that every line has an additional empty line below itself in the file.
However, It won't solve your problem, which is additional returns. If you use "read for X lines", the return at the end will be included. So the first time you use it, everything works, but the buttons will be set to have two line labels (Which I told you already above can be a problem with your approach). Then, when you run it the second time, you put the two lines into your file, and you see that every line has an additional empty line below itself in the file.
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
The need to save the labels of your buttons can be avoided all together by placing your user interfaces in a substack of your mainstack. Most of the time, one must manualy saves the user's changes to a file in the preference directory like you are doing only because standalones cannot be modified. Well this can be circumvented:
Put nothing into your mainstack and build your project in as many sub stacks as you want. When building the standalone check the option "Move substacks into individual stackfiles". These can then be saved by your standalone as any Revolution stack usualy is. You don't need to worry about recording and restoring preferences at every quit & launch anymore.
Cheers.
Put nothing into your mainstack and build your project in as many sub stacks as you want. When building the standalone check the option "Move substacks into individual stackfiles". These can then be saved by your standalone as any Revolution stack usualy is. You don't need to worry about recording and restoring preferences at every quit & launch anymore.
Cheers.