This tutorial and example stack use the open file, read from file, close file, and write to file commands along with the specialFolderPath function to read information into a Revolution stack from an external file (in this case, a text file, but it could be a binary file if so specified), as well as write data from a Revolution stack into an external file.
Upon completion of this tutorial, you should be able to:
•Use Revolution to write a file to disk
•Use Revolution to read a file from disk
•Understand the specialFolderPath function in Transcript
Resources of Interest:
Create Stack & File to Read
1. Launch Revolution. Create a new stack by performing the menu command File -> New Mainstack. Save your file.
2. Now we are going to create a script at the stack level that will create the text file that the stack will open to read and then write its contents in a text field. To create this handler, perform the menu command Object -> Stack Script and write the following in the resulting script editor window:
put empty into fld "Shakespeare" -- initializes empty field
on openStack -- This creates the text file on the desktop that we will later read into a stack's text field.
open file specialFolderPath("desktop") & "/SetUp.txt" for write
put "Let me not to the marriage of true minds admit impediments." into myText
write myText to file specialFolderPath("desktop") & "/SetUp.txt"
close file specialFolderPath("desktop") & "/SetUp.txt"
The openStack handler uses the open file command along with the specialFolderPath function to create a text file on your Macintosh or Windows desktop. The contents of
the text file are on the second line of the script of the openStack handler and are the first two lines of a famous poem by William Shakespeare (if you are feeling particularly creative, substitute any text for that of the Bard's). For further explanation of the specialFolderPath function, see here.
Create Stack Objects
Read from File
4. We will now script the "Read" button. To do so, click on on it then access the script editor by performing the menu command Object -> Object Script as shown above (alternatively, you can use the appropriate keystroke combination to directly open the script editor, which on a Macintosh is apple-e or command-e).
In the script editor window, enter the following script:
open file specialFolderPath("desktop") & "/SetUp.txt" for read -- opens the file created by the stack script earlier referenced
read from file specialFolderPath("desktop") & "/SetUp.txt" until EOF -- reads the contents of the file created earlier by the stack script
put it into fld "Shakespeare" -- puts the contents of that file into the text field we just created in our stack
close file specialFolderPath("desktop") & "/SetUp.txt" -- closes the file created by the stack script
Write to File
5. Similar to instruction #4 above, access the script editor window for the button "Write" and give that button the following script:
open file specialFolderPath("desktop") & "/myData.txt" for write -- creates a file for Revolution to write to
put field "Rotten Wood" into myText -- places the text from the field "Rotten Wood" into a local variable
write myText to file specialFolderPath("desktop") & "/myData.txt" -- writes the text placed in the local variable into the open text file we created
close file specialFolderPath("desktop") & "/myData.txt" -- closes/saves the text file containing the text form the field/local variable into a text file on the desktop
NOTE: We have used the "&" concatenator to create these text files for read/write purposes. "string" & "string" runs the two strings together without spaces, as in
"Foo" & "bar" evaluates to "Foobar" (no spaces)
"Foo" && "bar" evaluates to "Foo bar" (space between)
Create the Text to Write to File
6. Now we will input the text into the field "Rotten Wood" that the "Write" button will write to a text file on the desktop. To do so, either switch to browse/run mode by clicking on the plain arrow tool on the Tools Palette and click inside the "Rotten Wood" field and type the following text or access the "Contents" component of the field's Object Inspector by selecting the field by clicking on it and then performing the menu command Object -> Object Inspector and selecting "Contents" from the drop-down button, as indicated in the image above (alternatively, double-clicking on the field will also bring up the Object Inspector). Enter the text below (or whatever text which you fancy).
Say to the court it glows,
and shines like rotten wood;
Say to the church it shows
what's good and doth no good:
If court and church reply,
give court and church the lie.
Tell men of high condition
that manage the estate,
their purpose is ambition,
their practice only hate.
And if they do reply,
then thou must give the lie.
Save & Test!
7. Save your stack by performing the menu command File -> Save As... ; close it and remove its contents from memory by performing the menu command File -> Close and Remove from Memory.
8. Reopen your stack and test your buttons in browse/run mode by clicking on the simple arrow tool in the Tools Palette as indicated above; don't forget to look for the text files created on your desktop!