In this exercise we’ll explore LiveCode’s ability to read and write text to external files. Create a new Mainstack. Name it “ExternalFilesEx” and save it to your desktop. On the first card, create a field called “fileName” and another field called “fileContents”.
Note: This exercise is designed to work in a Macintosh lab. If you try this on a Windows computer, you will have to modify the file path names accordingly, including supplying the correct user name for the file path.
1. Open the message box, switch to the multi-line mode, and enter the following commands, then press Enter. Substitute your login name for username and your first name for your name.
open file "/Users/username/Desktop/your name's file"
close file "/Users/username/Desktop/your name's file"
(Do this instead if you are working on a Windows computer:
open file "C:/Documents and Settings/username/Desktop/your name's File.txt
Remember to substitute your login name for username.)
Although you may not have seen any change in LiveCode, you have just created an empty file with the name and file path you supplied above. The file should have been created on the desktop.
This is the basic framework you use with the File System Model for working with external files in LiveCode. The open file command “connects” your stack to a file of the name you specify. If the file does not exist, LiveCode creates the file.
2. Try issuing the same commands as above without a file path:
open file "your name's file"
close file "your name's file"
What does the result message in the lower part of the message box say?
If you don’t specify a file path with the open file command, the file will be created in the current defaultFolder, assuming your user has write permission for that folder. When you first launch LiveCode, the defaultFolder is set to the folder where the LiveCode application is located. If you try to create a file in a folder for which you do not have write permission, you will see an error message in the message box when you try to run these commands.
3. So far this isn’t very useful, since we are just opening and closing empty files. To write text to the file you created, use the write command. Enter these commands in the multi-line message box:
open file "/Users/macuser/Desktop/your name's file"
write "It's not empty now!" to file "/Users/macuser/Desktop/your name's file"
close file "/Users/macuser/Desktop/your name's file"
If you go back the Desktop and double-click on your file, it will open in the default text application. Notice that the phrase you typed is there.
4. It gets tedious typing in the entire path name every time. You can simplify the process by typing the entire file path into field "fileName". Do this now.... To read the contents of your file in LiveCode, use the read command. Enter the following commands in the message box. Notice how we refer to the field "fileName" instead of typing the path name in each time.
open file fld "fileName"
read from file fld "fileName" until end
close file fld "fileName"
put it into fld "fileContents"
If you did everything correctly the contents of your file should appear in card field "Contents".
Did you get an error message instead? One of the most common sources of errors is making an mistake in typing the file path. LiveCode will not open or create a file if one of the folders in the file path you used does not exist.
You now know the rudiments of reading and writing to external files in LiveCode using the File System Model.