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 LiveCode 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 LiveCode stack into an external file.
Upon completion of this tutorial, you should be able to:
- Use LiveCode to write a file to disk
- Use LiveCode to read a file from disk
- Understand the specialFolderPath function in LiveCode
Resources of Interest:
You can download the sample stack from this url: https://tinyurl.com/yaouhap3
Create Stack & File to Read
1. Launch LiveCode. Create a new stack by performing the menu command File -> New Stack -> Default Size. 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:
on preOpenStack put empty into fld "Shakespeare" -- initializes empty field end preOpenStack
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" end openStack
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).
Create Stack Objects
3. Create the following objects for your stack:
- a push button named "Read"
- a push button named "Write"
- a text field named "Shakespeare"
- a text field named "Rotten Wood"
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:
on mouseUp 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 end mouseUp
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:
on mouseUp open file specialFolderPath("desktop") & "/myData.txt" for write -- creates a file for LiveCode 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 end mouseUp
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 tab at the top, 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!